Skip to content

Cannot get Error from callback #230

Description

@yb3616

if e != nil {

Activity

  1. cozymore commented on Jun 18, 2021

    @cozymore

    +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)
  2. added this to the v2 milestone on Jun 18, 2021
  3. buger commented on Jun 18, 2021

    @buger
    Owner

    @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

    jsonparser/parser.go

    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.

  4. cozymore commented on Jun 22, 2021

    @cozymore

    @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.

  5. jmo-qap commented on Oct 17, 2022

    @jmo-qap

    Agree with @PiotrKozimor recommendation, I would like that behavior and that would bring it inline with ObjectEach, I think.

  6. buger commented on Jul 28, 2026

    @buger
    Owner

    Implemented in v1.4.0 — EachKeyErr and ArrayEachErr both support error-returning callbacks. See: https://github.com/buger/jsonparser/releases/tag/v1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions