homebox/backend/internal/sys/validate/errors.go
Hayden c4b923847a
feat: user defined currencies (#700)
* basic currency service for loading at runtime

* api endpoint for currencies

* sort slice before return

* remove currency validation

* validate using currency service

* implement selecting dynamic currency options

* bump go version

* fix type definition

* specify explicit type

* change go versions

* proper types for assetId

* log/return currency error

* make case insensative

* use ToUpper instead

* feat: adding new currencies (#715)

* fix: task swag (#710)

Co-authored-by: Quoing <pavel.cadersky@mavenir.com>

* [feat] Adding new currencies

---------

Co-authored-by: quoing <quoing@users.noreply.github.com>
Co-authored-by: Quoing <pavel.cadersky@mavenir.com>
Co-authored-by: Bradley <41597815+userbradley@users.noreply.github.com>

* remove ts file and consoldate new values into json

* move flag to options namespace

* add env config for currencies

* basic documentaion

* remove in sync test

---------

Co-authored-by: quoing <quoing@users.noreply.github.com>
Co-authored-by: Quoing <pavel.cadersky@mavenir.com>
Co-authored-by: Bradley <41597815+userbradley@users.noreply.github.com>
2024-01-18 10:45:42 -09:00

123 lines
2.5 KiB
Go

package validate
import (
"encoding/json"
"errors"
)
type UnauthorizedError struct {
}
func (err *UnauthorizedError) Error() string {
return "unauthorized"
}
func IsUnauthorizedError(err error) bool {
var re *UnauthorizedError
return errors.As(err, &re)
}
func NewUnauthorizedError() error {
return &UnauthorizedError{}
}
type InvalidRouteKeyError struct {
key string
}
func (err *InvalidRouteKeyError) Error() string {
return "invalid route key: " + err.key
}
func NewRouteKeyError(key string) error {
return &InvalidRouteKeyError{key}
}
func IsInvalidRouteKeyError(err error) bool {
var re *InvalidRouteKeyError
return errors.As(err, &re)
}
// ErrorResponse is the form used for API responses from failures in the API.
type ErrorResponse struct {
Error string `json:"error"`
Fields string `json:"fields,omitempty"`
}
// RequestError is used to pass an error during the request through the
// application with web specific context.
type RequestError struct {
Err error
Status int
Fields error
}
// NewRequestError wraps a provided error with an HTTP status code. This
// function should be used when handlers encounter expected errors.
func NewRequestError(err error, status int) error {
return &RequestError{err, status, nil}
}
func (err *RequestError) Error() string {
return err.Err.Error()
}
// IsRequestError checks if an error of type RequestError exists.
func IsRequestError(err error) bool {
var re *RequestError
return errors.As(err, &re)
}
// FieldError is used to indicate an error with a specific request field.
type FieldError struct {
Field string `json:"field"`
Error string `json:"error"`
}
// FieldErrors represents a collection of field errors.
type FieldErrors []FieldError
func (fe FieldErrors) Append(field, reason string) FieldErrors {
return append(fe, FieldError{
Field: field,
Error: reason,
})
}
func (fe FieldErrors) Nil() bool {
return len(fe) == 0
}
// Error implements the error interface.
func (fe FieldErrors) Error() string {
d, err := json.Marshal(fe)
if err != nil {
return err.Error()
}
return string(d)
}
func NewFieldErrors(errs ...FieldError) FieldErrors {
return errs
}
func NewFieldError(field, reason string) FieldError {
return FieldError{Field: field, Error: reason}
}
func IsFieldError(err error) bool {
v := FieldErrors{}
return errors.As(err, &v)
}
// Cause iterates through all the wrapped errors until the root
// error value is reached.
func Cause(err error) error {
root := err
for {
if err = errors.Unwrap(root); err == nil {
return root
}
root = err
}
}