Documentation
¶
Overview ¶
Package errors builds on Go 1.13 errors adding HTTP and GRPC code to your errors.
Wrap() and Wrapf()
When the wrap functions are used with one of the defined Err* constants you get back an error that you're able to pass the error through a GRPC server and client or use to build HTTP error messages and set the HTTP status.
Wrapping any error other than an Error will return an error with the message formatted as "<message>: <error>".
Wrapping an Error will return an error with an unaltered error message.
Transmitting errors over GRPC ¶
The errors produced with wrap, that have also been wrapped first with an Err* can be send with SendGRPCError() and received with ReceiveGRPCError().
You may want to create and use GRPC server and client interceptors to avoid having to call the Send/Receive methods in every handler.
The Err* constants are errors and can be used directly is desired.
Index ¶
- func As(err error, target interface{}) bool
- func Is(err, target error) bool
- func Message(err error) string
- func ReceiveGRPCError(err error) error
- func SendGRPCError(err error) error
- func Unwrap(err error) error
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type Error
- type GRPCCoder
- type HTTPCoder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Message ¶
Message displays the string value for Error prefixed to the existing error message
Prefixed as "Error: message"
If err is not an Error or it hasn't wrapped one then there will no modifications made to the message.
Example ¶
err := Wrap(ErrNotFound, "message") fmt.Println(Message(err))
Output: NOT_FOUND: message
func ReceiveGRPCError ¶
ReceiveGRPCError recreates the error with the coded Error reapplied
Non-nil results can be used as both Error and *status.Status. Methods errors.Is()/errors.As(), and status.Convert()/status.FromError() will continue to work.
Use in the clients when receiving errors. If err is nil then ReceiveGRPCError returns nil.
func SendGRPCError ¶
SendGRPCError ensures that the error being used is sent with the correct code applied
Use in the server when sending errors. If err is nil then SendGRPCError returns nil.
func Wrap ¶
Wrap returns an error with msg wrapped with the supplied error If err is nil then Wrap returns nil
Example ¶
err := Wrap(ErrNotFound, "message") fmt.Println(err)
Output: message
Example (Multiple) ¶
err := Wrap(ErrNotFound, "original message") err = Wrap(err, "prefixed message") fmt.Println(err)
Output: prefixed message: original message
Types ¶
type Error ¶
type Error string
Error base error
const ( ErrOK Error = "OK" // HTTP: 200 GRPC: codes.OK ErrCanceled Error = "CANCELED" // HTTP: 408 GRPC: codes.Canceled ErrUnknown Error = "UNKNOWN" // HTTP: 500 GRPC: codes.Unknown ErrInvalidArgument Error = "INVALID_ARGUMENT" // HTTP: 400 GRPC: codes.InvalidArgument ErrDeadlineExceeded Error = "DEADLINE_EXCEEDED" // HTTP: 504 GRPC: codes.DeadlineExceeded ErrNotFound Error = "NOT_FOUND" // HTTP: 404 GRPC: codes.NotFound ErrAlreadyExists Error = "ALREADY_EXISTS" // HTTP: 409 GRPC: codes.AlreadyExists ErrPermissionDenied Error = "PERMISSION_DENIED" // HTTP: 403 GRPC: codes.PermissionDenied ErrResourceExhausted Error = "RESOURCE_EXHAUSTED" // HTTP: 429 GRPC: codes.ResourceExhausted ErrFailedPrecondition Error = "FAILED_PRECONDITION" // HTTP: 400 GRPC: codes.FailedPrecondition ErrAborted Error = "ABORTED" // HTTP: 409 GRPC: codes.Aborted ErrOutOfRange Error = "OUT_OF_RANGE" // HTTP: 422 GRPC: codes.OutOfRange ErrUnimplemented Error = "UNIMPLEMENTED" // HTTP: 501 GRPC: codes.Unimplemented ErrInternal Error = "INTERNAL_ERROR" // HTTP: 500 GRPC: codes.Internal ErrDataLoss Error = "DATA_LOSS" // HTTP: 500 GRPC: codes.DataLoss ErrUnauthenticated Error = "UNAUTHENTICATED" // HTTP: 401 GRPC: codes.Unauthenticated ErrBadRequest Error = "BAD_REQUEST" // HTTP: 400 GRPC: codes.InvalidArgument ErrConflict Error = "CONFLICT" // HTTP: 409 GRPC: codes.AlreadyExists ErrForbidden Error = "FORBIDDEN" // HTTP: 403 GRPC: codes.PermissionDenied ErrUnprocessableEntity Error = "UNPROCESSABLE_ENTITY" // HTTP: 422 GRPC: codes.InvalidArgument ErrServer Error = "SERVER_ERROR" // HTTP: 500 GRPC: codes.Internal ErrClient Error = "CLIENT_ERROR" // HTTP: 400 GRPC: codes.InvalidArgument )
Errors