From 19807210bdff3c8312dda87a1e0dad608eb3559b Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Wed, 22 May 2024 11:21:50 -0500 Subject: [PATCH] Ensure QueryError always has a code and use `syntax` as code for all parser errors --- contactql/error.go | 6 +----- contactql/parser.go | 11 +---------- contactql/parser_test.go | 22 +++++++++++----------- utils/errors.go | 8 -------- 4 files changed, 13 insertions(+), 34 deletions(-) delete mode 100644 utils/errors.go diff --git a/contactql/error.go b/contactql/error.go index ffd65aaa4..2b6413755 100644 --- a/contactql/error.go +++ b/contactql/error.go @@ -3,13 +3,11 @@ package contactql import ( "errors" "fmt" - - "github.com/nyaruka/goflow/utils" ) // error codes with values included in extra const ( - ErrUnexpectedToken = "unexpected_token" // `token` the unexpected token + ErrSyntax = "syntax" ErrInvalidNumber = "invalid_number" // `value` the value we tried to parse as a number ErrInvalidDate = "invalid_date" // `value` the value we tried to parse as a date ErrInvalidStatus = "invalid_status" // `value` the value we tried to parse as a contact status @@ -66,5 +64,3 @@ func IsQueryError(err error) (bool, error) { var qErr *QueryError return errors.As(err, &qErr), qErr } - -var _ utils.RichError = (*QueryError)(nil) diff --git a/contactql/parser.go b/contactql/parser.go index 71f172020..25e1691b7 100644 --- a/contactql/parser.go +++ b/contactql/parser.go @@ -405,16 +405,7 @@ func (l *errorListener) Error() error { } func (l *errorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol any, line, column int, msg string, e antlr.RecognitionException) { - var err *QueryError - switch typed := e.(type) { - case *antlr.InputMisMatchException: - token := typed.GetOffendingToken().GetText() - err = NewQueryError(ErrUnexpectedToken, msg).withExtra("token", token) - default: - err = NewQueryError("", msg) - } - - l.errs = append(l.errs, err) + l.errs = append(l.errs, NewQueryError(ErrSyntax, msg)) } func tokenizeNameValue(value string) []string { diff --git a/contactql/parser_test.go b/contactql/parser_test.go index ff2a1b45e..b85cbcc85 100644 --- a/contactql/parser_test.go +++ b/contactql/parser_test.go @@ -283,38 +283,38 @@ func TestParsingErrors(t *testing.T) { { query: `$`, errMsg: "mismatched input '$' expecting {'(', STRING, PROPERTY, TEXT}", - errCode: "unexpected_token", - errExtra: map[string]any{"token": "$"}, + errCode: "syntax", + errExtra: nil, }, { query: `name = `, errMsg: "mismatched input '' expecting {STRING, PROPERTY, TEXT}", - errCode: "unexpected_token", - errExtra: map[string]any{"token": ""}, + errCode: "syntax", + errExtra: nil, }, { query: `name = "x`, errMsg: "extraneous input '\"' expecting {STRING, PROPERTY, TEXT}", - errCode: "", + errCode: "syntax", errExtra: nil, }, { query: `nam/e = "x"`, errMsg: "mismatched input '=' expecting ", // because .name isn't valid NAME - errCode: "unexpected_token", - errExtra: map[string]any{"token": "="}, + errCode: "syntax", + errExtra: nil, }, { query: `name. = "x"`, errMsg: "mismatched input '=' expecting ", - errCode: "unexpected_token", - errExtra: map[string]any{"token": "="}, + errCode: "syntax", + errExtra: nil, }, { query: `.name != "x"`, errMsg: "mismatched input '!=' expecting ", - errCode: "unexpected_token", - errExtra: map[string]any{"token": "!="}, + errCode: "syntax", + errExtra: nil, }, { query: `age = XZ`, diff --git a/utils/errors.go b/utils/errors.go deleted file mode 100644 index 5338e4b27..000000000 --- a/utils/errors.go +++ /dev/null @@ -1,8 +0,0 @@ -package utils - -// RichError is a common interface for error types that can provide more detail -type RichError interface { - error - Code() string - Extra() map[string]any -}