Documentation
¶
Overview ¶
Package errors provides a generic error carrying useful information:
- 2 different messages: one for the end user and one for the developer
- a Kind: a string that can act as an ID for errors
- a httpStatus
- a timestamp of when the error was created
This error implement the json.Marshaler and xml.Marshaler interface, so you can return this error in your http handlers.
It also implements the errors.Unwrap interface, allowing you to get the previous error.
It depends on the gapi/log package, as the error will implement several of its interfaces to log the errors correctly.
## Create a new error
To create a new error, simply call the Err() function and use the setter to set any data you want.
The setter functions will modify the data and won't create a new error, you don't need to reassign the error.
import ( "net/http" "github.com/mwm-io/gapi/errors" ) err := errors.Err("my error). WithKind("not_found"). WithStatusCode(http.StatusNotFound). WithMessage("not found")
## Wrap an existing error
To wrap an existing error, simply call the Wrap() function. You can then modify the new error as you want.
Wrapping a nil error will return a nil value.
import ( "fmt" "github.com/mwm-io/gapi/errors" ) err := fmt.Errorf("source error") newErr := errors.Wrap(err, "error").WithKind("new_kind")
### Populate data from the source error
You might want to carry more than just the message type from the source error.
In order to do that you need to implement the ErrorBuilder interface and register your builder with an init function.
Your builder should concern a single type of error.
You can read an example with the errors/google package.
import ( "github.com/mwm-io/gapi/errors" ) func init() { errors.AddBuilder() } var GrpcCodeErrorBuilder = errors.ErrorBuilderFunc(func(err errors.Error, sourceError error) errors.Error { sourceErrI, ok := sourceErr.(interface{ WithKind() string }) if !ok { return err } return err.WithKind(sourceErrI.WithKind()) })
## Why is it an interface ?
You may wonder why we use an Error interface and why don't we use the FullError struct directly.
This is because `(*FullError)(nil) != nil`: a nil value with a concrete type won't match nil.
In order to keep the idiomatic `if Err("my err") != nil`, we always return the Error interface.
Index ¶
- func AddErrorBuilders(builders ...ErrorBuilder)
- func GetCallers() (callerName, caller string, callStack []string)
- type Error
- func BadGateway(kind, msgFormat string, args ...interface{}) Error
- func BadRequest(kind, msgFormat string, args ...interface{}) Error
- func Conflict(kind, msgFormat string, args ...interface{}) Error
- func Err(kind, format string, args ...interface{}) Error
- func ExpectationFailed(kind, msgFormat string, args ...interface{}) Error
- func FailedDependency(kind, msgFormat string, args ...interface{}) Error
- func Forbidden(kind, msgFormat string, args ...interface{}) Error
- func GatewayTimeout(kind, msgFormat string, args ...interface{}) Error
- func Gone(kind, msgFormat string, args ...interface{}) Error
- func HTTPVersionNotSupported(kind, msgFormat string, args ...interface{}) Error
- func InsufficientStorage(kind, msgFormat string, args ...interface{}) Error
- func InternalServerError(kind, msgFormat string, args ...interface{}) Error
- func LengthRequired(kind, msgFormat string, args ...interface{}) Error
- func Locked(kind, msgFormat string, args ...interface{}) Error
- func LoopDetected(kind, msgFormat string, args ...interface{}) Error
- func MethodNotAllowed(kind, msgFormat string, args ...interface{}) Error
- func MisdirectedRequest(kind, msgFormat string, args ...interface{}) Error
- func NetworkAuthenticationRequired(kind, msgFormat string, args ...interface{}) Error
- func NotAcceptable(kind, msgFormat string, args ...interface{}) Error
- func NotExtended(kind, msgFormat string, args ...interface{}) Error
- func NotFound(kind, msgFormat string, args ...interface{}) Error
- func NotImplemented(kind, msgFormat string, args ...interface{}) Error
- func PaymentRequired(kind, msgFormat string, args ...interface{}) Error
- func PreconditionFailed(kind, msgFormat string, args ...interface{}) Error
- func PreconditionRequired(kind, msgFormat string, args ...interface{}) Error
- func ProxyAuthRequired(kind, msgFormat string, args ...interface{}) Error
- func RequestEntityTooLarge(kind, msgFormat string, args ...interface{}) Error
- func RequestHeaderFieldsTooLarge(kind, msgFormat string, args ...interface{}) Error
- func RequestTimeout(kind, msgFormat string, args ...interface{}) Error
- func RequestURITooLong(kind, msgFormat string, args ...interface{}) Error
- func RequestedRangeNotSatisfiable(kind, msgFormat string, args ...interface{}) Error
- func ServiceUnavailable(kind, msgFormat string, args ...interface{}) Error
- func Teapot(kind, msgFormat string, args ...interface{}) Error
- func TooEarly(kind, msgFormat string, args ...interface{}) Error
- func TooManyRequests(kind, msgFormat string, args ...interface{}) Error
- func Unauthorized(kind, msgFormat string, args ...interface{}) Error
- func UnavailableForLegalReasons(kind, msgFormat string, args ...interface{}) Error
- func UnprocessableEntity(kind, msgFormat string, args ...interface{}) Error
- func UnsupportedMediaType(kind, msgFormat string, args ...interface{}) Error
- func UpgradeRequired(kind, msgFormat string, args ...interface{}) Error
- func VariantAlsoNegotiates(kind, msgFormat string, args ...interface{}) Error
- func Wrap(err error) Error
- type ErrorBuilder
- type FullError
- func (e *FullError) Caller() string
- func (e *FullError) CallerName() string
- func (e *FullError) Callstack() []string
- func (e *FullError) Error() string
- func (e *FullError) Kind() string
- func (e *FullError) MarshalJSON() ([]byte, error)
- func (e *FullError) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error
- func (e *FullError) Message() string
- func (e *FullError) StatusCode() int
- func (e *FullError) Timestamp() time.Time
- func (e *FullError) Unwrap() error
- func (e *FullError) WithError(err error) Error
- func (e *FullError) WithKind(kind string) Error
- func (e *FullError) WithMessage(format string, args ...interface{}) Error
- func (e *FullError) WithStatus(status int) Error
- type HttpError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddErrorBuilders ¶ added in v0.2.2
func AddErrorBuilders(builders ...ErrorBuilder)
AddErrorBuilders appends custom errors.ErrorBuilder. These callbacks are executed when wrapping an error with errors.Wrap().
func GetCallers ¶ added in v0.2.4
GetCallers return the caller of the function and the call stack
Types ¶
type Error ¶
type Error interface { error Unwrap() error json.Marshaler xml.Marshaler Message() string Kind() string StatusCode() int Timestamp() time.Time CallerName() string Caller() string Callstack() []string WithMessage(format string, args ...interface{}) Error WithKind(string) Error WithStatus(int) Error WithError(error) Error }
Error represents the interface for the FullError struct. It is necessary so we can compare nil errors. (nil.(*FullError) != nil)
func BadGateway ¶ added in v0.0.8
BadGateway return an error with status code http.StatusBadGateway
func BadRequest ¶ added in v0.0.8
BadRequest return an error with status code http.StatusBadRequest
func ExpectationFailed ¶ added in v0.0.8
ExpectationFailed return an error with status code http.StatusExpectationFailed
func FailedDependency ¶ added in v0.0.8
FailedDependency return an error with status code http.StatusFailedDependency
func GatewayTimeout ¶ added in v0.0.8
GatewayTimeout return an error with status code http.StatusGatewayTimeout
func HTTPVersionNotSupported ¶ added in v0.0.8
HTTPVersionNotSupported return an error with status code http.StatusHTTPVersionNotSupported
func InsufficientStorage ¶ added in v0.0.8
InsufficientStorage return an error with status code http.StatusInsufficientStorage
func InternalServerError ¶ added in v0.0.8
InternalServerError return an error with status code http.StatusInternalServerError
func LengthRequired ¶ added in v0.0.8
LengthRequired return an error with status code http.StatusLengthRequired
func LoopDetected ¶ added in v0.0.8
LoopDetected return an error with status code http.StatusLoopDetected
func MethodNotAllowed ¶ added in v0.0.8
MethodNotAllowed return an error with status code http.StatusMethodNotAllowed
func MisdirectedRequest ¶ added in v0.0.8
MisdirectedRequest return an error with status code http.StatusMisdirectedRequest
func NetworkAuthenticationRequired ¶ added in v0.0.8
NetworkAuthenticationRequired return an error with status code http.StatusNetworkAuthenticationRequired
func NotAcceptable ¶ added in v0.0.8
NotAcceptable return an error with status code http.StatusNotAcceptable
func NotExtended ¶ added in v0.0.8
NotExtended return an error with status code http.StatusNotExtended
func NotImplemented ¶ added in v0.0.8
NotImplemented return an error with status code http.StatusNotImplemented
func PaymentRequired ¶ added in v0.0.8
PaymentRequired return an error with status code http.StatusPaymentRequired
func PreconditionFailed ¶ added in v0.0.8
PreconditionFailed return an error with status code http.StatusPreconditionFailed
func PreconditionRequired ¶ added in v0.0.8
PreconditionRequired return an error with status code http.StatusPreconditionRequired
func ProxyAuthRequired ¶ added in v0.0.8
ProxyAuthRequired return an error with status code http.StatusProxyAuthRequired
func RequestEntityTooLarge ¶ added in v0.0.8
RequestEntityTooLarge return an error with status code http.StatusRequestEntityTooLarge
func RequestHeaderFieldsTooLarge ¶ added in v0.0.8
RequestHeaderFieldsTooLarge return an error with status code http.StatusRequestHeaderFieldsTooLarge
func RequestTimeout ¶ added in v0.0.8
RequestTimeout return an error with status code http.StatusRequestTimeout
func RequestURITooLong ¶ added in v0.0.8
RequestURITooLong return an error with status code http.StatusRequestURITooLong
func RequestedRangeNotSatisfiable ¶ added in v0.0.8
RequestedRangeNotSatisfiable return an error with status code http.StatusRequestedRangeNotSatisfiable
func ServiceUnavailable ¶ added in v0.0.8
ServiceUnavailable return an error with status code http.StatusServiceUnavailable
func TooManyRequests ¶ added in v0.0.8
TooManyRequests return an error with status code http.StatusTooManyRequests
func Unauthorized ¶ added in v0.0.8
Unauthorized return an error with status code http.StatusUnauthorized
func UnavailableForLegalReasons ¶ added in v0.0.8
UnavailableForLegalReasons return an error with status code http.StatusUnavailableForLegalReasons
func UnprocessableEntity ¶ added in v0.0.8
UnprocessableEntity return an error with status code http.StatusUnprocessableEntity
func UnsupportedMediaType ¶ added in v0.0.8
UnsupportedMediaType return an error with status code http.StatusUnsupportedMediaType
func UpgradeRequired ¶ added in v0.0.8
UpgradeRequired return an error with status code http.StatusUpgradeRequired
func VariantAlsoNegotiates ¶ added in v0.0.8
VariantAlsoNegotiates return an error with status code http.StatusVariantAlsoNegotiates
type ErrorBuilder ¶
ErrorBuilder is a callback that transform the given error to a gapi Error.
type FullError ¶
type FullError struct {
// contains filtered or unexported fields
}
FullError is a concrete error that implements the Error interface
func (*FullError) Caller ¶ added in v0.2.4
Caller implements the error interface. It will return the name of the function that created the error
func (*FullError) CallerName ¶ added in v0.2.4
CallerName implements the error interface. It will return the name of the function that created the error
func (*FullError) Callstack ¶ added in v0.2.4
Callstack implements the error interface. It will return the complete callstack of the error creation
func (*FullError) Error ¶
FullError implements the error interface. It will return the "developer" message in opposition to the user message, which is returned by FullError.Message
func (*FullError) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*FullError) MarshalXML ¶
MarshalXML implements the xml.Marshaler interface.
func (*FullError) StatusCode ¶
StatusCode implements server.WithStatusCode
func (*FullError) WithMessage ¶
WithMessage sets the user message.
func (*FullError) WithStatus ¶
WithStatus sets the error status. It will also modify the severity for status >= 400