feat(filter): add better error message when passing an invalid filter expression

This commit is contained in:
kolaente 2023-11-21 19:55:29 +01:00
parent 9d3fb6f81d
commit c6b682507a
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 57 additions and 25 deletions

View File

@ -96,6 +96,7 @@ This document describes the different errors Vikunja can return.
| 4021 | 400 | This user is already assigned to that task. |
| 4022 | 400 | The task has a relative reminder which does not specify relative to what. |
| 4023 | 409 | Tried to create a task relation which would create a cycle. |
| 4024 | 400 | The provided filter expression is invalid. |
## Team

View File

@ -1021,7 +1021,7 @@ func (err ErrTaskRelationCycle) Error() string {
}
// ErrCodeTaskRelationCycle holds the unique world-error code of this error
const ErrCodeTaskRelationCycle = 4022
const ErrCodeTaskRelationCycle = 4023
// HTTPError holds the http error description
func (err ErrTaskRelationCycle) HTTPError() web.HTTPError {
@ -1032,6 +1032,34 @@ func (err ErrTaskRelationCycle) HTTPError() web.HTTPError {
}
}
// ErrInvalidFilterExpression represents an error where the task filter expression was invalid
type ErrInvalidFilterExpression struct {
Expression string
ExpressionError error
}
// IsErrInvalidFilterExpression checks if an error is ErrInvalidFilterExpression.
func IsErrInvalidFilterExpression(err error) bool {
_, ok := err.(ErrInvalidFilterExpression)
return ok
}
func (err ErrInvalidFilterExpression) Error() string {
return fmt.Sprintf("Task filter expression is invalid [ExpressionError: %v]", err.ExpressionError)
}
// ErrCodeInvalidFilterExpression holds the unique world-error code of this error
const ErrCodeInvalidFilterExpression = 4024
// HTTPError holds the http error description
func (err ErrInvalidFilterExpression) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusBadRequest,
Code: ErrCodeInvalidFilterExpression,
Message: fmt.Sprintf("The filter expression '%s' is invalid: %v", err.Expression, err.ExpressionError),
}
}
// ============
// Team errors
// ============

View File

@ -152,7 +152,10 @@ func getTaskFiltersByCollections(c *TaskCollection) (filters []*taskFilter, err
parsedFilter, err := fexpr.Parse(c.Filter)
if err != nil {
return nil, err
return nil, &ErrInvalidFilterExpression{
Expression: c.Filter,
ExpressionError: err,
}
}
filters = make([]*taskFilter, 0, len(parsedFilter))