Debian Codegraph @

internal/errors/error.go

raw symbols SCIP
1 package errors
2
3 import (
4 "errors"
5 "fmt"
6 "reflect"
7
8 "github.com/goccy/go-yaml/ast"
9 "github.com/goccy/go-yaml/printer"
10 "github.com/goccy/go-yaml/token"
11 )
12
13 var (
14 As = errors.As
15 Is = errors.Is
16 New = errors.New
17 )
18
19 const (
20 defaultFormatColor = false
21 defaultIncludeSource = true
22 )
23
24 type Error interface {
25 error
26 GetToken() *token.Token
27 GetMessage() string
28 FormatError(bool, bool) string
29 }
30
31 var (
32 _ Error = new(SyntaxError)
33 _ Error = new(TypeError)
34 _ Error = new(OverflowError)
35 _ Error = new(DuplicateKeyError)
36 _ Error = new(UnknownFieldError)
37 _ Error = new(UnexpectedNodeTypeError)
38 )
39
40 type SyntaxError struct {
41 Message string
42 Token *token.Token
43 }
44
45 type TypeError struct {
46 DstType reflect.Type
47 SrcType reflect.Type
48 StructFieldName *string
49 Token *token.Token
50 }
51
52 type OverflowError struct {
53 DstType reflect.Type
54 SrcNum string
55 Token *token.Token
56 }
57
58 type DuplicateKeyError struct {
59 Message string
60 Token *token.Token
61 }
62
63 type UnknownFieldError struct {
64 Message string
65 Token *token.Token
66 }
67
68 type UnexpectedNodeTypeError struct {
69 Actual ast.NodeType
70 Expected ast.NodeType
71 Token *token.Token
72 }
73
74 // ErrSyntax create syntax error instance with message and token
75 func ErrSyntax(msg string, tk *token.Token) *SyntaxError {
76 return &SyntaxError{
77 Message: msg,
78 Token: tk,
79 }
80 }
81
82 // ErrOverflow creates an overflow error instance with message and a token.
83 func ErrOverflow(dstType reflect.Type, num string, tk *token.Token) *OverflowError {
84 return &OverflowError{
85 DstType: dstType,
86 SrcNum: num,
87 Token: tk,
88 }
89 }
90
91 // ErrTypeMismatch cerates an type mismatch error instance with token.
92 func ErrTypeMismatch(dstType, srcType reflect.Type, token *token.Token) *TypeError {
93 return &TypeError{
94 DstType: dstType,
95 SrcType: srcType,
96 Token: token,
97 }
98 }
99
100 // ErrDuplicateKey creates an duplicate key error instance with token.
101 func ErrDuplicateKey(msg string, tk *token.Token) *DuplicateKeyError {
102 return &DuplicateKeyError{
103 Message: msg,
104 Token: tk,
105 }
106 }
107
108 // ErrUnknownField creates an unknown field error instance with token.
109 func ErrUnknownField(msg string, tk *token.Token) *UnknownFieldError {
110 return &UnknownFieldError{
111 Message: msg,
112 Token: tk,
113 }
114 }
115
116 func ErrUnexpectedNodeType(actual, expected ast.NodeType, tk *token.Token) *UnexpectedNodeTypeError {
117 return &UnexpectedNodeTypeError{
118 Actual: actual,
119 Expected: expected,
120 Token: tk,
121 }
122 }
123
124 func (e *SyntaxError) GetMessage() string {
125 return e.Message
126 }
127
128 func (e *SyntaxError) GetToken() *token.Token {
129 return e.Token
130 }
131
132 func (e *SyntaxError) Error() string {
133 return e.FormatError(defaultFormatColor, defaultIncludeSource)
134 }
135
136 func (e *SyntaxError) FormatError(colored, inclSource bool) string {
137 return FormatError(e.Message, e.Token, colored, inclSource)
138 }
139
140 func (e *OverflowError) GetMessage() string {
141 return e.msg()
142 }
143
144 func (e *OverflowError) GetToken() *token.Token {
145 return e.Token
146 }
147
148 func (e *OverflowError) Error() string {
149 return e.FormatError(defaultFormatColor, defaultIncludeSource)
150 }
151
152 func (e *OverflowError) FormatError(colored, inclSource bool) string {
153 return FormatError(e.msg(), e.Token, colored, inclSource)
154 }
155
156 func (e *OverflowError) msg() string {
157 return fmt.Sprintf("cannot unmarshal %s into Go value of type %s ( overflow )", e.SrcNum, e.DstType)
158 }
159
160 func (e *TypeError) msg() string {
161 if e.StructFieldName != nil {
162 return fmt.Sprintf("cannot unmarshal %s into Go struct field %s of type %s", e.SrcType, *e.StructFieldName, e.DstType)
163 }
164 return fmt.Sprintf("cannot unmarshal %s into Go value of type %s", e.SrcType, e.DstType)
165 }
166
167 func (e *TypeError) GetMessage() string {
168 return e.msg()
169 }
170
171 func (e *TypeError) GetToken() *token.Token {
172 return e.Token
173 }
174
175 func (e *TypeError) Error() string {
176 return e.FormatError(defaultFormatColor, defaultIncludeSource)
177 }
178
179 func (e *TypeError) FormatError(colored, inclSource bool) string {
180 return FormatError(e.msg(), e.Token, colored, inclSource)
181 }
182
183 func (e *DuplicateKeyError) GetMessage() string {
184 return e.Message
185 }
186
187 func (e *DuplicateKeyError) GetToken() *token.Token {
188 return e.Token
189 }
190
191 func (e *DuplicateKeyError) Error() string {
192 return e.FormatError(defaultFormatColor, defaultIncludeSource)
193 }
194
195 func (e *DuplicateKeyError) FormatError(colored, inclSource bool) string {
196 return FormatError(e.Message, e.Token, colored, inclSource)
197 }
198
199 func (e *UnknownFieldError) GetMessage() string {
200 return e.Message
201 }
202
203 func (e *UnknownFieldError) GetToken() *token.Token {
204 return e.Token
205 }
206
207 func (e *UnknownFieldError) Error() string {
208 return e.FormatError(defaultFormatColor, defaultIncludeSource)
209 }
210
211 func (e *UnknownFieldError) FormatError(colored, inclSource bool) string {
212 return FormatError(e.Message, e.Token, colored, inclSource)
213 }
214
215 func (e *UnexpectedNodeTypeError) GetMessage() string {
216 return e.msg()
217 }
218
219 func (e *UnexpectedNodeTypeError) GetToken() *token.Token {
220 return e.Token
221 }
222
223 func (e *UnexpectedNodeTypeError) Error() string {
224 return e.FormatError(defaultFormatColor, defaultIncludeSource)
225 }
226
227 func (e *UnexpectedNodeTypeError) FormatError(colored, inclSource bool) string {
228 return FormatError(e.msg(), e.Token, colored, inclSource)
229 }
230
231 func (e *UnexpectedNodeTypeError) msg() string {
232 return fmt.Sprintf("%s was used where %s is expected", e.Actual.YAMLName(), e.Expected.YAMLName())
233 }
234
235 func FormatError(errMsg string, token *token.Token, colored, inclSource bool) string {
236 var pp printer.Printer
237 if token == nil {
238 return pp.PrintErrorMessage(errMsg, colored)
239 }
240 pos := fmt.Sprintf("[%d:%d] ", token.Position.Line, token.Position.Column)
241 msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, errMsg), colored)
242 if inclSource {
243 msg += "\n" + pp.PrintErrorToken(token, colored)
244 }
245 return msg
246 }