homebox/backend/internal/sys/validate/errors.go
Hayden 6529549289
refactor: http interfaces (#114)
* implement custom http handler interface

* implement trace_id

* normalize http method spacing for consistent logs

* fix failing test

* fix linter errors

* cleanup old dead code

* more route cleanup

* cleanup some inconsistent errors

* update and generate code

* make taskfile more consistent

* update task calls

* run tidy

* drop `@` tag for version

* use relative paths

* tidy

* fix auto-setting variables

* update build paths

* add contributing guide

* tidy
2022-10-29 18:15:35 -08:00

119 lines
2.4 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 NewInvalidRouteKeyError(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 implments 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 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
}
}