fix(filters): return more details when the provided filter time zone is invalid

This commit is contained in:
kolaente 2024-09-30 08:42:16 +02:00
parent 754d56ca82
commit 8d05b5cb01
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
2 changed files with 32 additions and 1 deletions

View File

@ -121,6 +121,34 @@ func InvalidFieldError(fields []string) error {
}
}
// ErrInvalidTimezone represents a "InvalidTimezone" kind of error.
type ErrInvalidTimezone struct {
Name string
LoadError error
}
// IsErrInvalidTimezone checks if an error is a ErrInvalidTimezone.
func IsErrInvalidTimezone(err error) bool {
_, ok := err.(ErrInvalidTimezone)
return ok
}
func (err ErrInvalidTimezone) Error() string {
return fmt.Sprintf("invalid timezone: %s, err: %v", err.Name, err.LoadError)
}
// ErrCodeInvalidTimezone holds the unique world-error code of this error
const ErrCodeInvalidTimezone = 2003
// HTTPError holds the http error description
func (err ErrInvalidTimezone) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusBadRequest,
Code: ErrCodeInvalidTimezone,
Message: fmt.Sprintf("The timezone '%s' is invalid", err.Name),
}
}
// ===========
// Project errors
// ===========

View File

@ -192,7 +192,10 @@ func getTaskFiltersFromFilterString(filter string, filterTimezone string) (filte
if filterTimezone != "" {
loc, err = time.LoadLocation(filterTimezone)
if err != nil {
return
return nil, &ErrInvalidTimezone{
Name: filterTimezone,
LoadError: err,
}
}
}