Skip to content

Commit

Permalink
feat: add ParseError (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Jens Neuse <jens.neuse@gmx.de>
  • Loading branch information
alepane21 and jensneuse authored Dec 10, 2024
1 parent 8afac53 commit 15ca0ac
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ import (
"github.com/wundergraph/astjson/fastfloat"
)

type ParseError struct {
Err error
}

func (p *ParseError) Error() string {
if p == nil {
return ""
}
return p.Err.Error()
}

func NewParseError(err error) *ParseError {
if err == nil {
return nil
}

return &ParseError{Err: err}
}

// Parser parses JSON.
//
// Parser may be re-used for subsequent parsing.
Expand Down Expand Up @@ -38,11 +57,11 @@ func (p *Parser) Parse(s string) (*Value, error) {

v, tail, err := parseValue(b2s(p.b), p.c, 0)
if err != nil {
return nil, fmt.Errorf("cannot parse JSON: %s; unparsed tail: %q", err, startEndString(tail))
return nil, NewParseError(fmt.Errorf("cannot parse JSON: %s; unparsed tail: %q", err, startEndString(tail)))
}
tail = skipWS(tail)
if len(tail) > 0 {
return nil, fmt.Errorf("unexpected tail: %q", startEndString(tail))
return nil, NewParseError(fmt.Errorf("unexpected tail: %q", startEndString(tail)))
}
return v, nil
}
Expand All @@ -54,11 +73,11 @@ func (p *Parser) ParseWithoutCache(s string) (*Value, error) {

v, tail, err := parseValue(b2s(p.b), p.c, 0)
if err != nil {
return nil, fmt.Errorf("cannot parse JSON: %s; unparsed tail: %q", err, startEndString(tail))
return nil, NewParseError(fmt.Errorf("cannot parse JSON: %s; unparsed tail: %q", err, startEndString(tail)))
}
tail = skipWS(tail)
if len(tail) > 0 {
return nil, fmt.Errorf("unexpected tail: %q", startEndString(tail))
return nil, NewParseError(fmt.Errorf("unexpected tail: %q", startEndString(tail)))
}
return v, nil
}
Expand Down

0 comments on commit 15ca0ac

Please sign in to comment.