herrors

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2020 License: MPL-2.0 Imports: 2 Imported by: 1

README

herrors Build Status

import "github.com/geek/herrors"

Examples

Basic Bad Request
func handler(w http.ResponseWriter, r *http.Request) {
  // perform some validation
  if !isValid(r) {
    herrors.Write(w, herrors.ErrBadRequest)
    return
  }
}
Bad Request with Wrapping
func validate(r *http.Request) error {
  // ... do some validation and return an error on failure
}

func handler(w http.ResponseWriter, r *http.Request) {
  if err := validate(r); err != nil {
    err = herrors.Wrap(err, http.StatusBadRequest)
    herrors.Write(w, err)
    return
  }
}

Usage

func New
func New(statusCode uint) error

New constructs an ErrHttp error with the code and message populated

func Wrap
func Wrap(err error, statusCode uint) error

Wrap will create a new ErrHttp and place the provided error inside of it. There is a complementary errors.Unwrap to retrieve the wrapped error.

func Write
func Write(w http.ResponseWriter, err error)

Write sets the response status code to the provided error code. When unable to find an ErrHttp error in the error chain then a 500 internal error is output.

type ErrHttp
type ErrHttp struct {
}
func (*ErrHttp) Code
func (e *ErrHttp) Code() uint

Code returns http status code that the error represents

func (*ErrHttp) Error
func (e *ErrHttp) Error() string

Error returns http friendly error message

func (*ErrHttp) Is
func (e *ErrHttp) Is(target error) bool

Is used by errors.Is for comparing ErrHttp

func (*ErrHttp) Unwrap
func (e *ErrHttp) Unwrap() error

Unwrap will return the first wrapped error if there is one

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// 4xx errors
	ErrBadRequest           = New(http.StatusBadRequest)
	ErrUnauthorized         = New(http.StatusUnauthorized)
	ErrPaymentRequired      = New(http.StatusPaymentRequired)
	ErrForbidden            = New(http.StatusForbidden)
	ErrNotFound             = New(http.StatusNotFound)
	ErrMethodNotAllowed     = New(http.StatusMethodNotAllowed)
	ErrNotAcceptable        = New(http.StatusNotAcceptable)
	ErrProxyAuthRequired    = New(http.StatusProxyAuthRequired)
	ErrRequestTimeout       = New(http.StatusRequestTimeout)
	ErrConflict             = New(http.StatusConflict)
	ErrResourceGone         = New(http.StatusGone)
	ErrLengthRequired       = New(http.StatusLengthRequired)
	ErrPreconditionFailed   = New(http.StatusPreconditionFailed)
	ErrEntityTooLarge       = New(http.StatusRequestEntityTooLarge)
	ErrURITooLong           = New(http.StatusRequestURITooLong)
	ErrUnsupportedMediaType = New(http.StatusUnsupportedMediaType)
	ErrRangeNotSatisfiable  = New(http.StatusRequestedRangeNotSatisfiable)
	ErrExpectationFailed    = New(http.StatusExpectationFailed)
	ErrMisdirectedRequest   = New(http.StatusMisdirectedRequest)
	ErrUnprocessableEntity  = New(http.StatusUnprocessableEntity)
	ErrLocked               = New(http.StatusLocked)
	ErrFailedDependency     = New(http.StatusFailedDependency)
	ErrTooEarly             = New(http.StatusTooEarly)
	ErrPreconditionRequired = New(http.StatusPreconditionRequired)
	ErrTooManyRequests      = New(http.StatusTooManyRequests)
	ErrHeaderFieldsTooLarge = New(http.StatusRequestHeaderFieldsTooLarge)
	ErrIllegal              = New(http.StatusUnavailableForLegalReasons)

	// 5xx errors
	ErrInternalServer                = New(http.StatusInternalServerError)
	ErrNotImplemented                = New(http.StatusNotImplemented)
	ErrBadGateway                    = New(http.StatusBadGateway)
	ErrServiceUnavailable            = New(http.StatusServiceUnavailable)
	ErrGatewayTimeout                = New(http.StatusGatewayTimeout)
	ErrHTTPVersionNotSupported       = New(http.StatusHTTPVersionNotSupported)
	ErrVariantAlsoNegotiates         = New(http.StatusVariantAlsoNegotiates)
	ErrInsufficientStorage           = New(http.StatusInsufficientStorage)
	ErrLoopDetected                  = New(http.StatusLoopDetected)
	ErrNotExtended                   = New(http.StatusNotExtended)
	ErrNetworkAuthenticationRequired = New(http.StatusNetworkAuthenticationRequired)
)

Functions

func New

func New(statusCode uint) error

New constructs an ErrHttp error with the code and message populated

func Wrap

func Wrap(err error, statusCode uint) error

Wrap will create a new ErrHttp and place the provided error inside of it. There is a complementary errors.Unwrap to retrieve the wrapped error.

func Write

func Write(w http.ResponseWriter, err error)

Write sets the response status code to the provided error code. When unable to find an ErrHttp error in the error chain then a 500 internal error is output.

Types

type ErrHttp

type ErrHttp struct {
	// contains filtered or unexported fields
}

func (*ErrHttp) Code

func (e *ErrHttp) Code() uint

Code returns http status code that the error represents

func (*ErrHttp) Error

func (e *ErrHttp) Error() string

Error returns http friendly error message

func (*ErrHttp) Is

func (e *ErrHttp) Is(target error) bool

Is used by errors.Is for comparing ErrHttp

func (*ErrHttp) Unwrap

func (e *ErrHttp) Unwrap() error

Unwrap will return the first wrapped error if there is one

Jump to

Keyboard shortcuts

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