modulepackage
0.0.0-20220929234051-087d6d8bb841
Repository: https://github.com/cyberhorsey/errors.git
Documentation: pkg.go.dev
# README
Errors Utility
This package provides utility methods for error handling in Go, such as error types, context, pointer, and detail. The utility methods build on the functionality in github.com/pkg/errors.
Examples
Create a new error of a specific type, with detail:
doc, err := jsonapi.DecodeRequest(r, "account", req)
if err != nil {
return errors.BadRequest.NewWithDetail("Invalid request body")
}
Wrap an error:
err := errors.New("an_error")
err = errors.AddErrorContext(err, "field", "value")
wrappedErr := errors.BadRequest.Wrapf(err, "error %s", "1")
Handle errors of specific types with pointer/detail:
switch errors.GetType(err) {
case errors.NotFound:
status = http.StatusNotFound
title = "Not Found"
case errors.InvalidParameter:
status = http.StatusBadRequest
title = "Invalid Parameter"
case errors.MissingParameter:
status = http.StatusBadRequest
title = "Missing Parameter"
case errors.Validation, errors.Public:
title = "Resource Invalid"
status = http.StatusUnprocessableEntity
case errors.BadRequest:
title = "Bad Request"
status = http.StatusBadRequest
case errors.Forbidden:
title = "Forbidden"
status = http.StatusForbidden
default:
status = http.StatusInternalServerError
title = "Internal Server Error"
}
pointer := errors.Pointer(err)
detail := errors.Detail(err)
Add/retrieve error context:
err := errors.BadRequest.New("an_error")
errWithContext := errors.AddErrorContext(err, "field1", "the field is empty")
fmt.Println(errors.GetErrorContextValue(errWithContext, "field1"))
Get original error cause from wrapped error:
cusErr := customError{}
wrappedErr := errors.Wrap(cusErr, "wrapped customError")
wrappedErr = errors.Wrap(wrappedErr, "outer wrapped customError")
wrappedErr = errors.Wrap(wrappedErr, "outer outer wrapped customError")
causeErr := errors.Cause(wrappedErr)
originalErr, ok := causeErr.(customError)
# originalErr == cusErr
See more examples in errors_test.go.
# Functions
AddErrorContext adds a context to an error.
Cause gives the original error.
Detail returns the error detail.
GetErrorContext returns the error context.
GetErrorContextValue returns an error context value.
GetType returns the error type.
IsFailFast returns whether the error is fail fast (i.e.
Key returns the error key.
New creates a NoType error.
Newf creates a no type error with formatted message.
Pointer returns the error pointer.
WithCause wraps causeErr with err.
WithDetail adds detail to the error.
WithFailFast signifies that err is fail fast (i.e.
WithKey adds key to the error.
WithKeyAndDetail adds key and detail to a message.
WithPointer adds a pointer to the error.
Wrap an error with a string.
Wrapf an error with format string.
# Constants
BadRequest error.
Forbidden error.
InvalidParameter error.
MissingParameter error.
NotFound error.
NoType error.
Public error.
Unauthorized error.
Validation error.
# Type aliases
ErrorType is the type of an error.