Skip to content

Commit 357d7c1

Browse files
committed
Merge branch 'better-error-handling' of https://github.com/matijavizintin/jsonparser into matijavizintin-better-error-handling
2 parents 5c8d867 + 1d3fc5a commit 357d7c1

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 {
@@ -186,7 +191,7 @@ If no keys provided it will try to extract closest JSON value (simple ones or ob
186191
func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset int, err error) {
187192
if len(keys) > 0 {
188193
if offset = searchKeys(data, keys...); offset == -1 {
189-
return []byte{}, NotExist, -1, errors.New("Key path not found")
194+
return []byte{}, NotExist, -1, KeyPathNotFoundError
190195
}
191196
}
192197

@@ -243,18 +248,18 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
243248
if bytes.Equal(value, trueLiteral) || bytes.Equal(value, falseLiteral) {
244249
dataType = Boolean
245250
} else {
246-
return nil, Unknown, offset, errors.New("Unknown value type")
251+
return nil, Unknown, offset, UnknownValueTypeError
247252
}
248253
case 'u', 'n': // undefined or null
249254
if bytes.Equal(value, nullLiteral) {
250255
dataType = Null
251256
} else {
252-
return nil, Unknown, offset, errors.New("Unknown value type")
257+
return nil, Unknown, offset, UnknownValueTypeError
253258
}
254259
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
255260
dataType = Number
256261
default:
257-
return nil, Unknown, offset, errors.New("Unknown value type")
262+
return nil, Unknown, offset, UnknownValueTypeError
258263
}
259264

260265
endOffset += end
@@ -284,7 +289,7 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
284289

285290
if len(keys) > 0 {
286291
if offset = searchKeys(data, keys...); offset == -1 {
287-
return errors.New("Key path not found")
292+
return KeyPathNotFoundError
288293
}
289294

290295
// 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 ValueT
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)