weberr

package
v0.0.0-...-53bd342 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2023 License: GPL-3.0 Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(err error, opts ...Opt) error

BadRequest returns a new `Bad Request` request error.

func Fields

func Fields(err error) (fields map[string]interface{}, ok bool)

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

func InternalError(err error, opts ...Opt) error

InternalError returns a new `Internal Server Error` request error.

func NewError

func NewError(err error, msg string, status int, opts ...Opt) error

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

func NotAuthorized(err error, opts ...Opt) error

NotAuthorized returns a new `Status Not Authorized` request error.

func NotFound

func NotFound(err error, opts ...Opt) error

NotFound returns a new `Status Not Found` request error.

func Response

func Response(err error) (body interface{}, status int, ok bool)

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.

func Wrap

func Wrap(err error, opts ...Opt) error

Wrap allows to assign behaviors to an error. It leverages functional options for the selection of the required behaviors.

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

type Opt func(error) error

func WithFields

func WithFields(fields map[string]interface{}) Opt

WithFields returns a functional option that adds the 'Fields' behavior to the error.

func WithResponse

func WithResponse(body interface{}, status int) Opt

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL