Skip to content

Commit e130aa9

Browse files
committed
Convert all errors to be constants
1 parent 357d7c1 commit e130aa9

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

parser.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ import (
99
"unsafe"
1010
)
1111

12+
// Errors
1213
var (
1314
KeyPathNotFoundError = errors.New("Key path not found")
1415
UnknownValueTypeError = errors.New("Unknown value type")
16+
MalformedJsonError = errors.New("Malformed JSON error")
17+
MalformedStringError = errors.New("Value is string, but can't find closing '\"' symbol")
18+
MalformedArrayError = errors.New("Value is array, but can't find closing ']' symbol")
19+
MalformedObjectError = errors.New("Value looks like object, but can't find closing '}' symbol")
20+
MalformedValueError = errors.New("Value looks like Number/Boolean/None, but can't find its end: ',' or '}' symbol")
1521
)
1622

1723
func tokenEnd(data []byte) int {
@@ -199,7 +205,7 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
199205
nO := nextToken(data[offset:], false)
200206

201207
if nO == -1 {
202-
return []byte{}, NotExist, -1, errors.New("Malformed JSON error")
208+
return []byte{}, NotExist, -1, MalformedJsonError
203209
}
204210

205211
offset += nO
@@ -211,15 +217,15 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
211217
if idx := stringEnd(data[offset+1:]); idx != -1 {
212218
endOffset += idx + 1
213219
} else {
214-
return []byte{}, dataType, offset, errors.New("Value is string, but can't find closing '\"' symbol")
220+
return []byte{}, dataType, offset, MalformedStringError
215221
}
216222
} else if data[offset] == '[' { // if array value
217223
dataType = Array
218224
// break label, for stopping nested loops
219225
endOffset = blockEnd(data[offset:], '[', ']')
220226

221227
if endOffset == -1 {
222-
return []byte{}, dataType, offset, errors.New("Value is array, but can't find closing ']' symbol")
228+
return []byte{}, dataType, offset, MalformedArrayError
223229
}
224230

225231
endOffset += offset
@@ -229,7 +235,7 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
229235
endOffset = blockEnd(data[offset:], '{', '}')
230236

231237
if endOffset == -1 {
232-
return []byte{}, dataType, offset, errors.New("Value looks like object, but can't find closing '}' symbol")
238+
return []byte{}, dataType, offset, MalformedObjectError
233239
}
234240

235241
endOffset += offset
@@ -238,7 +244,7 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
238244
end := tokenEnd(data[endOffset:])
239245

240246
if end == -1 {
241-
return nil, dataType, offset, errors.New("Value looks like Number/Boolean/None, but can't find its end: ',' or '}' symbol")
247+
return nil, dataType, offset, MalformedValueError
242248
}
243249

244250
value := data[offset : endOffset+end]
@@ -282,7 +288,7 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
282288
// ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
283289
func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (err error) {
284290
if len(data) == 0 {
285-
return errors.New("Object is empty")
291+
return MalformedObjectError
286292
}
287293

288294
offset := 1
@@ -296,13 +302,13 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
296302
nO := nextToken(data[offset:], false)
297303

298304
if nO == -1 {
299-
return errors.New("Malformed JSON")
305+
return MalformedJsonError
300306
}
301307

302308
offset += nO
303309

304310
if data[offset] != '[' {
305-
return errors.New("Value is not array")
311+
return MalformedArrayError
306312
}
307313

308314
offset++

0 commit comments

Comments
 (0)