Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure QueryError always has a code and use syntax as code for all parser errors #1260

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading