errors

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2022 License: MIT Imports: 3 Imported by: 4

README

errors

Build Status Report Card GoCover

Collection of HTTP error implementations

HTTP error interface
type Error interface{
  error
  Code() int
}
Implementations
Code Corresponding error type
400 BadRequest
401 Unauthorized
403 Forbidden
404 NotFound
405 MethodNotAllowed
406 NotAcceptable
408 RequestTimeout
409 Conflict
500 InternalServer
501 NotImplemented

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v1.1.0

func As(err error, target interface{}) bool

As finds the first error in err's chain that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false. Wraps original errors.As function to avoid importing "errors".

func Is added in v1.1.0

func Is(err, target error) bool

Is reports whether any error in err's chain matches target. Wraps original errors.Is function to avoid importing "errors".

func New added in v1.1.0

func New(text string) error

New returns an error that formats as the given text. Wraps original errors.New function to avoid importing "errors".

func Send

func Send(w http.ResponseWriter, err interface{})

Send provided argument as an HTTP error to the client (retrieving status code if available). If status code cannot be retrieved it sends error with status 500 by default . If value does not implement any of supported error interfaces (error/Error) - it tries to convert the value to a string.

func Unwrap added in v1.1.0

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil. Wraps original errors.Unwrap function to avoid importing "errors".

Types

type BadRequest

type BadRequest string

BadRequest is a 400 HTTP error.

func BadRequestf

func BadRequestf(format string, args ...interface{}) BadRequest

BadRequestf returns formatted BadRequest error.

func NewBadRequest

func NewBadRequest(cause error) BadRequest

NewBadRequest is a constructor func for 400 HTTP error.

func (BadRequest) Code

func (e BadRequest) Code() int

Code returns HTTP status code.

func (BadRequest) Error

func (e BadRequest) Error() string

type Conflict

type Conflict string

Conflict is a 409 HTTP error.

func Conflictf

func Conflictf(format string, args ...interface{}) Conflict

Conflictf returns formatted Conflict error.

func NewConflict

func NewConflict(cause error) Conflict

NewConflict is a constructor func for 409 HTTP error.

func (Conflict) Code

func (e Conflict) Code() int

Code returns HTTP status code.

func (Conflict) Error

func (e Conflict) Error() string

type Error

type Error interface {
	error
	Code() int
}

Error interface describes errors that contain (HTTP or any other) status code.

type Forbidden

type Forbidden string

Forbidden is a 403 HTTP error.

func Forbiddenf

func Forbiddenf(format string, args ...interface{}) Forbidden

Forbiddenf returns formatted Forbidden error.

func NewForbidden

func NewForbidden(cause error) Forbidden

NewForbidden is a constructor func for 403 HTTP error.

func (Forbidden) Code

func (e Forbidden) Code() int

Code returns HTTP status code.

func (Forbidden) Error

func (e Forbidden) Error() string

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, message string, code int)

HandlerFunc is an error handler function, for instance http.Error

type InternalServer

type InternalServer string

InternalServer is a 500 HTTP error.

func InternalServerf

func InternalServerf(format string, args ...interface{}) InternalServer

InternalServerf returns formatted InternalServer error.

func NewInternalServer

func NewInternalServer(cause error) InternalServer

NewInternalServer is a constructor func for 500 HTTP error.

func (InternalServer) Code

func (e InternalServer) Code() int

Code returns HTTP status code.

func (InternalServer) Error

func (e InternalServer) Error() string

type Logger

type Logger interface{ Println(...interface{}) }

Logger is a simple error logger.

type MethodNotAllowed

type MethodNotAllowed string

MethodNotAllowed is a 404 HTTP error.

func MethodNotAllowedf

func MethodNotAllowedf(format string, args ...interface{}) MethodNotAllowed

MethodNotAllowedf returns formatted MethodNotAllowed error.

func NewMethodNotAllowed

func NewMethodNotAllowed(cause error) MethodNotAllowed

NewMethodNotAllowed is a constructor func for 405 HTTP error.

func (MethodNotAllowed) Code

func (e MethodNotAllowed) Code() int

Code returns HTTP status code.

func (MethodNotAllowed) Error

func (e MethodNotAllowed) Error() string

type Middleware

type Middleware func(Sender) Sender

Middleware is a chainable sender wrapper func.

func WithLogger

func WithLogger(logger Logger) Middleware

WithLogger wrapps provided sender with logger.

Example: errors.LoggerWrapper(logger)(errors.Send)(w, err)

type NotAcceptable added in v1.2.0

type NotAcceptable string

NotAcceptable is a 406 HTTP error.

func NewNotAcceptable added in v1.2.0

func NewNotAcceptable(cause error) NotAcceptable

NewMethodNotAllowed is a constructor func for 406 HTTP error.

func NotAcceptablef added in v1.2.0

func NotAcceptablef(format string, args ...interface{}) NotAcceptable

NotAcceptablef returns formatted NotAcceptable error.

func (NotAcceptable) Code added in v1.2.0

func (e NotAcceptable) Code() int

Code returns HTTP status code.

func (NotAcceptable) Error added in v1.2.0

func (e NotAcceptable) Error() string

type NotFound

type NotFound string

NotFound is a 404 HTTP error.

func NewNotFound

func NewNotFound(cause error) NotFound

NewNotFound is a constructor func for 404 HTTP error.

func NotFoundf

func NotFoundf(format string, args ...interface{}) NotFound

NotFoundf returns formatted NotFound error.

func (NotFound) Code

func (e NotFound) Code() int

Code returns HTTP status code.

func (NotFound) Error

func (e NotFound) Error() string

type NotImplemented

type NotImplemented string

NotImplemented is a 501 HTTP error.

func NewNotImplemented

func NewNotImplemented(cause error) NotImplemented

NewNotImplemented is a constructor func for 501 HTTP error.

func NotImplementedf

func NotImplementedf(format string, args ...interface{}) NotImplemented

NotImplementedf returns formatted NotImplemented error.

func (NotImplemented) Code

func (e NotImplemented) Code() int

Code returns HTTP status code.

func (NotImplemented) Error

func (e NotImplemented) Error() string

type RequestTimeout

type RequestTimeout string

RequestTimeout is a 404 HTTP error.

func NewRequestTimeout

func NewRequestTimeout(cause error) RequestTimeout

NewRequestTimeout is a constructor func for 408 HTTP error.

func RequestTimeoutf

func RequestTimeoutf(format string, args ...interface{}) RequestTimeout

RequestTimeoutf returns formatted RequestTimeout error.

func (RequestTimeout) Code

func (e RequestTimeout) Code() int

Code returns HTTP status code.

func (RequestTimeout) Error

func (e RequestTimeout) Error() string

type ResponseLogger

type ResponseLogger struct{ Logger }

ResponseLogger provides another (object oriented) approach to log the error.

Example: (&errors.ResponseLogger{Logger: logger}).Log(errors.Send)(w, err)

func (*ResponseLogger) Log

func (rl *ResponseLogger) Log(sender Sender) Sender

Log creates Sender middleware func.

type Sender

type Sender func(http.ResponseWriter, interface{})

Sender is a function that sends an error through the provided response writer.

type StatusError

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

StatusError is a basic Error interface implementation.

func NewStatusError

func NewStatusError(code int, cause error) StatusError

NewStatusError is a constructor func for StatusError.

func (StatusError) Code

func (e StatusError) Code() int

Code returns HTTP status code (to satisfy Error interface).

type Unauthorized

type Unauthorized string

Unauthorized is a 401 HTTP error.

func NewUnauthorized

func NewUnauthorized(cause error) Unauthorized

NewUnauthorized is a constructor func for 401 HTTP error.

func Unauthorizedf

func Unauthorizedf(format string, args ...interface{}) Unauthorized

Unauthorizedf returns formatted Unauthorized error.

func (Unauthorized) Code

func (e Unauthorized) Code() int

Code returns HTTP status code.

func (Unauthorized) Error

func (e Unauthorized) Error() string

Jump to

Keyboard shortcuts

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