-
Notifications
You must be signed in to change notification settings - Fork 6
/
error.go
276 lines (234 loc) · 5.59 KB
/
error.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package tezerrors
import (
"bytes"
"database/sql/driver"
"errors"
"fmt"
"strings"
stdJSON "encoding/json"
jsoniter "github.com/json-iterator/go"
"github.com/dipdup-net/go-lib/tools/ast"
"github.com/dipdup-net/go-lib/tools/consts"
"github.com/dipdup-net/go-lib/tools/forge"
"github.com/dipdup-net/go-lib/tools/formatter"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// Errors -
type Errors []*Error
// Scan scan value into Jsonb, implements sql.Scanner interface
func (e *Errors) Scan(value interface{}) error {
if value == nil {
arr := make([]*Error, 0)
*e = Errors(arr)
return nil
}
bytes, ok := value.([]byte)
if !ok {
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
}
return json.Unmarshal(bytes, e)
}
// Value return json value, implement driver.Valuer interface
func (e Errors) Value() (driver.Value, error) {
if e == nil {
return nil, nil
}
return json.Marshal(e)
}
// ParseArray -
func ParseArray(data []byte) ([]*Error, error) {
if len(data) == 0 {
return nil, nil
}
ret := make([]*Error, 0)
err := json.Unmarshal(data, &ret)
return ret, err
}
func getErrorID(id string) string {
parts := strings.Split(id, ".")
if len(parts) > 1 {
parts = parts[2:]
}
return strings.Join(parts, ".")
}
// IError -
type IError interface {
fmt.Stringer
Format() error
}
// Error -
type Error struct {
ID string `json:"id"`
Kind string `json:"kind"`
Title string `json:"title,omitempty"`
Description string `json:"descr,omitempty"`
IError `json:"-"`
}
// GetTitle -
func (e *Error) GetTitle() string {
return e.Title
}
// Is -
func (e *Error) Is(errorID string) bool {
return strings.Contains(e.ID, errorID)
}
// Format -
func (e *Error) Format() error {
if e.IError == nil {
return nil
}
return e.IError.Format()
}
// String -
func (e *Error) String() string {
return e.ID
}
// UnmarshalJSON -
func (e *Error) UnmarshalJSON(data []byte) error {
var typ struct {
ID string `json:"id"`
Kind string `json:"kind"`
}
if err := json.Unmarshal(data, &typ); err != nil {
return err
}
e.ID = typ.ID
e.Kind = typ.Kind
errorID := getErrorID(e.ID)
var descr Description
var ok bool
if descr, ok = errorDescriptions[errorID]; !ok {
if err := json.Unmarshal(data, &descr); err != nil {
return err
}
}
e.Title = descr.Title
e.Description = descr.Description
switch {
case strings.Contains(e.ID, consts.BalanceTooLowError):
e.IError = new(BalanceTooLowError)
case strings.Contains(e.ID, consts.InvalidSyntacticConstantError):
e.IError = new(InvalidSyntacticConstantError)
default:
e.IError = new(DefaultError)
}
return json.Unmarshal(data, e.IError)
}
// MarshalJSON -
func (e *Error) MarshalJSON() ([]byte, error) {
type eBuf *Error
data, err := json.Marshal(eBuf(e))
if err != nil {
return nil, err
}
data = data[:len(data)-1]
w := bytes.NewBuffer(data)
body, err := json.Marshal(e.IError)
if err != nil {
return nil, err
}
if len(body) > 2 {
body = body[1:]
w.WriteString(", ")
w.Write(body)
} else {
w.WriteByte('}')
}
return w.Bytes(), nil
}
// DefaultError -
type DefaultError struct {
Location int64 `json:"location,omitempty"`
With stdJSON.RawMessage `json:"with,omitempty"`
}
// Format -
func (e *DefaultError) Format() error {
if e.With == nil {
return nil
}
var tree ast.UntypedAST
if err := json.Unmarshal(e.With, &tree); err != nil {
return err
}
if len(tree) == 0 {
return nil
}
text := string(e.With)
if tree[0].BytesValue != nil {
subTree, err := forge.UnpackString(*tree[0].BytesValue)
if err == nil {
text, _ = json.MarshalToString(subTree)
}
}
if text != "" {
errString, err := formatter.MichelineStringToMichelson(text, true, formatter.DefLineSize)
if err != nil {
return err
}
e.With = []byte(errString)
}
return nil
}
// String -
func (e *DefaultError) String() string {
return string(e.With)
}
// BalanceTooLowError -
type BalanceTooLowError struct {
DefaultError
Balance int64 `json:"balance,string"`
Amount int64 `json:"amount,string"`
}
// Format -
func (e *BalanceTooLowError) Format() error {
return nil
}
// String -
func (e *BalanceTooLowError) String() string {
return fmt.Sprintf("Balance too low: %d < %d", e.Balance, e.Amount)
}
// InvalidSyntacticConstantError -
type InvalidSyntacticConstantError struct {
WrongExpressionSnake stdJSON.RawMessage `json:"wrong_expression"`
ExpectedFormSnake stdJSON.RawMessage `json:"expected_form"`
WrongExpressionCamel stdJSON.RawMessage `json:"wrongExpression"`
ExpectedFormCamel stdJSON.RawMessage `json:"expectedForm"`
}
func (e *InvalidSyntacticConstantError) getWrongExpression() []byte {
wrongExpr := e.ExpectedFormCamel
if wrongExpr == nil {
wrongExpr = e.WrongExpressionCamel
}
return wrongExpr
}
func (e *InvalidSyntacticConstantError) getExpectedForm() []byte {
expForm := e.ExpectedFormCamel
if expForm == nil {
expForm = e.ExpectedFormSnake
}
return expForm
}
// String -
func (e *InvalidSyntacticConstantError) String() string {
return string(e.getWrongExpression())
}
// Format -
func (e *InvalidSyntacticConstantError) Format() error {
wrongExpr := e.getWrongExpression()
if wrongExpr != nil {
wrongExpression, err := formatter.MichelineStringToMichelson(string(wrongExpr), false, formatter.DefLineSize)
if err != nil {
return err
}
e.WrongExpressionCamel = []byte(wrongExpression)
}
expForm := e.getExpectedForm()
if expForm != nil {
expectedForm, err := formatter.MichelineStringToMichelson(string(expForm), false, formatter.DefLineSize)
if err != nil {
return err
}
e.ExpectedFormCamel = []byte(expectedForm)
}
return nil
}