This repository was archived by the owner on Jul 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.go
More file actions
129 lines (112 loc) · 3.33 KB
/
Copy patherror.go
File metadata and controls
129 lines (112 loc) · 3.33 KB
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
package goda
import (
"errors"
"fmt"
)
// ErrUnsupported indicates that the specified operation or field is not supported.
var ErrUnsupported = errors.ErrUnsupported
// ErrOutOfRange indicates that the specified value is out of range.
var ErrOutOfRange = errors.New("out of range")
// ErrArithmeticOverflow indicates that the result of an arithmetic operation overflows.
var ErrArithmeticOverflow = errors.New("arithmetic overflow")
// newError creates a new Error with the given format and arguments.
// All error messages are prefixed with "goda: ".
func newError(format string, a ...any) error {
return &Error{message: fmt.Sprintf(format, a...)}
}
// sqlScannerDefaultBranch creates an error for unsupported SQL scan types.
func sqlScannerDefaultBranch(value any) error {
return newError("cannot scan value of type %T", value)
}
const (
errReasonInvalidField = iota + 1
errReasonUnsupportedField
errReasonOutOfRange
errReasonArithmeticOverflow
errReasonParseFailed
errReasonInvalidZoneId
)
// Error is the error type used by this package.
// It wraps error messages with the "goda: " prefix.
type Error struct {
field Field
int64Value int64
message string
cause error
typeNameId int8
funcNameId int8
reason int8
}
// Error implements the error interface.
func (e Error) Error() string {
var text string
var cause = e.cause
switch e.reason {
case errReasonInvalidField:
text = fmt.Sprintf("goda: invalid field (value=%d)", int64(e.field))
case errReasonUnsupportedField:
text = fmt.Sprintf("goda: unsupported field %s", e.field)
case errReasonOutOfRange:
fr := e.field.fieldRange()
text = fmt.Sprintf("goda: invalid value of %s (valid range %d - %d): %d", e.field, fr.Min, fr.Max, e.int64Value)
case errReasonArithmeticOverflow:
text = "goda: arithmetic overflow"
case errReasonParseFailed:
text = "goda: parse user input failed"
if cause != nil {
text += ", " + cause.Error()
cause = nil
}
case errReasonInvalidZoneId:
text = "goda: invalid zone id"
default:
text = "goda: " + e.message
}
if e.typeNameId != 0 {
text += " at " + tyNames[e.typeNameId] + "/" + fnNames[e.funcNameId]
}
if cause != nil {
text += ", caused by: " + cause.Error()
}
return text
}
func (e Error) Unwrap() error {
if e.cause == nil {
switch e.reason {
case errReasonOutOfRange:
return ErrOutOfRange
case errReasonArithmeticOverflow:
return ErrArithmeticOverflow
case errReasonUnsupportedField:
return ErrUnsupported
}
}
return e.cause
}
func overflowError() error {
return &Error{reason: errReasonArithmeticOverflow}
}
func fieldOutOfRangeError(field Field, value int64) error {
return &Error{reason: errReasonOutOfRange, field: field, int64Value: value}
}
func unsupportedField(field Field) error {
return &Error{reason: errReasonUnsupportedField, field: field}
}
func invalidFieldError(field Field) error {
return &Error{reason: errReasonInvalidField, field: field}
}
func parseFailedError(userInput []byte) error {
return &Error{reason: errReasonParseFailed}
}
func parseFailedErrorWithCause(userInput []byte, cause error) error {
return &Error{reason: errReasonParseFailed, cause: cause}
}
func deferOpInParse(userInput []byte, e *error) {
if *e == nil {
return
}
//goland:noinspection GoTypeAssertionOnErrors
if _, ok := (*e).(*Error); !ok {
*e = parseFailedErrorWithCause(userInput, *e)
}
}