Repository navigation
Cannot get Error from callback #230
Description
Activity
+1, for now error passed to callback will be always nil and break will never occur. I think the right api would be:
func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int) error, keys ...string) (offset int, err error)
Reacted by Muhammad Yahya Muhaimin and Mendel Greenberg@PiotrKozimor you are right that callback at the moment never receive the error. But I guess proper way to handle it will be smth like:
Instead of
Lines 1028 to 1030 in 09bcf22
if e != nil { return offset, e } Do
if err != nil { cb(nil, t, offset+o-len(v), e) return offset, e }Well it does not make TOO much sense from API point of view, since it always returns on the first error. But at least it does not break API (I really care about backward compatibility).
I've set v2 milestone, so when we do next major release, we can "remove" err object from callback or fix it in another way.
@buger It would make sense as a temporary, compatible fix.
Going back to my breaking suggestion - when error is returned from callback, iterating over array would be stopped and this error would be returned from
ArrayEach. That's how I see it. Fail fast could bring benefit in some use cases.Reacted by Ozan Hacıbekiroğlu, Muhammad Yahya Muhaimin and Mendel GreenbergAgree with @PiotrKozimor recommendation, I would like that behavior and that would bring it inline with ObjectEach, I think.
- added a commit that references this issue
on Jul 28, 2026 Implemented in v1.4.0 —
EachKeyErrandArrayEachErrboth support error-returning callbacks. See: https://github.com/buger/jsonparser/releases/tag/v1.4.0
jsonparser/parser.go
Line 1040 in 09bcf22