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 ¶
- Variables
- func As(err error, target interface{}) bool
- func GRPCCode(err error) codes.Code
- func HTTPCode(err error) int
- func Is(err, target error) bool
- func Join(errs ...error) error
- func ReceiveGRPCError(err error) error
- func SendGRPCError(err error) error
- func TypeCode(err error) string
- func Unwrap(err error) error
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type Error
- func (e Error) Err(err error) error
- func (e Error) Error() string
- func (e Error) GRPCCode() codes.Code
- func (e Error) GRPCStatus() *status.Status
- func (e Error) HTTPCode() int
- func (e Error) Msg(msg string) error
- func (e Error) Msgf(format string, args ...interface{}) error
- func (e Error) TypeCode() string
- func (e Error) Wrap(err error, msg string) error
- func (e Error) Wrapf(err error, format string, args ...interface{}) error
- type ErrorType
- func (*ErrorType) Descriptor() ([]byte, []int)deprecated
- func (x *ErrorType) GetGRPCCode() int64
- func (x *ErrorType) GetHTTPCode() int64
- func (x *ErrorType) GetTypeCode() string
- func (*ErrorType) ProtoMessage()
- func (x *ErrorType) ProtoReflect() protoreflect.Message
- func (x *ErrorType) Reset()
- func (x *ErrorType) String() string
- type GRPCCoder
- type HTTPCoder
- type TypeCoder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var File_errorspb_proto protoreflect.FileDescriptor
Functions ¶
func GRPCCode ¶ added in v0.0.2
GRPCCode returns the GRPC code for the given error or codes.OK when nil or codes.Unknown otherwise
func HTTPCode ¶ added in v0.0.2
HTTPCode returns the HTTP status for the given error or http.StatusOK when nil or http.StatusNotExtended otherwise
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 TypeCode ¶ added in v0.0.2
TypeCode returns the embedded type for the given error or blank when nil or UNKNOWN otherwise
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: 510 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" // HTTP: 500 GRPC: codes.Internal ErrDataLoss Error = "DATA_LOSS" // HTTP: 500 GRPC: codes.DataLoss ErrUnauthenticated Error = "UNAUTHENTICATED" // HTTP: 401 GRPC: codes.Unauthenticated )
Errors named in line with GRPC codes and some that overlap with HTTP statuses
const ( ErrBadRequest Error = "BAD_REQUEST" // HTTP: 400 GRPC: codes.InvalidArgument ErrForbidden Error = "FORBIDDEN" // HTTP: 403 GRPC: codes.PermissionDenied ErrMethodNotAllowed Error = "METHOD_NOT_ALLOWED" // HTTP: 405 GRPC: codes.Unimplemented ErrRequestTimeout Error = "REQUEST_TIMEOUT" // HTTP: 408 GRPC: codes.DeadlineExceeded ErrConflict Error = "CONFLICT" // HTTP: 409 GRPC: codes.AlreadyExists ErrGone Error = "GONE" // HTTP: 410 GRPC: codes.NotFound ErrUnsupportedMediaType Error = "UNSUPPORTED_MEDIA_TYPE" // HTTP: 415 GRPC: codes.InvalidArgument ErrImATeapot Error = "IM_A_TEAPOT" // HTTP: 418 GRPC: codes.Unknown ErrUnprocessableEntity Error = "UNPROCESSABLE_ENTITY" // HTTP: 422 GRPC: codes.InvalidArgument ErrTooManyRequests Error = "TOO_MANY_REQUESTS" // HTTP: 429 GRPC: codes.ResourceExhausted ErrInternalServerError Error = "INTERNAL_SERVER_ERROR" // HTTP: 500 GRPC: codes.Internal ErrNotImplemented Error = "NOT_IMPLEMENTED" // HTTP: 501 GRPC: codes.Unimplemented ErrBadGateway Error = "BAD_GATEWAY" // HTTP: 502 GRPC: codes.Aborted ErrGatewayTimeout Error = "GATEWAY_TIMEOUT" // HTTP: 504 GRPC: codes.DeadlineExceeded )
Errors named in line with HTTP statuses
func (Error) Err ¶ added in v0.1.4
Err overrides or adds Type,HTTP,GRPC information for the passed in error while leaving Is() and As() functionality unchanged
func (Error) GRPCStatus ¶ added in v0.1.0
type ErrorType ¶ added in v0.0.2
type ErrorType struct { TypeCode string `protobuf:"bytes,1,opt,name=TypeCode,proto3" json:"TypeCode,omitempty"` HTTPCode int64 `protobuf:"varint,2,opt,name=HTTPCode,proto3" json:"HTTPCode,omitempty"` GRPCCode int64 `protobuf:"varint,3,opt,name=GRPCCode,proto3" json:"GRPCCode,omitempty"` // contains filtered or unexported fields }
func (*ErrorType) Descriptor
deprecated
added in
v0.0.2
func (*ErrorType) GetGRPCCode ¶ added in v0.0.2
func (*ErrorType) GetHTTPCode ¶ added in v0.0.2
func (*ErrorType) GetTypeCode ¶ added in v0.0.2
func (*ErrorType) ProtoMessage ¶ added in v0.0.2
func (*ErrorType) ProtoMessage()
func (*ErrorType) ProtoReflect ¶ added in v0.0.2
func (x *ErrorType) ProtoReflect() protoreflect.Message