-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
179 lines (154 loc) · 3.73 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package yaml
import (
"fmt"
"reflect"
"github.com/go-faster/errors"
)
var _ = []interface {
error
}{
(*SyntaxError)(nil),
(*UnmarshalError)(nil),
}
// SyntaxError is an error that occurs during parsing.
type SyntaxError struct {
Offset int
Line int
Column int
Msg string
}
func syntaxErr(offset, line, column int, msg string) error {
return &SyntaxError{
Offset: offset,
Line: line,
Column: column,
Msg: msg,
}
}
func syntaxErrf(offset, line, column int, msgf string, args ...any) error {
return &SyntaxError{
Offset: offset,
Line: line,
Column: column,
Msg: fmt.Sprintf(msgf, args...),
}
}
// Error returns the error message.
func (s *SyntaxError) Error() string {
if s.Line == 0 {
if s.Offset == 0 {
return fmt.Sprintf("yaml: %s", s.Msg)
}
return fmt.Sprintf("yaml: offset %d: %s", s.Offset, s.Msg)
}
if s.Column == 0 {
return fmt.Sprintf("yaml: line %d: %s", s.Line, s.Msg)
}
return fmt.Sprintf("yaml: line %d:%d: %s", s.Line, s.Column, s.Msg)
}
// UnknownFieldError reports an unknown field.
type UnknownFieldError struct {
Field string
Type reflect.Type
}
// Error returns the error message.
func (d *UnknownFieldError) Error() string {
return fmt.Sprintf("field %q not found in type %s", d.Field, d.Type)
}
func unknownFieldErr(field string, f *Node, typ reflect.Type) error {
return &UnmarshalError{
Node: f,
Type: typ,
Err: &UnknownFieldError{Field: field, Type: typ},
}
}
// DuplicateKeyError reports a duplicate key.
type DuplicateKeyError struct {
First, Second *Node
}
func duplicateKeyErr(f, s *Node, typ reflect.Type) error {
return &UnmarshalError{
Node: f,
Type: typ,
Err: &DuplicateKeyError{First: f, Second: s},
}
}
// Error returns the error message.
func (d *DuplicateKeyError) Error() string {
f, s := d.First, d.Second
if s == nil {
return fmt.Sprintf("duplicate key: %q", f.Value)
}
switch s.Kind {
case SequenceNode, MappingNode:
return fmt.Sprintf("mapping key already defined at line %d", s.Line)
default:
return fmt.Sprintf("mapping key %q already defined at line %d", s.Value, s.Line)
}
}
// UnmarshalError is an error that occurs during unmarshaling.
type UnmarshalError struct {
Node *Node
Type reflect.Type
Err error
}
func unmarshalErrf(n *Node, typ reflect.Type, msgf string, args ...any) error {
return &UnmarshalError{
Node: n,
Type: typ,
Err: errors.Errorf(msgf, args...),
}
}
// Unwrap returns the underlying error.
func (s *UnmarshalError) Unwrap() error {
return s.Err
}
// Error returns the error message.
func (s *UnmarshalError) Error() string {
n := s.Node
if n == nil || n.Line == 0 {
return fmt.Sprintf("yaml: %s", s.Err)
}
return fmt.Sprintf("yaml: line %d: %s", n.Line, s.Err)
}
// MarshalError is an error that occurs during marshaling.
type MarshalError struct {
Msg string
}
// Error returns the error message.
func (s *MarshalError) Error() string {
return fmt.Sprintf("yaml: %s", s.Msg)
}
// A TypeError is returned by Unmarshal when one or more fields in
// the YAML document cannot be properly decoded into the requested
// types. When this error is returned, the value is still
// unmarshaled partially.
//
// Group is a multi-error which contains all errors that occurred.
// Use multierr.Errors to get a list of all errors.
type TypeError struct {
Group error
}
// Unwrap returns the underlying error.
func (e *TypeError) Unwrap() error {
return e.Group
}
// Error returns the error message.
func (e *TypeError) Error() string {
return fmt.Sprintf("yaml: unmarshal errors:\n %s", e.Group)
}
func handleErr(err *error) {
if v := recover(); v != nil {
if e, ok := v.(yamlError); ok {
*err = e.err
} else {
panic(v)
}
}
}
type yamlError struct {
err error
}
func fail(err error) {
panic(yamlError{err})
}