Skip to content

Commit

Permalink
Merge pull request #1260 from nyaruka/simpler_syntax_errors
Browse files Browse the repository at this point in the history
Ensure `QueryError` always has a code and use `syntax` as code for all parser errors
  • Loading branch information
rowanseymour authored May 22, 2024
2 parents 632cfd8 + 1980721 commit 3ddb7c9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 34 deletions.
6 changes: 1 addition & 5 deletions contactql/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,5 +64,3 @@ func IsQueryError(err error) (bool, error) {
var qErr *QueryError
return errors.As(err, &qErr), qErr
}

var _ utils.RichError = (*QueryError)(nil)
11 changes: 1 addition & 10 deletions contactql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions contactql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<EOF>' expecting {STRING, PROPERTY, TEXT}",
errCode: "unexpected_token",
errExtra: map[string]any{"token": "<EOF>"},
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 <EOF>", // 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 <EOF>",
errCode: "unexpected_token",
errExtra: map[string]any{"token": "="},
errCode: "syntax",
errExtra: nil,
},
{
query: `.name != "x"`,
errMsg: "mismatched input '!=' expecting <EOF>",
errCode: "unexpected_token",
errExtra: map[string]any{"token": "!="},
errCode: "syntax",
errExtra: nil,
},
{
query: `age = XZ`,
Expand Down
8 changes: 0 additions & 8 deletions utils/errors.go

This file was deleted.

0 comments on commit 3ddb7c9

Please sign in to comment.