Merge pull request #1930 from molecula/fb1207

[FB-1207] catch the panic we throw for an invalid timestamp
This commit is contained in:
seebs 2022-02-23 10:11:02 -06:00 committed by GitHub
commit 94cfecf0d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View file

@ -15,7 +15,7 @@ import (
// error strings in the parser
const duplicateArgErrorMessage = "duplicate argument provided"
const intOutOfRangeError = "integer is not in signed 64-bit range"
const invalidTimestampError = "string is not a timestamp"
const invalidTimestampError = "string is not a valid timestamp"
// parser represents a parser for the PQL language.
type parser struct {
@ -66,7 +66,7 @@ func (p *parser) Parse() (*Query, error) {
if !ok {
return nil, fmt.Errorf("unexpected parser error of type %T: %[1]v", v)
}
if strings.HasPrefix(errorMessage, duplicateArgErrorMessage) || strings.HasPrefix(errorMessage, intOutOfRangeError) {
if strings.HasPrefix(errorMessage, duplicateArgErrorMessage) || strings.HasPrefix(errorMessage, intOutOfRangeError) || strings.HasPrefix(errorMessage, invalidTimestampError) {
return nil, fmt.Errorf("%s", v)
} else {
panic(v)

View file

@ -5,6 +5,7 @@ import (
"reflect"
"strings"
"testing"
"time"
"github.com/molecula/featurebase/v3/pql"
_ "github.com/molecula/featurebase/v3/test"
@ -197,6 +198,33 @@ func TestParser_Parse(t *testing.T) {
}
})
t.Run("Timestamp", func(t *testing.T) {
twos := "2022-02-22T22:22:22Z"
date, err := time.Parse(time.RFC3339, twos)
if err != nil {
t.Fatal(err)
}
q, err := pql.ParseString(`Row(x>'2022-02-22T22:22:22Z')`)
if err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(q.Calls[0],
&pql.Call{
Name: "Row",
Args: map[string]interface{}{
"x": &pql.Condition{Op: pql.GT, Value: date},
},
},
) {
t.Fatalf("unexpected call: %#v", q.Calls[0])
}
q, err = pql.ParseString(`Row(x>'2024-04-24T24:24:24Z')`)
if err == nil {
t.Fatal("no error parsing invalid date")
} else if !strings.Contains(err.Error(), "not a valid timestamp") {
t.Fatalf("expected error for invalid timestamp, got: %s", err.Error())
}
})
t.Run("VariousSpaces", func(t *testing.T) {
q, err := pql.ParseString(`TopN( x )`)
if err != nil {