Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide access to original error from json.Unmarshal #83

Closed
aakhan1 opened this issue Sep 15, 2020 · 3 comments
Closed

Provide access to original error from json.Unmarshal #83

aakhan1 opened this issue Sep 15, 2020 · 3 comments

Comments

@aakhan1
Copy link
Contributor

aakhan1 commented Sep 15, 2020

What feature or capability would you like?

I would like to gain access the Offset property when a json syntax error occurs so I can report the line and column of the error.

schemaErrors, err := rs.ValidateBytes(ctx, document)
if err!=nil {
	syntaxError := err.(*json.SyntaxError)
	fmt.Println("The offset is:", syntaxError.Offset)
}

Unfortunately, this results in a panic because the rs.ValidateBytes returns a *errors.errorString
panic: interface conversion: error is *errors.errorString, not *json.SyntaxError

Do you have a proposed solution?

Would it be possible to change the implementation of rs.ValidateBytes to return the error as a wrapped error?

Change the %s to %w? Then I can unwrap the error and get what I need.

func (s *Schema) ValidateBytes(ctx context.Context, data []byte) ([]KeyError, error) {
	var doc interface{}
	if err := json.Unmarshal(data, &doc); err != nil {
		return nil, fmt.Errorf("error parsing JSON bytes: %w", err) // <--- change from %s to %w
	}
	vs := s.Validate(ctx, doc)
	return *vs.Errs, nil
}

Or provide a method on the error object where the original error can be accessed.

Have you considered any alternative solutions or workarounds?

I have implemented my own json.Unmarshal call before calling rs.ValidateBytes, but I feel this adds a performance delay unnecessarily.

@aakhan1
Copy link
Contributor Author

aakhan1 commented Oct 25, 2020

@Arqu I'd like to work on this if possible.

@Arqu
Copy link
Contributor

Arqu commented Oct 26, 2020

@aakhan1 Absolutely appreciated, as you see time is tight, and help like this really polishes some of the rough edges. A PR is more than welcome. Happy hacktober :)

aakhan1 added a commit to aakhan1/jsonschema that referenced this issue Oct 27, 2020
…apped error (qri-io#83)

this allows the caller get to the underlying error
Arqu pushed a commit that referenced this issue Oct 28, 2020
…apped error (#83) (#85)

this allows the caller get to the underlying error
@Arqu
Copy link
Contributor

Arqu commented Oct 28, 2020

Done in #85

@Arqu Arqu closed this as completed Oct 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants