-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parser): wrap errors with object location
- Loading branch information
Showing
10 changed files
with
105 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package parser | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-faster/errors" | ||
|
||
ogenjson "github.com/ogen-go/ogen/json" | ||
) | ||
|
||
var _ interface { | ||
error | ||
errors.Wrapper | ||
errors.Formatter | ||
} = (*LocationError)(nil) | ||
|
||
// LocationError is a wrapper for an error that has a location. | ||
type LocationError struct { | ||
loc ogenjson.Location | ||
err error | ||
} | ||
|
||
// Unwrap implements errors.Wrapper. | ||
func (e *LocationError) Unwrap() error { | ||
return e.err | ||
} | ||
|
||
// FormatError implements errors.Formatter. | ||
func (e *LocationError) FormatError(p errors.Printer) (next error) { | ||
p.Printf("at %s", e.loc) | ||
return e.err | ||
} | ||
|
||
// Error implements error. | ||
func (e *LocationError) Error() string { | ||
return fmt.Sprintf("at %s: %s", e.loc, e.err) | ||
} | ||
|
||
func (p *parser) wrapLocation(l ogenjson.Locatable, err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
loc, ok := l.Location() | ||
if !ok { | ||
return err | ||
} | ||
return &LocationError{ | ||
loc: loc, | ||
err: err, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters