-
Notifications
You must be signed in to change notification settings - Fork 20
/
unmarshal-error.go
133 lines (111 loc) · 3.16 KB
/
unmarshal-error.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
package goq
import (
"fmt"
"reflect"
)
// All "Reason" fields within CannotUnmarshalError will be constants and part of
// this list
const (
nonPointer = "non-pointer value"
nilValue = "destination argument is nil"
documentReadError = "error reading goquery document"
arrayLengthMismatch = "array length does not match document elements found"
customUnmarshalError = "a custom Unmarshaler implementation threw an error"
typeConversionError = "a type conversion error occurred"
mapKeyUnmarshalError = "error unmarshaling a map key"
missingValueSelector = "at least one value selector must be passed to use as map index"
)
// CannotUnmarshalError represents an error returned by the goquery Unmarshaler
// and helps consumers in programmatically diagnosing the cause of their error.
type CannotUnmarshalError struct {
Err error
Val string
FldOrIdx interface{}
V reflect.Value
Reason string
}
// This type is a mid-level abstraction to help understand the error printing logic
type errChain struct {
chain []*CannotUnmarshalError
val string
tail error
}
// tPath returns the type path in the same string format one might use to access
// the nested value in go code. This should hopefully help make debugging easier.
func (e errChain) tPath() string {
nest := ""
for _, err := range e.chain {
if err.FldOrIdx != nil {
switch nesting := err.FldOrIdx.(type) {
case string:
switch err.V.Type().Kind() {
case reflect.Map:
nest += fmt.Sprintf("[%q]", nesting)
case reflect.Struct:
nest += fmt.Sprintf(".%s", nesting)
}
case int:
nest += fmt.Sprintf("[%d]", nesting)
case *int:
nest += fmt.Sprintf("[%d]", *nesting)
default:
fmt.Printf("err.FldOrIdx = %#v\n", err.FldOrIdx)
nest += fmt.Sprintf("[%v]", nesting)
}
}
}
return nest
}
func (e errChain) last() *CannotUnmarshalError {
return e.chain[len(e.chain)-1]
}
// Error gives a human-readable error message for debugging purposes.
func (e errChain) Error() string {
last := e.last()
// Avoid panic if we cannot get a type name for the Value
t := "unknown: invalid value"
if last.V.IsValid() {
t = last.V.Type().String()
}
msg := "could not unmarshal "
if e.val != "" {
msg += fmt.Sprintf("value %q ", e.val)
}
msg += fmt.Sprintf(
"into '%s%s' (type %s): %s",
e.chain[0].V.Type(),
e.tPath(),
t,
last.Reason,
)
// If a generic error was reported elsewhere, report its message last
if e.tail != nil {
msg = msg + ": " + e.tail.Error()
}
return msg
}
// Traverse e.Err, printing hopefully helpful type info until there are no more
// chained errors.
func (e *CannotUnmarshalError) unwind() *errChain {
str := &errChain{chain: []*CannotUnmarshalError{}}
for {
str.chain = append(str.chain, e)
if e.Val != "" {
str.val = e.Val
}
// Terminal error was of type *CannotUnmarshalError and had no children
if e.Err == nil {
return str
}
if e2, ok := e.Err.(*CannotUnmarshalError); ok {
e = e2
continue
}
// Child error was not a *CannotUnmarshalError; print its message
str.tail = e.Err
return str
}
}
func (e *CannotUnmarshalError) Error() string {
return e.unwind().Error()
}