weberr

package module
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2023 License: BSD-2-Clause Imports: 4 Imported by: 42

README

weberr logo

weberr

Package weberr based on https://github.com/pkg/errors with a few additions:

  • Set an error type that corresponds to HTTP status returned by this error
  • Set a user friendly error message (in addition to the error message that will be logged)
  • Add arbitrary details to the error

Go Report Card Build Status GoDoc License

go get github.com/zgalor/weberr

Read the package documentation for more information.

Divergences from pkg/errors

  • We chose weberr.Wrapf(nil, ...) and similar wrapping functions should return a new error, whereas errors.Wrapf(nil, ...) historically returns nil.

License

BSD-2-Clause

[ awesome logo image by gophers... ]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDetails added in v0.5.0

func AddDetails(err error, details interface{}) error

AddDetails adds arbitrary details to an error.

func As added in v0.7.0

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

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true. Otherwise, it returns false.

func Errorf

func Errorf(msg string, args ...interface{}) error

Errorf returns a new NoType error with formatted string.

func GetDetails added in v0.5.0

func GetDetails(err error) []interface{}

GetDetails returns a slice of arbitrary details for all errors. If error is not `errorDetailer` returns nil.

func GetStackTrace

func GetStackTrace(err error) string

GetStackTrace returns the stack trace starting from the first error that has been wrapped / created

func GetUserMessage

func GetUserMessage(err error) string

GetUserMessage returns user readable error message for all errors. If error is not `userMessager` returns empty string.

func Is added in v0.8.0

func Is(err error, target error) bool

Is finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true. Otherwise, it returns false.

func SetUserMessage added in v0.6.0

func SetUserMessage(err error, msg string) error

SetUserMessage sets the user message to an error

func UserErrorf

func UserErrorf(msg string, args ...interface{}) error

UserErrorf returns an error with formatted user message.

func UserWrapf

func UserWrapf(err error, msg string, args ...interface{}) error

UserWrapf adds a user readable message to an error.

func Wrapf

func Wrapf(err error, msg string, args ...interface{}) error

Wrapf creates a wrapping error, with unmodified type and formatted string. The relation to the wrapped err is implicit, do not add a %s for it (like you would with fmt.Errorf). If wrapped err is nil, still returns a new error.

Types

type ErrorType

type ErrorType uint

ErrorType is the type of an error

const (
	// NoType error - Default placeholder for un-typed errors.
	// Methods on NoType behave differently than other types - they
	// will preserve an existing type when wrapping a typed error.
	NoType ErrorType = iota

	// BadRequest error - Code 400
	BadRequest ErrorType = http.StatusBadRequest
	// Unauthorized error - Code 401
	Unauthorized ErrorType = http.StatusUnauthorized
	// PaymentRequired error - Code 402
	PaymentRequired ErrorType = http.StatusPaymentRequired
	// Forbidden error - Code 403
	Forbidden ErrorType = http.StatusForbidden
	// NotFound error - Code 404
	NotFound ErrorType = http.StatusNotFound
	// MethodNotAllowed error - Code 405
	MethodNotAllowed ErrorType = http.StatusMethodNotAllowed
	// NotAcceptable error - Code 406
	NotAcceptable ErrorType = http.StatusNotAcceptable
	// ProxyAuthRequired error - Code 407
	ProxyAuthRequired ErrorType = http.StatusProxyAuthRequired
	// RequestTimeout error - Code 408
	RequestTimeout ErrorType = http.StatusRequestTimeout
	// Conflict error - Code 409
	Conflict ErrorType = http.StatusConflict
	// Gone error - Code 410
	Gone ErrorType = http.StatusGone
	// LengthRequired error - Code 411
	LengthRequired ErrorType = http.StatusLengthRequired
	// PreconditionFailed error - Code 412
	PreconditionFailed ErrorType = http.StatusPreconditionFailed
	// RequestEntityTooLarge error - Code 413
	RequestEntityTooLarge ErrorType = http.StatusRequestEntityTooLarge
	// RequestURITooLong error - Code 414
	RequestURITooLong ErrorType = http.StatusRequestURITooLong
	// UnsupportedMediaType error - Code 415
	UnsupportedMediaType ErrorType = http.StatusUnsupportedMediaType
	// RequestedRangeNotSatisfiable error - Code 416
	RequestedRangeNotSatisfiable ErrorType = http.StatusRequestedRangeNotSatisfiable
	// ExpectationFailed error - Code 417
	ExpectationFailed ErrorType = http.StatusExpectationFailed
	// Teapot error - Code 418
	Teapot ErrorType = http.StatusTeapot
	// UnprocessableEntity error - Code 422
	UnprocessableEntity ErrorType = http.StatusUnprocessableEntity
	// Locked error - Code 423
	Locked ErrorType = http.StatusLocked
	// FailedDependency error - Code 424
	FailedDependency ErrorType = http.StatusFailedDependency
	// UpgradeRequired error - Code 426
	UpgradeRequired ErrorType = http.StatusUpgradeRequired
	// PreconditionRequired error - Code 428
	PreconditionRequired ErrorType = http.StatusPreconditionRequired
	// TooManyRequests error - Code 429
	TooManyRequests ErrorType = http.StatusTooManyRequests
	// RequestHeaderFieldsTooLarge error - Code 431
	RequestHeaderFieldsTooLarge ErrorType = http.StatusRequestHeaderFieldsTooLarge
	// UnavailableForLegalReasons error - Code 451
	UnavailableForLegalReasons ErrorType = http.StatusUnavailableForLegalReasons

	// InternalServerError error - Code 500
	InternalServerError ErrorType = http.StatusInternalServerError
	// NotImplemented error - Code 501
	NotImplemented ErrorType = http.StatusNotImplemented
	// BadGateway error - Code 502
	BadGateway ErrorType = http.StatusBadGateway
	// ServiceUnavailable error - Code 503
	ServiceUnavailable ErrorType = http.StatusServiceUnavailable
	// GatewayTimeout error - Code 504
	GatewayTimeout ErrorType = http.StatusGatewayTimeout
	// HTTPVersionNotSupported error - Code 505
	HTTPVersionNotSupported ErrorType = http.StatusHTTPVersionNotSupported
	// VariantAlsoNegotiates error - Code 506
	VariantAlsoNegotiates ErrorType = http.StatusVariantAlsoNegotiates
	// InsufficientStorage error - Code 507
	InsufficientStorage ErrorType = http.StatusInsufficientStorage
	// LoopDetected error - Code 508
	LoopDetected ErrorType = http.StatusLoopDetected
	// NotExtended error - Code 510
	NotExtended ErrorType = http.StatusNotExtended
	// NetworkAuthenticationRequired error - Code 511
	NetworkAuthenticationRequired ErrorType = http.StatusNetworkAuthenticationRequired
)

func GetType

func GetType(err error) ErrorType

GetType returns the error type for all errors. If error is not `typed` - it returns NoType.

func (ErrorType) AddDetails added in v0.5.0

func (errorType ErrorType) AddDetails(err error, details interface{}) error

AddDetails adds a details element to an error. Also sets error type (or preserves existing type if called on NoType).

func (ErrorType) Errorf

func (errorType ErrorType) Errorf(msg string, args ...interface{}) error

Errorf creates a new error of this type with formatted string.

func (ErrorType) Set

func (errorType ErrorType) Set(err error) error

Set the type of the error

func (ErrorType) SetUserMessage added in v0.6.0

func (errorType ErrorType) SetUserMessage(err error, msg string) error

func (ErrorType) UserErrorf

func (errorType ErrorType) UserErrorf(msg string, args ...interface{}) error

UserErrorf creates a new error with a user readable message.

func (ErrorType) UserWrapf

func (errorType ErrorType) UserWrapf(err error, msg string, args ...interface{}) error

UserWrapf adds a formatted user readable message to an error. If wrapped err already had a user message, combines them with a colon, you should not add a %s for it. Also sets error type (or preserves existing type if called on NoType). If wrapped err is nil, still returns a new error.

func (ErrorType) Wrapf

func (errorType ErrorType) Wrapf(err error, msg string, args ...interface{}) error

Wrapf creates a wrapping error of this type, with formatted string. The relation to the wrapped err is implicit, do not add a %s for it (like you would with fmt.Errorf). If wrapped err is nil, still returns a new error.

Jump to

Keyboard shortcuts

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