Documentation ¶
Overview ¶
Package weberr allows to add behaviors to errors. The idea is to decorate errors with behaviors without the needs of creating custom error types that directly implement them.
You can decorate also custom errors. The advantage of adding behaviors to custom type in this way - rather than making them implement such behaviors directly - is that behaviors of wrapped errors are implicitly propagated.
Index ¶
- func BadRequest(err error, opts ...Opt) error
- func Fields(err error) (fields map[string]interface{}, ok bool)
- func InternalError(err error, opts ...Opt) error
- func NewError(err error, msg string, status int, opts ...Opt) error
- func NotAuthorized(err error, opts ...Opt) error
- func NotFound(err error, opts ...Opt) error
- func Response(err error) (body interface{}, status int, ok bool)
- func Wrap(err error, opts ...Opt) error
- type ErrorResponse
- type Opt
- type RequestError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BadRequest ¶
BadRequest returns a new `Bad Request` request error.
func Fields ¶
Fields extracts fields to be logged together with the error, if possible. An error has fields if it implements the interface:
type fielder interface { Fields() map[string]interface{} }
If the error does not implement 'Fields' behavior, it returns 'ok' to false and other parameters should be ignored.
func InternalError ¶
InternalError returns a new `Internal Server Error` request error.
func NewError ¶
NewError wraps a provided error with HTTP details that can be used later on to build and log an appropriate HTTP error response. This function should be used when handlers encounter expected errors.
func NotAuthorized ¶
NotAuthorized returns a new `Status Not Authorized` request error.
func Response ¶
Response extracts a web response body and a status code from the error, if possible.
An error has a response if it satisfies the interface:
type responder interface { Response() (interface{}, int) }
If the error does not have the Response behavior, this function returns 'ok' to false and other return parameters should be ignored.
Types ¶
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
}
ErrorResponse contains the error message in the following form: `{ "error": "some error message" }`.
type Opt ¶
func WithFields ¶
WithFields returns a functional option that adds the 'Fields' behavior to the error.
func WithResponse ¶
WithResponse returns a functional option that adds the 'Response' behavior to the error.
type RequestError ¶
type RequestError struct {
Err error
}
RequestError is used to pass an error during the request through the application with web specific context. RequestError wraps a provided error with HTTP details that can be used later on to build an appropriate HTTP error response.
func (*RequestError) Error ¶
func (r *RequestError) Error() string
Error implements the error interface. It uses the default message of the wrapped error. This is what will be shown in the services' logs.
func (*RequestError) Unwrap ¶
func (e *RequestError) Unwrap() error
Unwrap allows to propagate inner error behaviors.