forked from mitchellh/mapstructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
133 lines (112 loc) · 3.11 KB
/
error.go
File metadata and controls
133 lines (112 loc) · 3.11 KB
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 mapstructure
import (
"fmt"
"reflect"
"sort"
"strings"
)
// FieldError implements the error interface and provide access to
// field path where that error occurred.
type FieldError interface {
error
Path() FieldPath
}
// baseError default implementation of FieldError.
type baseError struct {
error
path FieldPath
}
func (e baseError) Path() FieldPath {
return e.path
}
func formatError(path FieldPath, format string, a ...interface{}) error {
return &baseError{
path: path,
error: fmt.Errorf(format, a...),
}
}
// SliceExpectedError implements the error interface. It provides access to
// field path where that error occurred and specific for this type of errors parameters.
type SliceExpectedError struct {
path FieldPath
Got reflect.Kind
}
func (e SliceExpectedError) Path() FieldPath {
return e.path
}
func (e *SliceExpectedError) Error() string {
return fmt.Sprintf(
"'%s': source data must be an array or slice, got %s", e.path, e.Got)
}
// UnexpectedUnconvertibleTypeError implements the error interface. It provides access to
// field path where that error occurred and specific for this type of errors parameters.
type UnexpectedUnconvertibleTypeError struct {
path FieldPath
Expected reflect.Type
Got reflect.Type
Data interface{}
}
func (e UnexpectedUnconvertibleTypeError) Path() FieldPath {
return e.path
}
func (e *UnexpectedUnconvertibleTypeError) Error() string {
return fmt.Sprintf(
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
e.path, e.Expected, e.Got, e.Data)
}
// CanNotParseError implements the error interface. It provides access to
// field path where that error occurred and specific for this type of errors parameters.
type CanNotParseError struct {
path FieldPath
Reason error
Type string
}
func (e CanNotParseError) Path() FieldPath {
return e.path
}
func (e *CanNotParseError) Error() string {
return fmt.Sprintf("cannot parse '%s' as %s: %s", e.path, e.Type, e.Reason)
}
// Error implements the error interface and can represents multiple
// errors that occur in the course of a single decode.
type Error struct {
// Deprecated: left for backward compatibility.
Errors []string
realErrors []error
}
func newMultiError(errors []error) *Error {
stringErrors := make([]string, len(errors))
for i, err := range errors {
stringErrors[i] = err.Error()
}
return &Error{
Errors: stringErrors,
realErrors: errors,
}
}
func (e *Error) Error() string {
points := make([]string, len(e.realErrors))
for i, err := range e.realErrors {
points[i] = fmt.Sprintf("* %s", err.Error())
}
sort.Strings(points)
return fmt.Sprintf(
"%d error(s) decoding:\n\n%s",
len(e.realErrors), strings.Join(points, "\n"))
}
// WrappedErrors implements the errwrap.Wrapper interface to make this
// return value more useful with the errwrap and go-multierror libraries.
func (e *Error) WrappedErrors() []error {
if e == nil {
return nil
}
return e.realErrors
}
func appendErrors(errors []error, err error) []error {
switch e := err.(type) {
case *Error:
return append(errors, e.WrappedErrors()...)
default:
return append(errors, e)
}
}