-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
145 lines (118 loc) · 4.44 KB
/
errors.go
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
package isoduration
import (
"errors"
"fmt"
"reflect"
)
// is checks type matching
var is = func(err, target error) bool {
if err == nil {
return false
}
if reflect.TypeOf(err) != reflect.TypeOf(target) {
return false
}
return reflect.DeepEqual(err, target)
}
// IsNotIsoFormatError occurs when parsing a string in ISO 8601 duration format, the error cannot be clearly identified
var IsNotIsoFormatError = errors.New("incorrect ISO 8601 duration format")
// TimeIsEmptyError occurs when a time designator is found in a line, but its value is not found
// For example: P10YT
var TimeIsEmptyError = errors.New("incorrect ISO 8601 T duration format, designator T found, but value is empty")
// PeriodIsEmptyError occurs when a period designator is found in a line, but its value is not found
// For example: P
var PeriodIsEmptyError = errors.New("incorrect ISO 8601 P duration format, designator P found, but value is empty")
// IncorrectIsoFormatError occurs when a token is found in a string that cannot be converted to the float64 type
// For example: P10,5Y
type IncorrectIsoFormatError struct {
text string
in string
}
// Error defines error output
func (i *IncorrectIsoFormatError) Error() string {
return fmt.Sprintf(i.text, i.in)
}
// Is checks for object matching
func (i *IncorrectIsoFormatError) Is(err error) bool {
return is(i, err)
}
// NewIncorrectIsoFormatError creates new IncorrectIsoFormatError
func NewIncorrectIsoFormatError(in string) *IncorrectIsoFormatError {
return &IncorrectIsoFormatError{"incorrect ISO 8601 duration format, invalid tokens %s", in}
}
// IncorrectDesignatorError occurs when an unknown designator is encountered in the line.
// For example: PT10Y or P1V
type IncorrectDesignatorError struct {
text string
designator rune
state rune
}
// Error defines error output
func (i *IncorrectDesignatorError) Error() string {
return fmt.Sprintf(i.text, i.state, i.designator)
}
// Is checks for object matching
func (i *IncorrectDesignatorError) Is(err error) bool {
return is(i, err)
}
// NewIncorrectDesignatorError creates new IncorrectDesignatorError
func NewIncorrectDesignatorError(state, designator rune) *IncorrectDesignatorError {
return &IncorrectDesignatorError{"incorrect ISO 8601 duration %c format, invalid designator %c", designator, state}
}
// DesignatorNotFoundError occurs when there is no designator in the line after the token
// For example P10T10H.
type DesignatorNotFoundError struct {
text string
state rune
after string
}
// Error defines error output
func (i *DesignatorNotFoundError) Error() string {
return fmt.Sprintf(i.text, i.state, i.after)
}
// Is checks for object matching
func (i *DesignatorNotFoundError) Is(err error) bool {
return is(i, err)
}
// NewDesignatorNotFoundError creates new DesignatorNotFoundError
func NewDesignatorNotFoundError(state rune, after string) *DesignatorNotFoundError {
return &DesignatorNotFoundError{"incorrect ISO 8601 duration %c format, designator not found after token %s", state, after}
}
// DesignatorValueNotFoundError occurs when no value is found for the designator
// For example: PT1HM
type DesignatorValueNotFoundError struct {
text string
state rune
designator rune
}
// Error defines error output
func (i *DesignatorValueNotFoundError) Error() string {
return fmt.Sprintf(i.text, i.state, i.designator)
}
// Is checks for object matching
func (i *DesignatorValueNotFoundError) Is(err error) bool {
return is(i, err)
}
// NewDesignatorValueNotFoundError creates new DesignatorValueNotFoundError
func NewDesignatorValueNotFoundError(state, designator rune) *DesignatorValueNotFoundError {
return &DesignatorValueNotFoundError{"incorrect ISO 8601 duration %c format, %c designator's value not found", state, designator}
}
// DesignatorMetError occurs when this designator has already been processed previously.
// Affect: if you write string like PT10H0M10M it defines value as 10
// For example: P1Y5Y
type DesignatorMetError struct {
text string
designator rune
}
// Error defines error output
func (i *DesignatorMetError) Error() string {
return fmt.Sprintf(i.text, i.designator)
}
// Is checks for object matching
func (i *DesignatorMetError) Is(err error) bool {
return is(i, err)
}
// NewDesignatorMetError creates new DesignatorMetError
func NewDesignatorMetError(designator rune) *DesignatorMetError {
return &DesignatorMetError{"incorrect ISO 8601 duration format, the designator %c has already been processed", designator}
}