httperrors

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package httperrors defines an error wrapper that carries extra data for convenient rendering of errors in HTTP responses, esp. in JSON APIs.

Index

Constants

View Source
const (
	HTTPCodeKey    = "HTTPCode"
	PublicErrorKey = "PublicError"
	ErrorIDKey     = "ErrorID"
)

Variables

View Source
var (
	/*
		Unavailable error should be returned when the service is experiencing
		a temporary downtime (HTTP 500 Internal Server Error).

		An appropriate client behavior is to retry after a delay.

		Examples: critical external API is unreachable, server is out of
		disk space, general unexpected failure.
	*/
	Unavailable = Define(http.StatusInternalServerError, "unavail")

	/*
		Overload signals expected temporary unavailability of the endpoint
		due to excessive requests or quota violation (HTTP 503 Service Unavailable).

		An appropriate client behavior is to retry with increasing backoff.
	*/
	Overload = Define(http.StatusServiceUnavailable, "unavail")

	/*
		BadRequest signals a wildly incorrect HTTP call that uses invalid
		parameter names, omits required parameters, uses invalid format for
		parameter values (e.g. a string passed for an integer parameter)
		or uses an unsupported value for a enumerated parameter.

		This should NOT be used for merely invalid data that likely comes from
		the user. Please define an appropriate error value for that.

		An appropriate client behavior is to log an error and fail the operation
		with a generic error message asking to contact support.
	*/
	BadRequest = Define(http.StatusBadRequest, "bad_request")

	/*
		NotFound signals a that the resource that the HTTP call primarily refers
		to does not exist. E.g. you're trying to update a page that does not exist.

		This should NOT be used for non-existent auxiliary data. For example,
		trying to save an article with a non-existent author should return
		a validation error (which you should define), not NotFound. Similarly,
		an unknown user account on a normal API endpoint should return
		an appropriate authentication error, not NotFound.

		An appropriate client behavior is to remove the UI corresponding to
		the data that this API call operates on.
	*/
	NotFound = Define(http.StatusNotFound, "not_found")

	/*
		MethodNotAllowed signals a wildly incorrect HTTP call, caused by
		using a mismatched HTTP verb.

		An appropriate client behavior is to log an error and fail the operation
		with a generic error message asking to contact support.
	*/
	MethodNotAllowed = Define(http.StatusMethodNotAllowed, "bad_request")
)

Functions

func ErrorID

func ErrorID(err error) string

func HTTPCode

func HTTPCode(err error) int

func HTTPMessage

func HTTPMessage(err error) string

func Is4xx

func Is4xx(err error) bool

func Is5xx

func Is5xx(err error) bool

func String

func String(err error, keys ...string) string

func Value

func Value(err error, key string) interface{}

Types

type BaseError

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

func Define

func Define(statusCode int, id string) BaseError

func (BaseError) Error

func (base BaseError) Error() string

func (BaseError) ErrorID

func (base BaseError) ErrorID() string

func (BaseError) Extra

func (base BaseError) Extra(k string, v interface{}) *Error

func (BaseError) ForeachExtra

func (base BaseError) ForeachExtra(f func(k string, v interface{}))

func (BaseError) HTTPCode

func (base BaseError) HTTPCode() int

func (BaseError) Is

func (base BaseError) Is(err error) bool

func (BaseError) Msg

func (base BaseError) Msg(pubmsg string) *Error

func (BaseError) Msgf

func (base BaseError) Msgf(format string, args ...any) *Error

func (BaseError) PublicError

func (base BaseError) PublicError() string

func (BaseError) String

func (base BaseError) String() string

func (BaseError) Wrap

func (base BaseError) Wrap(cause error) Interface

func (BaseError) WrapCustom

func (base BaseError) WrapCustom(prototype, cause error) *Error

func (BaseError) WrapMsg

func (base BaseError) WrapMsg(cause error, pubmsg string) Interface

type Error

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

func New

func New(statusCode int, id string) *Error

func (*Error) Error

func (err *Error) Error() string

func (*Error) ErrorID

func (err *Error) ErrorID() string

func (*Error) Extra

func (err *Error) Extra(k string, v interface{}) *Error

func (*Error) ForeachExtra

func (err *Error) ForeachExtra(f func(k string, v interface{}))

func (*Error) HTTPCode

func (err *Error) HTTPCode() int

func (*Error) Is

func (err *Error) Is(e error) bool

func (*Error) Msg

func (err *Error) Msg(pubmsg string) *Error

func (*Error) Msgf

func (err *Error) Msgf(format string, args ...any) *Error

func (*Error) PublicError

func (err *Error) PublicError() string

func (*Error) String

func (err *Error) String() string

func (*Error) Unwrap

func (err *Error) Unwrap() error

type Interface

type Interface interface {
	error
	HTTPCode() int
	ErrorID() string
	PublicError() string
	ForeachExtra(f func(k string, v interface{}))
}

func Errorf

func Errorf(statusCode int, id string, msg string, args ...any) Interface

type PublicMessenger added in v0.2.2

type PublicMessenger interface {
	PublicError() string
}

Jump to

Keyboard shortcuts

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