-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
72 lines (58 loc) · 1.33 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
package jsonry
import (
"encoding/json"
"fmt"
"reflect"
"code.cloudfoundry.org/jsonry/internal/context"
)
type unsupportedType struct {
context context.Context
typ reflect.Type
}
func newUnsupportedTypeError(ctx context.Context, t reflect.Type) error {
return &unsupportedType{
context: ctx,
typ: t,
}
}
func (u unsupportedType) Error() string {
return fmt.Sprintf(`unsupported type "%s" at %s`, u.typ, u.context)
}
type unsupportedKeyType struct {
context context.Context
typ reflect.Type
}
func newUnsupportedKeyTypeError(ctx context.Context, t reflect.Type) error {
return &unsupportedKeyType{
context: ctx,
typ: t,
}
}
func (u unsupportedKeyType) Error() string {
return fmt.Sprintf(`maps must only have string keys for "%s" at %s`, u.typ, u.context)
}
type conversionError struct {
context context.Context
value interface{}
}
func newConversionError(ctx context.Context, value interface{}) error {
return &conversionError{
context: ctx,
value: value,
}
}
func (c conversionError) Error() 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 " + c.context.String()
}