summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/github.com/goccy/go-yaml@v1.9.6/internal/errors/error.go
blob: 7f1ea9af7e906ff7bb5f061eaa56e12c32464d5f (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package errors

import (
	"bytes"
	"fmt"
	"reflect"

	"github.com/goccy/go-yaml/printer"
	"github.com/goccy/go-yaml/token"
	"golang.org/x/xerrors"
)

const (
	defaultColorize      = false
	defaultIncludeSource = true
)

var (
	ErrDecodeRequiredPointerType = xerrors.New("required pointer type value")
)

// Wrapf wrap error for stack trace
func Wrapf(err error, msg string, args ...interface{}) error {
	return &wrapError{
		baseError: &baseError{},
		err:       xerrors.Errorf(msg, args...),
		nextErr:   err,
		frame:     xerrors.Caller(1),
	}
}

// ErrSyntax create syntax error instance with message and token
func ErrSyntax(msg string, tk *token.Token) *syntaxError {
	return &syntaxError{
		baseError: &baseError{},
		msg:       msg,
		token:     tk,
		frame:     xerrors.Caller(1),
	}
}

type baseError struct {
	state fmt.State
	verb  rune
}

func (e *baseError) Error() string {
	return ""
}

func (e *baseError) chainStateAndVerb(err error) {
	wrapErr, ok := err.(*wrapError)
	if ok {
		wrapErr.state = e.state
		wrapErr.verb = e.verb
	}
	syntaxErr, ok := err.(*syntaxError)
	if ok {
		syntaxErr.state = e.state
		syntaxErr.verb = e.verb
	}
}

type wrapError struct {
	*baseError
	err     error
	nextErr error
	frame   xerrors.Frame
}

type FormatErrorPrinter struct {
	xerrors.Printer
	Colored    bool
	InclSource bool
}

func (e *wrapError) As(target interface{}) bool {
	err := e.nextErr
	for {
		if wrapErr, ok := err.(*wrapError); ok {
			err = wrapErr.nextErr
			continue
		}
		break
	}
	return xerrors.As(err, target)
}

func (e *wrapError) Unwrap() error {
	return e.nextErr
}

func (e *wrapError) PrettyPrint(p xerrors.Printer, colored, inclSource bool) error {
	return e.FormatError(&FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource})
}

func (e *wrapError) FormatError(p xerrors.Printer) error {
	if _, ok := p.(*FormatErrorPrinter); !ok {
		p = &FormatErrorPrinter{
			Printer:    p,
			Colored:    defaultColorize,
			InclSource: defaultIncludeSource,
		}
	}
	if e.verb == 'v' && e.state.Flag('+') {
		// print stack trace for debugging
		p.Print(e.err, "\n")
		e.frame.Format(p)
		e.chainStateAndVerb(e.nextErr)
		return e.nextErr
	}
	err := e.nextErr
	for {
		if wrapErr, ok := err.(*wrapError); ok {
			err = wrapErr.nextErr
			continue
		}
		break
	}
	e.chainStateAndVerb(err)
	if fmtErr, ok := err.(xerrors.Formatter); ok {
		fmtErr.FormatError(p)
	} else {
		p.Print(err)
	}
	return nil
}

type wrapState struct {
	org fmt.State
}

func (s *wrapState) Write(b []byte) (n int, err error) {
	return s.org.Write(b)
}

func (s *wrapState) Width() (wid int, ok bool) {
	return s.org.Width()
}

func (s *wrapState) Precision() (prec int, ok bool) {
	return s.org.Precision()
}

func (s *wrapState) Flag(c int) bool {
	// set true to 'printDetail' forced because when p.Detail() is false, xerrors.Printer no output any text
	if c == '#' {
		// ignore '#' keyword because xerrors.FormatError doesn't set true to printDetail.
		// ( see https://github.com/golang/xerrors/blob/master/adaptor.go#L39-L43 )
		return false
	}
	return true
}

func (e *wrapError) Format(state fmt.State, verb rune) {
	e.state = state
	e.verb = verb
	xerrors.FormatError(e, &wrapState{org: state}, verb)
}

func (e *wrapError) Error() string {
	var buf bytes.Buffer
	e.PrettyPrint(&Sink{&buf}, defaultColorize, defaultIncludeSource)
	return buf.String()
}

type syntaxError struct {
	*baseError
	msg   string
	token *token.Token
	frame xerrors.Frame
}

func (e *syntaxError) PrettyPrint(p xerrors.Printer, colored, inclSource bool) error {
	return e.FormatError(&FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource})
}

func (e *syntaxError) FormatError(p xerrors.Printer) error {
	var pp printer.Printer

	var colored, inclSource bool
	if fep, ok := p.(*FormatErrorPrinter); ok {
		colored = fep.Colored
		inclSource = fep.InclSource
	}

	pos := fmt.Sprintf("[%d:%d] ", e.token.Position.Line, e.token.Position.Column)
	msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, e.msg), colored)
	if inclSource {
		msg += "\n" + pp.PrintErrorToken(e.token, colored)
	}
	p.Print(msg)

	if e.verb == 'v' && e.state.Flag('+') {
		// %+v
		// print stack trace for debugging
		e.frame.Format(p)
	}
	return nil
}

type PrettyPrinter interface {
	PrettyPrint(xerrors.Printer, bool, bool) error
}
type Sink struct{ *bytes.Buffer }

func (es *Sink) Print(args ...interface{}) {
	fmt.Fprint(es.Buffer, args...)
}

func (es *Sink) Printf(f string, args ...interface{}) {
	fmt.Fprintf(es.Buffer, f, args...)
}

func (es *Sink) Detail() bool {
	return false
}

func (e *syntaxError) Error() string {
	var buf bytes.Buffer
	e.PrettyPrint(&Sink{&buf}, defaultColorize, defaultIncludeSource)
	return buf.String()
}

type TypeError struct {
	DstType         reflect.Type
	SrcType         reflect.Type
	StructFieldName *string
	Token           *token.Token
}

func (e *TypeError) Error() string {
	if e.StructFieldName != nil {
		return fmt.Sprintf("cannot unmarshal %s into Go struct field %s of type %s", e.SrcType, *e.StructFieldName, e.DstType)
	}
	return fmt.Sprintf("cannot unmarshal %s into Go value of type %s", e.SrcType, e.DstType)
}

func (e *TypeError) PrettyPrint(p xerrors.Printer, colored, inclSource bool) error {
	return e.FormatError(&FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource})
}

func (e *TypeError) FormatError(p xerrors.Printer) error {
	var pp printer.Printer

	var colored, inclSource bool
	if fep, ok := p.(*FormatErrorPrinter); ok {
		colored = fep.Colored
		inclSource = fep.InclSource
	}

	pos := fmt.Sprintf("[%d:%d] ", e.Token.Position.Line, e.Token.Position.Column)
	msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, e.Error()), colored)
	if inclSource {
		msg += "\n" + pp.PrintErrorToken(e.Token, colored)
	}
	p.Print(msg)

	return nil
}