Skip to content

Commit

Permalink
Resolves #26 - Improves error handling so that consumers can return 4…
Browse files Browse the repository at this point in the history
…00 and not 500 on unrecognized operators
  • Loading branch information
steve-r-west committed Nov 29, 2024
1 parent 7e2a907 commit 72e059a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func Example(headerValue string) (*epsearchast_v3.AstNode, error) {

```

If the error that comes back is a ValidationErr you should treat it as a 400 to the caller.



### Aliases

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ services:
- '20003:9200'
environment:
discovery.type: single-node
ES_JAVA_OPTS: "-Xms1024m -Xmx1024m"
healthcheck:
test: [ "CMD-SHELL", "curl -f http://localhost:9200/_cat/health" ]
interval: 10s
Expand Down
2 changes: 1 addition & 1 deletion external/epsearchast/v3/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GetAst(jsonTxt string) (*AstNode, error) {
// we receive something that doesn't make any sense like ge(a). The main argument case where we should
// treat this as a validation is an unknown operator. However, in theory the upstream generator
// passing us something likely means we should treat it as unsupported if we are out of date.
return nil, NewValidationErr(fmt.Errorf("error validating filter (%s) :%w", astNode.AsFilter(), err))
return nil, NewValidationErr(fmt.Errorf("(%s): %w", astNode.AsFilter(), err))
} else {
return astNode, nil
}
Expand Down
29 changes: 28 additions & 1 deletion external/epsearchast/v3/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,34 @@ func TestInvalidObjectReturnsError(t *testing.T) {

// Verify
require.Error(t, err)
require.EqualError(t, err, "error validating filter: unsupported operator foo()")
require.EqualError(t, err, "error validating filter: (foo()): unsupported operator foo()")
require.ErrorAs(t, err, &ValidationErr{})
require.Nil(t, astNode)
}

func TestUnrecognizedOperatorWithChildReturnsError(t *testing.T) {
// Fixture Setup

//language=JSON
jsonTxt := `{
"type": "NOT",
"children": [
{
"type": "EQ",
"args": [
"status",
"paid"
]
}
]
}
`
// Execute SUT
astNode, err := GetAst(jsonTxt)

// Verify
require.Error(t, err)
require.EqualError(t, err, "error validating filter: (not()): unsupported operator not()")
require.ErrorAs(t, err, &ValidationErr{})
require.Nil(t, astNode)
}
Expand Down
8 changes: 7 additions & 1 deletion external/epsearchast/v3/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@ func ValidateAstFieldAndOperatorsWithAliasesAndValueValidationAndFieldTypes(astN
return err
}

return astNode.Accept(visitor)
err = astNode.Accept(visitor)

if err != nil {
return NewValidationErr(err)
}

return nil
}
2 changes: 1 addition & 1 deletion external/epsearchast/v3/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestValidationReturnsErrorForBinaryOperatorsWhenAstUsesUnknownField(t *test
err = ValidateAstFieldAndOperators(ast, map[string][]string{"other_field": {otherBinOp}, "another_field": {otherBinOp}})

// Verification
require.EqualError(t, err, fmt.Sprintf("unknown field [amount] specified in search filter, allowed fields are [another_field other_field]"))
require.EqualError(t, err, fmt.Sprintf("error validating filter: unknown field [amount] specified in search filter, allowed fields are [another_field other_field]"))
})
}

Expand Down

0 comments on commit 72e059a

Please sign in to comment.