@@ -21,10 +21,22 @@ const (
2121 defaultIncludeSource = true
2222)
2323
24- type PrettyFormatError interface {
24+ type Error interface {
25+ error
26+ GetToken () * token.Token
27+ GetMessage () string
2528 FormatError (bool , bool ) string
2629}
2730
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+
2840type SyntaxError struct {
2941 Message string
3042 Token * token.Token
@@ -109,20 +121,40 @@ func ErrUnexpectedNodeType(actual, expected ast.NodeType, tk *token.Token) *Unex
109121 }
110122}
111123
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+
112132func (e * SyntaxError ) Error () string {
113133 return e .FormatError (defaultFormatColor , defaultIncludeSource )
114134}
115135
116136func (e * SyntaxError ) FormatError (colored , inclSource bool ) string {
117- return formatError (e .Message , e .Token , colored , inclSource )
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
118146}
119147
120148func (e * OverflowError ) Error () string {
121149 return e .FormatError (defaultFormatColor , defaultIncludeSource )
122150}
123151
124152func (e * OverflowError ) FormatError (colored , inclSource bool ) string {
125- return formatError (fmt .Sprintf ("cannot unmarshal %s into Go value of type %s ( overflow )" , e .SrcNum , e .DstType ), e .Token , colored , inclSource )
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 )
126158}
127159
128160func (e * TypeError ) msg () string {
@@ -132,39 +164,75 @@ func (e *TypeError) msg() string {
132164 return fmt .Sprintf ("cannot unmarshal %s into Go value of type %s" , e .SrcType , e .DstType )
133165}
134166
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+
135175func (e * TypeError ) Error () string {
136176 return e .FormatError (defaultFormatColor , defaultIncludeSource )
137177}
138178
139179func (e * TypeError ) FormatError (colored , inclSource bool ) string {
140- return formatError (e .msg (), e .Token , colored , inclSource )
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
141189}
142190
143191func (e * DuplicateKeyError ) Error () string {
144192 return e .FormatError (defaultFormatColor , defaultIncludeSource )
145193}
146194
147195func (e * DuplicateKeyError ) FormatError (colored , inclSource bool ) string {
148- return formatError (e .Message , e .Token , colored , inclSource )
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
149205}
150206
151207func (e * UnknownFieldError ) Error () string {
152208 return e .FormatError (defaultFormatColor , defaultIncludeSource )
153209}
154210
155211func (e * UnknownFieldError ) FormatError (colored , inclSource bool ) string {
156- return formatError (e .Message , e .Token , colored , inclSource )
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
157221}
158222
159223func (e * UnexpectedNodeTypeError ) Error () string {
160224 return e .FormatError (defaultFormatColor , defaultIncludeSource )
161225}
162226
163227func (e * UnexpectedNodeTypeError ) FormatError (colored , inclSource bool ) string {
164- return formatError (fmt .Sprintf ("%s was used where %s is expected" , e .Actual .YAMLName (), e .Expected .YAMLName ()), e .Token , colored , inclSource )
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 ())
165233}
166234
167- func formatError (errMsg string , token * token.Token , colored , inclSource bool ) string {
235+ func FormatError (errMsg string , token * token.Token , colored , inclSource bool ) string {
168236 var pp printer.Printer
169237 pos := fmt .Sprintf ("[%d:%d] " , token .Position .Line , token .Position .Column )
170238 msg := pp .PrintErrorMessage (fmt .Sprintf ("%s%s" , pos , errMsg ), colored )
0 commit comments