-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
151 lines (125 loc) · 3.09 KB
/
errors.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package jsonry
import (
"encoding/json"
"fmt"
"reflect"
"code.cloudfoundry.org/jsonry/internal/errorcontext"
)
type unsupportedType struct {
typ reflect.Type
}
func newUnsupportedTypeError(t reflect.Type) error {
return &unsupportedType{
typ: t,
}
}
func (u unsupportedType) Error() string {
return u.message(errorcontext.ErrorContext{})
}
func (u unsupportedType) message(ctx errorcontext.ErrorContext) string {
return fmt.Sprintf(`unsupported type "%s" at %s`, u.typ, ctx)
}
type unsupportedKeyType struct {
typ reflect.Type
}
func newUnsupportedKeyTypeError(t reflect.Type) error {
return &unsupportedKeyType{
typ: t,
}
}
func (u unsupportedKeyType) Error() string {
return u.message(errorcontext.ErrorContext{})
}
func (u unsupportedKeyType) message(ctx errorcontext.ErrorContext) string {
return fmt.Sprintf(`maps must only have string keys for "%s" at %s`, u.typ, ctx)
}
type conversionError struct {
value interface{}
}
func newConversionError(value interface{}) error {
return &conversionError{
value: value,
}
}
func (c conversionError) Error() string {
return c.message(errorcontext.ErrorContext{})
}
func (c conversionError) message(ctx errorcontext.ErrorContext) string {
var t string
switch c.value.(type) {
case nil:
case json.Number:
t = "number"
default:
t = reflect.TypeOf(c.value).String()
}
msg := fmt.Sprintf(`cannot unmarshal "%+v" `, c.value)
if t != "" {
msg = fmt.Sprintf(`%stype "%s" `, msg, t)
}
return msg + "into " + ctx.String()
}
type foreignError struct {
msg string
cause error
}
func newForeignError(msg string, cause error) error {
return foreignError{
msg: msg,
cause: cause,
}
}
func (e foreignError) Error() string {
return e.message(errorcontext.ErrorContext{})
}
func (e foreignError) message(ctx errorcontext.ErrorContext) string {
return fmt.Sprintf("%s at %s: %s", e.msg, ctx, e.cause)
}
type contextError struct {
cause error
context errorcontext.ErrorContext
}
func (c contextError) Error() string {
if e, ok := c.cause.(interface {
message(errorcontext.ErrorContext) string
}); ok {
return e.message(c.context)
}
return c.cause.Error()
}
func wrapErrorWithFieldContext(err error, fieldName string, fieldType reflect.Type) error {
switch e := err.(type) {
case contextError:
e.context = e.context.WithField(fieldName, fieldType)
return e
default:
return contextError{
cause: err,
context: errorcontext.ErrorContext{}.WithField(fieldName, fieldType),
}
}
}
func wrapErrorWithIndexContext(err error, index int, elementType reflect.Type) error {
switch e := err.(type) {
case contextError:
e.context = e.context.WithIndex(index, elementType)
return e
default:
return contextError{
cause: err,
context: errorcontext.ErrorContext{}.WithIndex(index, elementType),
}
}
}
func wrapErrorWithKeyContext(err error, keyName string, valueType reflect.Type) error {
switch e := err.(type) {
case contextError:
e.context = e.context.WithKey(keyName, valueType)
return e
default:
return contextError{
cause: err,
context: errorcontext.ErrorContext{}.WithKey(keyName, valueType),
}
}
}