Skip to content

Commit 1d3fc5a

Browse files
error handling
1 parent c76e4b8 commit 1d3fc5a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

parser.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
"unsafe"
1010
)
1111

12+
var (
13+
KeyPathNotFoundError = errors.New("Key path not found")
14+
UnknownValueTypeError = errors.New("Unknown value type")
15+
)
16+
1217
func tokenEnd(data []byte) int {
1318
for i, c := range data {
1419
switch c {
@@ -182,7 +187,7 @@ If no keys provided it will try to extract closest JSON value (simple ones or ob
182187
func Get(data []byte, keys ...string) (value []byte, dataType int, offset int, err error) {
183188
if len(keys) > 0 {
184189
if offset = searchKeys(data, keys...); offset == -1 {
185-
return []byte{}, NotExist, -1, errors.New("Key path not found")
190+
return []byte{}, NotExist, -1, KeyPathNotFoundError
186191
}
187192
}
188193

@@ -239,18 +244,18 @@ func Get(data []byte, keys ...string) (value []byte, dataType int, offset int, e
239244
if bytes.Equal(value, trueLiteral) || bytes.Equal(value, falseLiteral) {
240245
dataType = Boolean
241246
} else {
242-
return nil, Unknown, offset, errors.New("Unknown value type")
247+
return nil, Unknown, offset, UnknownValueTypeError
243248
}
244249
case 'u', 'n': // undefined or null
245250
if bytes.Equal(value, nullLiteral) {
246251
dataType = Null
247252
} else {
248-
return nil, Unknown, offset, errors.New("Unknown value type")
253+
return nil, Unknown, offset, UnknownValueTypeError
249254
}
250255
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
251256
dataType = Number
252257
default:
253-
return nil, Unknown, offset, errors.New("Unknown value type")
258+
return nil, Unknown, offset, UnknownValueTypeError
254259
}
255260

256261
endOffset += end
@@ -280,7 +285,7 @@ func ArrayEach(data []byte, cb func(value []byte, dataType int, offset int, err
280285

281286
if len(keys) > 0 {
282287
if offset = searchKeys(data, keys...); offset == -1 {
283-
return errors.New("Key path not found")
288+
return KeyPathNotFoundError
284289
}
285290

286291
// Go to closest value

parser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ func checkFoundAndNoError(t *testing.T, testKind string, test Test, jtype int, v
418418
// Else, if the call didn't match the is-found expectation, fail
419419
t.Errorf("%s test '%s' isFound mismatch: expected %t, obtained %t", testKind, test.desc, test.isFound, isFound)
420420
return false
421+
} else if !isFound && err != KeyPathNotFoundError {
422+
// Else, if no value was found and the error is not correct, fail
423+
t.Errorf("%s test '%s' error mismatch: expected %t, obtained %t", testKind, test.desc, KeyPathNotFoundError, err)
424+
return false
421425
} else if !isFound {
422426
// Else, if no value was found, don't fail and don't check the value
423427
return false

0 commit comments

Comments
 (0)