-
Notifications
You must be signed in to change notification settings - Fork 9
/
err_context.go
102 lines (85 loc) · 2.53 KB
/
err_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package console
import (
"context"
"io"
"sync"
"namespacelabs.dev/foundation/internal/console/consolesink"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/std/tasks"
)
var errCtx = newErrContext()
// errContext stores Attachment's buffer names that may provide additional context in case of an error.
type errContext struct {
mu sync.Mutex
// provides errorContext for a specific action (keyed by ActionID).
perAction map[tasks.ActionID]*actionErrContext
}
type actionErrContext struct {
mu sync.Mutex
buffNames []tasks.OutputName
}
func newErrContext() *errContext {
return &errContext{
perAction: make(map[tasks.ActionID]*actionErrContext),
}
}
// WithLogs adds additional context to the error message, but only if a given message
// hasn't been output in the most recent log lines.
func WithLogs(ctx context.Context, err error) error {
if true {
// Disable logs processing until the formatter below is updated to deal
// with structured logs.
return err
}
sink := UnwrapSink(tasks.SinkFrom(ctx))
if sink == nil {
return err
}
attachments := tasks.Attachments(ctx)
if consoleSink, ok := sink.(*consolesink.ConsoleSink); ok {
// Only skip the error message
if consoleSink.RecentInputSourcesContain(attachments.ActionID()) {
return err
}
}
bufNames := GetErrContext(ctx).getBufNames()
for i := range bufNames {
err = fnerrors.WithLogs(
err,
func() io.Reader {
return attachments.ReaderByOutputName(bufNames[len(bufNames)-i-1])
})
// TODO: allow multi buffer as contexts. As for now we use the last buffer as a heuristic.
break
}
return err
}
func (err *actionErrContext) AddLog(name tasks.OutputName) {
err.mu.Lock()
defer err.mu.Unlock()
err.buffNames = append(err.buffNames, name)
}
func GetErrContext(ctx context.Context) *actionErrContext {
actionId := tasks.Attachments(ctx).ActionID()
errCtx.mu.Lock()
defer errCtx.mu.Unlock()
aec, present := errCtx.perAction[actionId]
if !present {
aec = &actionErrContext{
buffNames: []tasks.OutputName{},
}
errCtx.perAction[actionId] = aec
}
return aec
}
func (err *actionErrContext) getBufNames() []tasks.OutputName {
err.mu.Lock()
defer err.mu.Unlock()
ret := make([]tasks.OutputName, len(err.buffNames))
// TODO prefer buffers with errors over those without errors
copy(ret, err.buffNames)
return ret
}