Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/river/internal/value/errors.go
4096 views
1
package value
2
3
import "fmt"
4
5
// Error is used for reporting on a value-level error. It is the most general
6
// type of error for a value.
7
type Error struct {
8
Value Value
9
Inner error
10
}
11
12
// TypeError is used for reporting on a value having an unexpected type.
13
type TypeError struct {
14
// Value which caused the error.
15
Value Value
16
Expected Type
17
}
18
19
// Error returns the string form of the TypeError.
20
func (te TypeError) Error() string {
21
return fmt.Sprintf("expected %s, got %s", te.Expected, te.Value.Type())
22
}
23
24
// Error returns the message of the decode error.
25
func (de Error) Error() string { return de.Inner.Error() }
26
27
// MissingKeyError is used for reporting that a value is missing a key.
28
type MissingKeyError struct {
29
Value Value
30
Missing string
31
}
32
33
// Error returns the string form of the MissingKeyError.
34
func (mke MissingKeyError) Error() string {
35
return fmt.Sprintf("key %q does not exist", mke.Missing)
36
}
37
38
// ElementError is used to report on an error inside of an array.
39
type ElementError struct {
40
Value Value // The Array value
41
Index int // The index of the element with the issue
42
Inner error // The error from the element
43
}
44
45
// Error returns the text of the inner error.
46
func (ee ElementError) Error() string { return ee.Inner.Error() }
47
48
// FieldError is used to report on an invalid field inside an object.
49
type FieldError struct {
50
Value Value // The Object value
51
Field string // The field name with the issue
52
Inner error // The error from the field
53
}
54
55
// Error returns the text of the inner error.
56
func (fe FieldError) Error() string { return fe.Inner.Error() }
57
58
// ArgError is used to report on an invalid argument to a function.
59
type ArgError struct {
60
Function Value
61
Argument Value
62
Index int
63
Inner error
64
}
65
66
// Error returns the text of the inner error.
67
func (ae ArgError) Error() string { return ae.Inner.Error() }
68
69
// WalkError walks err for all value-related errors in this package.
70
// WalkError returns false if err is not an error from this package.
71
func WalkError(err error, f func(err error)) bool {
72
var foundOne bool
73
74
nextError := err
75
for nextError != nil {
76
switch ne := nextError.(type) {
77
case Error:
78
f(nextError)
79
nextError = ne.Inner
80
foundOne = true
81
case TypeError:
82
f(nextError)
83
nextError = nil
84
foundOne = true
85
case MissingKeyError:
86
f(nextError)
87
nextError = nil
88
foundOne = true
89
case ElementError:
90
f(nextError)
91
nextError = ne.Inner
92
foundOne = true
93
case FieldError:
94
f(nextError)
95
nextError = ne.Inner
96
foundOne = true
97
case ArgError:
98
f(nextError)
99
nextError = ne.Inner
100
foundOne = true
101
default:
102
nextError = nil
103
}
104
}
105
106
return foundOne
107
}
108
109