Debian Codegraph @

dependencies/pkg/mod/github.com/goccy/[email protected]/internal/errors/error.go

raw
1 package errors
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "reflect"
8
9 "github.com/goccy/go-yaml/printer"
10 "github.com/goccy/go-yaml/token"
11 )
12
13 const (
14 defaultColorize = false
15 defaultIncludeSource = true
16 )
17
18 // ErrSyntax create syntax error instance with message and token
19 func ErrSyntax(msg string, tk *token.Token) *syntaxError {
20 return &syntaxError{
21 msg: msg,
22 token: tk,
23 }
24 }
25
26 // ErrOverflow creates an overflow error instance with message and a token.
27 func ErrOverflow(dstType reflect.Type, num string, tk *token.Token) *overflowError {
28 return &overflowError{dstType: dstType, srcNum: num, token: tk}
29 }
30
31 type Printer interface {
32 // Print appends args to the message output.
33 Print(args ...any)
34 }
35
36 type FormatErrorPrinter struct {
37 Printer
38 Colored bool
39 InclSource bool
40 }
41
42 var (
43 As = errors.As
44 Is = errors.Is
45 New = errors.New
46 )
47
48 type overflowError struct {
49 dstType reflect.Type
50 srcNum string
51 token *token.Token
52 }
53
54 func (e *overflowError) Error() string {
55 return fmt.Sprintf("cannot unmarshal %s into Go value of type %s ( overflow )", e.srcNum, e.dstType)
56 }
57
58 func (e *overflowError) PrettyPrint(p Printer, colored, inclSource bool) error {
59 return e.FormatError(&FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource})
60 }
61
62 func (e *overflowError) FormatError(p Printer) error {
63 var pp printer.Printer
64
65 var colored, inclSource bool
66 if fep, ok := p.(*FormatErrorPrinter); ok {
67 colored = fep.Colored
68 inclSource = fep.InclSource
69 }
70
71 pos := fmt.Sprintf("[%d:%d] ", e.token.Position.Line, e.token.Position.Column)
72 msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, e.Error()), colored)
73 if inclSource {
74 msg += "\n" + pp.PrintErrorToken(e.token, colored)
75 }
76 p.Print(msg)
77
78 return nil
79 }
80
81 type syntaxError struct {
82 msg string
83 token *token.Token
84 }
85
86 func (e *syntaxError) PrettyPrint(p Printer, colored, inclSource bool) error {
87 return e.FormatError(&FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource})
88 }
89
90 func (e *syntaxError) FormatError(p Printer) error {
91 var pp printer.Printer
92
93 var colored, inclSource bool
94 if fep, ok := p.(*FormatErrorPrinter); ok {
95 colored = fep.Colored
96 inclSource = fep.InclSource
97 }
98
99 pos := fmt.Sprintf("[%d:%d] ", e.token.Position.Line, e.token.Position.Column)
100 msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, e.msg), colored)
101 if inclSource {
102 msg += "\n" + pp.PrintErrorToken(e.token, colored)
103 }
104 p.Print(msg)
105 return nil
106 }
107
108 type PrettyPrinter interface {
109 PrettyPrint(Printer, bool, bool) error
110 }
111
112 type Sink struct{ *bytes.Buffer }
113
114 func (es *Sink) Print(args ...interface{}) {
115 fmt.Fprint(es.Buffer, args...)
116 }
117
118 func (es *Sink) Printf(f string, args ...interface{}) {
119 fmt.Fprintf(es.Buffer, f, args...)
120 }
121
122 func (es *Sink) Detail() bool {
123 return false
124 }
125
126 func (e *syntaxError) Error() string {
127 var buf bytes.Buffer
128 e.PrettyPrint(&Sink{&buf}, defaultColorize, defaultIncludeSource)
129 return buf.String()
130 }
131
132 type TypeError struct {
133 DstType reflect.Type
134 SrcType reflect.Type
135 StructFieldName *string
136 Token *token.Token
137 }
138
139 func (e *TypeError) Error() string {
140 if e.StructFieldName != nil {
141 return fmt.Sprintf("cannot unmarshal %s into Go struct field %s of type %s", e.SrcType, *e.StructFieldName, e.DstType)
142 }
143 return fmt.Sprintf("cannot unmarshal %s into Go value of type %s", e.SrcType, e.DstType)
144 }
145
146 func (e *TypeError) PrettyPrint(p Printer, colored, inclSource bool) error {
147 return e.FormatError(&FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource})
148 }
149
150 func (e *TypeError) FormatError(p Printer) error {
151 var pp printer.Printer
152
153 var colored, inclSource bool
154 if fep, ok := p.(*FormatErrorPrinter); ok {
155 colored = fep.Colored
156 inclSource = fep.InclSource
157 }
158
159 pos := fmt.Sprintf("[%d:%d] ", e.Token.Position.Line, e.Token.Position.Column)
160 msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, e.Error()), colored)
161 if inclSource {
162 msg += "\n" + pp.PrintErrorToken(e.Token, colored)
163 }
164 p.Print(msg)
165
166 return nil
167 }