Skip to content

Commit

Permalink
feat: expose syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nieomylnieja committed Feb 1, 2024
1 parent 31fe1ba commit 9eb62dc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
29 changes: 29 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package yaml

import (
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/internal/errors"
"github.com/goccy/go-yaml/token"
"golang.org/x/xerrors"
)

Expand Down Expand Up @@ -60,3 +62,30 @@ func IsInvalidAnchorNameError(err error) bool {
func IsInvalidAliasNameError(err error) bool {
return xerrors.Is(err, ast.ErrInvalidAliasName)
}

// SyntaxError represents an syntax error associated with a specific [token.Token].
type SyntaxError struct {
Msg string
Token *token.Token

err *errors.SyntaxError
}

// Error implements the error interface.
func (s SyntaxError) Error() string {
return s.err.Error()
}

// AsSyntaxError checks if error was a syntax error and returns it if so.
// Otherwise, it returns nil.
func AsSyntaxError(err error) *SyntaxError {
var syntaxError *errors.SyntaxError
if xerrors.As(err, &syntaxError) {
return &SyntaxError{
Msg: syntaxError.GetMessage(),
Token: syntaxError.GetToken(),
err: syntaxError,
}
}
return nil
}
22 changes: 15 additions & 7 deletions internal/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func Wrapf(err error, msg string, args ...interface{}) error {
}

// ErrSyntax create syntax error instance with message and token
func ErrSyntax(msg string, tk *token.Token) *syntaxError {
return &syntaxError{
func ErrSyntax(msg string, tk *token.Token) *SyntaxError {
return &SyntaxError{
baseError: &baseError{},
msg: msg,
token: tk,
Expand All @@ -54,7 +54,7 @@ func (e *baseError) chainStateAndVerb(err error) {
wrapErr.state = e.state
wrapErr.verb = e.verb
}
syntaxErr, ok := err.(*syntaxError)
syntaxErr, ok := err.(*SyntaxError)
if ok {
syntaxErr.state = e.state
syntaxErr.verb = e.verb
Expand Down Expand Up @@ -164,18 +164,18 @@ func (e *wrapError) Error() string {
return buf.String()
}

type syntaxError struct {
type SyntaxError struct {
*baseError
msg string
token *token.Token
frame xerrors.Frame
}

func (e *syntaxError) PrettyPrint(p xerrors.Printer, colored, inclSource bool) error {
func (e *SyntaxError) PrettyPrint(p xerrors.Printer, colored, inclSource bool) error {
return e.FormatError(&FormatErrorPrinter{Printer: p, Colored: colored, InclSource: inclSource})
}

func (e *syntaxError) FormatError(p xerrors.Printer) error {
func (e *SyntaxError) FormatError(p xerrors.Printer) error {
var pp printer.Printer

var colored, inclSource bool
Expand All @@ -199,6 +199,14 @@ func (e *syntaxError) FormatError(p xerrors.Printer) error {
return nil
}

func (e *SyntaxError) GetToken() *token.Token {
return e.token
}

func (e *SyntaxError) GetMessage() string {
return e.msg
}

type PrettyPrinter interface {
PrettyPrint(xerrors.Printer, bool, bool) error
}
Expand All @@ -216,7 +224,7 @@ func (es *Sink) Detail() bool {
return false
}

func (e *syntaxError) Error() string {
func (e *SyntaxError) Error() string {
var buf bytes.Buffer
e.PrettyPrint(&Sink{&buf}, defaultColorize, defaultIncludeSource)
return buf.String()
Expand Down

0 comments on commit 9eb62dc

Please sign in to comment.