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
|
package errors
import (
"errors"
"fmt"
"reflect"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/printer"
"github.com/goccy/go-yaml/token"
)
var (
As = errors.As
Is = errors.Is
New = errors.New
)
const (
defaultFormatColor = false
defaultIncludeSource = true
)
type Error interface {
error
GetToken() *token.Token
GetMessage() string
FormatError(bool, bool) string
}
var (
_ Error = new(SyntaxError)
_ Error = new(TypeError)
_ Error = new(OverflowError)
_ Error = new(DuplicateKeyError)
_ Error = new(UnknownFieldError)
_ Error = new(UnexpectedNodeTypeError)
)
type SyntaxError struct {
Message string
Token *token.Token
}
type TypeError struct {
DstType reflect.Type
SrcType reflect.Type
StructFieldName *string
Token *token.Token
}
type OverflowError struct {
DstType reflect.Type
SrcNum string
Token *token.Token
}
type DuplicateKeyError struct {
Message string
Token *token.Token
}
type UnknownFieldError struct {
Message string
Token *token.Token
}
type UnexpectedNodeTypeError struct {
Actual ast.NodeType
Expected ast.NodeType
Token *token.Token
}
// ErrSyntax create syntax error instance with message and token
func ErrSyntax(msg string, tk *token.Token) *SyntaxError {
return &SyntaxError{
Message: msg,
Token: tk,
}
}
// ErrOverflow creates an overflow error instance with message and a token.
func ErrOverflow(dstType reflect.Type, num string, tk *token.Token) *OverflowError {
return &OverflowError{
DstType: dstType,
SrcNum: num,
Token: tk,
}
}
// ErrTypeMismatch cerates an type mismatch error instance with token.
func ErrTypeMismatch(dstType, srcType reflect.Type, token *token.Token) *TypeError {
return &TypeError{
DstType: dstType,
SrcType: srcType,
Token: token,
}
}
// ErrDuplicateKey creates an duplicate key error instance with token.
func ErrDuplicateKey(msg string, tk *token.Token) *DuplicateKeyError {
return &DuplicateKeyError{
Message: msg,
Token: tk,
}
}
// ErrUnknownField creates an unknown field error instance with token.
func ErrUnknownField(msg string, tk *token.Token) *UnknownFieldError {
return &UnknownFieldError{
Message: msg,
Token: tk,
}
}
func ErrUnexpectedNodeType(actual, expected ast.NodeType, tk *token.Token) *UnexpectedNodeTypeError {
return &UnexpectedNodeTypeError{
Actual: actual,
Expected: expected,
Token: tk,
}
}
func (e *SyntaxError) GetMessage() string {
return e.Message
}
func (e *SyntaxError) GetToken() *token.Token {
return e.Token
}
func (e *SyntaxError) Error() string {
return e.FormatError(defaultFormatColor, defaultIncludeSource)
}
func (e *SyntaxError) FormatError(colored, inclSource bool) string {
return FormatError(e.Message, e.Token, colored, inclSource)
}
func (e *OverflowError) GetMessage() string {
return e.msg()
}
func (e *OverflowError) GetToken() *token.Token {
return e.Token
}
func (e *OverflowError) Error() string {
return e.FormatError(defaultFormatColor, defaultIncludeSource)
}
func (e *OverflowError) FormatError(colored, inclSource bool) string {
return FormatError(e.msg(), e.Token, colored, inclSource)
}
func (e *OverflowError) msg() string {
return fmt.Sprintf("cannot unmarshal %s into Go value of type %s ( overflow )", e.SrcNum, e.DstType)
}
func (e *TypeError) msg() 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) GetMessage() string {
return e.msg()
}
func (e *TypeError) GetToken() *token.Token {
return e.Token
}
func (e *TypeError) Error() string {
return e.FormatError(defaultFormatColor, defaultIncludeSource)
}
func (e *TypeError) FormatError(colored, inclSource bool) string {
return FormatError(e.msg(), e.Token, colored, inclSource)
}
func (e *DuplicateKeyError) GetMessage() string {
return e.Message
}
func (e *DuplicateKeyError) GetToken() *token.Token {
return e.Token
}
func (e *DuplicateKeyError) Error() string {
return e.FormatError(defaultFormatColor, defaultIncludeSource)
}
func (e *DuplicateKeyError) FormatError(colored, inclSource bool) string {
return FormatError(e.Message, e.Token, colored, inclSource)
}
func (e *UnknownFieldError) GetMessage() string {
return e.Message
}
func (e *UnknownFieldError) GetToken() *token.Token {
return e.Token
}
func (e *UnknownFieldError) Error() string {
return e.FormatError(defaultFormatColor, defaultIncludeSource)
}
func (e *UnknownFieldError) FormatError(colored, inclSource bool) string {
return FormatError(e.Message, e.Token, colored, inclSource)
}
func (e *UnexpectedNodeTypeError) GetMessage() string {
return e.msg()
}
func (e *UnexpectedNodeTypeError) GetToken() *token.Token {
return e.Token
}
func (e *UnexpectedNodeTypeError) Error() string {
return e.FormatError(defaultFormatColor, defaultIncludeSource)
}
func (e *UnexpectedNodeTypeError) FormatError(colored, inclSource bool) string {
return FormatError(e.msg(), e.Token, colored, inclSource)
}
func (e *UnexpectedNodeTypeError) msg() string {
return fmt.Sprintf("%s was used where %s is expected", e.Actual.YAMLName(), e.Expected.YAMLName())
}
func FormatError(errMsg string, token *token.Token, colored, inclSource bool) string {
var pp printer.Printer
if token == nil {
return pp.PrintErrorMessage(errMsg, colored)
}
pos := fmt.Sprintf("[%d:%d] ", token.Position.Line, token.Position.Column)
msg := pp.PrintErrorMessage(fmt.Sprintf("%s%s", pos, errMsg), colored)
if inclSource {
msg += "\n" + pp.PrintErrorToken(token, colored)
}
return msg
}
|