Documentation ¶
Index ¶
- Variables
- func Check(prefix string, err error)
- func Find(err error, matcher func(err error) bool) error
- func FindFirstMatching(err error, matcher func(err error) bool) errordeprecated
- func HasAny(err error, cause error) booldeprecated
- func Is(err error, cause error) bool
- func IsClientSideNetworkError(err error) bool
- func IsShuttingDown() bool
- func Retry(retries uint64, f func(ctx context.Context) error) error
- func RetryContext(ctx context.Context, retries uint64, f func(ctx context.Context) error) error
- func SetupSignalHandler(gracefulShutdownDelay time.Duration) <-chan os.Signal
- func Status(code codes.Code, message string) error
- func Statusf(code codes.Code, format string, args ...interface{}) error
- func Walk(err error, processor func(err error) (bool, error)) error
- func Wrap(err error, message string) error
- func WrapCode(code codes.Code, err error, message string) error
- func Wrapf(err error, format string, args ...interface{}) error
- func WrapfCode(code codes.Code, err error, format string, args ...interface{}) error
- func WriteError(ctx context.Context, w http.ResponseWriter, message string, err error)
- type ErrorCode
- type ErrorResponse
- func HTTPErrorFromStatus(status int, ctx context.Context, cause error, code ErrorCode, ...) *ErrorResponse
- func InvalidJSONError(ctx context.Context, err error) *ErrorResponse
- func MissingBodyError(ctx context.Context) *ErrorResponse
- func RequestValidationError(ctx context.Context, errors url.Values) *ErrorResponse
- func ServiceUnavailableError(ctx context.Context, cause error, serviceName string) *ErrorResponse
- func ToErrorResponse(ctx context.Context, err error) *ErrorResponse
- func UnexpectedError(ctx context.Context, cause error) *ErrorResponse
Constants ¶
This section is empty.
Variables ¶
var ( HTTPBadRequestError = newErrorClass(http.StatusBadRequest) HTTPPaymentRequiredError = newErrorClass(http.StatusPaymentRequired) HTTPForbiddenError = newErrorClass(http.StatusForbidden) HTTPNotFoundError = newErrorClass(http.StatusNotFound) HTTPMethodNotAllowedError = newErrorClass(http.StatusMethodNotAllowed) HTTPNotAcceptableError = newErrorClass(http.StatusNotAcceptable) HTTPProxyAuthRequiredError = newErrorClass(http.StatusProxyAuthRequired) HTTPRequestTimeoutError = newErrorClass(http.StatusRequestTimeout) HTTPConflictError = newErrorClass(http.StatusConflict) HTTPGoneError = newErrorClass(http.StatusGone) HTTPLengthRequiredError = newErrorClass(http.StatusLengthRequired) HTTPPreconditionFailedError = newErrorClass(http.StatusPreconditionFailed) HTTPRequestEntityTooLargeError = newErrorClass(http.StatusRequestEntityTooLarge) HTTPRequestURITooLongError = newErrorClass(http.StatusRequestURITooLong) HTTPUnsupportedMediaTypeError = newErrorClass(http.StatusUnsupportedMediaType) HTTPRequestedRangeNotSatisfiableError = newErrorClass(http.StatusRequestedRangeNotSatisfiable) HTTPExpectationFailedError = newErrorClass(http.StatusExpectationFailed) HTTPTeapotError = newErrorClass(http.StatusTeapot) HTTPUnprocessableEntityError = newErrorClass(http.StatusUnprocessableEntity) HTTPLockedError = newErrorClass(http.StatusLocked) HTTPFailedDependencyError = newErrorClass(http.StatusFailedDependency) HTTPUpgradeRequiredError = newErrorClass(http.StatusUpgradeRequired) HTTPPreconditionRequiredError = newErrorClass(http.StatusPreconditionRequired) HTTPTooManyRequestsError = newErrorClass(http.StatusTooManyRequests) HTTPRequestHeaderFieldsTooLargeError = newErrorClass(http.StatusRequestHeaderFieldsTooLarge) )
var ( HTTPInternalServerError = newErrorClass(http.StatusInternalServerError) HTTPNotImplementedError = newErrorClass(http.StatusNotImplemented) HTTPBadGatewayError = newErrorClass(http.StatusBadGateway) HTTPGatewayTimeoutError = newErrorClass(http.StatusGatewayTimeout) HTTPHTTPVersionNotSupportedError = newErrorClass(http.StatusHTTPVersionNotSupported) HTTPVariantAlsoNegotiatesError = newErrorClass(http.StatusVariantAlsoNegotiates) HTTPInsufficientStorageError = newErrorClass(http.StatusInsufficientStorage) HTTPLoopDetectedError = newErrorClass(http.StatusLoopDetected) HTTPNotExtendedError = newErrorClass(http.StatusNotExtended) HTTPNetworkAuthenticationRequiredError = newErrorClass(http.StatusNetworkAuthenticationRequired) )
var ErrorCheck = Check
Deprecated: use `Check` with `derr.Check`).
Functions ¶
func Find ¶
Find walks the error(s) stack (causes chain) and return the first error matching the `matcher` function received in argument.
Act exactly like `errors.Is` but using a matcher function instead of trying to match a particular address.
func FindFirstMatching
deprecated
func Is ¶
Is reports whether any error in err's chain matches target.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap (or Cause).
func IsClientSideNetworkError ¶
IsClientSideNetworkError returns wheter the error received is a network error caused by the client side that could not be possibily handled correctly on the server side anyway.
func IsShuttingDown ¶
func IsShuttingDown() bool
func RetryContext ¶
func SetupSignalHandler ¶
this is a graceful delay to allow residual traffic sent by the load balancer to be processed without returning 500. Once the delay has passed then the service can be shutdown
func Walk ¶
Walk traverse error causes in a top to bottom fashion. Starting from the top `err` received, will invoke `processor(err)` with it. If the `processor` returns `true`, check if `err` has a cause and continue walking it like this recursively. If `processor` return a `non-nil` value, stop walking at this point. If `processor` returns an `error` stop walking from there and bubble up the error through `Walk` return value.
Returns an `error` if `processor` returned an `error`, `nil` otherwise
func Wrapf ¶
Wrapf is a shortcut for `pkgErrors.Wrapf` (where `pkgErrors` is `github.com/pkg/errors`)
func WriteError ¶
WriteError writes the receiver error to HTTP and log it into a Zap logger at the same time with the right level based on the actual status code. The `WriteError` handles various type for the `err` parameter.
Types ¶
type ErrorResponse ¶
type ErrorResponse struct { Code ErrorCode `json:"code"` TraceID string `json:"trace_id"` Status int `json:"-"` Message string `json:"message"` Details map[string]interface{} `json:"details,omitempty"` Causer error `json:"-"` }
func HTTPErrorFromStatus ¶
func HTTPErrorFromStatus(status int, ctx context.Context, cause error, code ErrorCode, message interface{}, keyvals ...interface{}) *ErrorResponse
HTTPErrorFromStatus can be used to programmaticaly route the right status to one of the HTTP error class above
func InvalidJSONError ¶
func InvalidJSONError(ctx context.Context, err error) *ErrorResponse
func MissingBodyError ¶
func MissingBodyError(ctx context.Context) *ErrorResponse
func RequestValidationError ¶
func RequestValidationError(ctx context.Context, errors url.Values) *ErrorResponse
func ServiceUnavailableError ¶
func ServiceUnavailableError(ctx context.Context, cause error, serviceName string) *ErrorResponse
ServiceUnavailableError represents a failure at the transport layer to reach a given micro-service. Note that while `serviceName` is required, it's not directly available to final response for now, will probably encrypt it into an opaque string if you ever make usage of it
func ToErrorResponse ¶
func ToErrorResponse(ctx context.Context, err error) *ErrorResponse
ToErrorResponse turns a plain `error` interface into a proper `ErrorResponse` object. It does so with the following rules:
- If `err` is already an `ErrorResponse`, turns it into such and returns it. - If `err` was wrapped, find the most cause which is an `ErrorResponse` and returns it. - If `err` is a status.Status (or one that was wrapped), convert it to an ErrorResponse - Otherwise, return an `UnexpectedError` with the cause sets to `err` received.
func UnexpectedError ¶
func UnexpectedError(ctx context.Context, cause error) *ErrorResponse
func (*ErrorResponse) Cause ¶
func (e *ErrorResponse) Cause() error
func (*ErrorResponse) Error ¶
func (e *ErrorResponse) Error() string
func (*ErrorResponse) ResponseStatus ¶
func (e *ErrorResponse) ResponseStatus() int