Documentation ¶
Overview ¶
Package errors implements rich errors that carry contextual information such as stack traces, causality and attributes.
Example ¶
package main import ( "fmt" "go.thethings.network/lorawan-stack/pkg/errors" "go.thethings.network/lorawan-stack/pkg/ttnpb" ) func main() { var errApplicationNotFound = errors.DefineNotFound( "application_not_found", "Application with ID `{id}` not found", // Public attribute "id" is parsed from the message format. ) var errCouldNotCreateDevice = errors.Define( "could_not_create_device", "Could not create Device", "right_answer", // right_answer could be some extra attribute (that isn't rendered in the message format) ) findApplication := func(id *ttnpb.ApplicationIdentifiers) (*ttnpb.Application, error) { // try really hard, but fail return nil, errApplicationNotFound.WithAttributes("id", id.ApplicationID) } createDevice := func(dev *ttnpb.EndDevice) error { app, err := findApplication(&dev.ApplicationIdentifiers) if err != nil { return err // you can just pass errors up } // create device _ = app return nil } if err := createDevice(&ttnpb.EndDevice{}); err != nil { fmt.Println(errCouldNotCreateDevice.WithCause(err).WithAttributes("right_answer", 42)) } }
Output: error:pkg/errors_test:could_not_create_device (Could not create Device)
Index ¶
- Variables
- func Attributes(err ...error) map[string]interface{}
- func Cause(err error) error
- func Code(err error) uint32
- func Details(err error) []proto.Message
- func FromHTTP(resp *http.Response) error
- func HasCode(err error, c uint32) bool
- func IsAborted(err error) bool
- func IsAlreadyExists(err error) bool
- func IsCanceled(err error) bool
- func IsDataLoss(err error) bool
- func IsDeadlineExceeded(err error) bool
- func IsFailedPrecondition(err error) bool
- func IsInternal(err error) bool
- func IsInvalidArgument(err error) bool
- func IsNotFound(err error) bool
- func IsPermissionDenied(err error) bool
- func IsResourceExhausted(err error) bool
- func IsUnauthenticated(err error) bool
- func IsUnavailable(err error) bool
- func IsUnknown(err error) bool
- func PublicAttributes(err ...error) map[string]interface{}
- func Resemble(a, b error) bool
- func RootCause(err error) error
- func Stack(err error) (stack []error)
- func StreamClientInterceptor() grpc.StreamClientInterceptor
- func StreamServerInterceptor() grpc.StreamServerInterceptor
- func ToHTTP(in error, w http.ResponseWriter) error
- func ToHTTPStatusCode(err error) int
- func UnaryClientInterceptor() grpc.UnaryClientInterceptor
- func UnaryServerInterceptor() grpc.UnaryServerInterceptor
- type Definition
- func Define(name, messageFormat string, publicAttributes ...string) Definition
- func DefineAborted(name, messageFormat string, publicAttributes ...string) Definition
- func DefineAlreadyExists(name, messageFormat string, publicAttributes ...string) Definition
- func DefineCorruption(name, messageFormat string, publicAttributes ...string) Definition
- func DefineDataLoss(name, messageFormat string, publicAttributes ...string) Definition
- func DefineFailedPrecondition(name, messageFormat string, publicAttributes ...string) Definition
- func DefineInternal(name, messageFormat string, publicAttributes ...string) Definition
- func DefineInvalidArgument(name, messageFormat string, publicAttributes ...string) Definition
- func DefineNotFound(name, messageFormat string, publicAttributes ...string) Definition
- func DefinePermissionDenied(name, messageFormat string, publicAttributes ...string) Definition
- func DefineResourceExhausted(name, messageFormat string, publicAttributes ...string) Definition
- func DefineUnauthenticated(name, messageFormat string, publicAttributes ...string) Definition
- func DefineUnavailable(name, messageFormat string, publicAttributes ...string) Definition
- func DefineUnimplemented(name, messageFormat string, publicAttributes ...string) Definition
- func (d Definition) Attributes() map[string]interface{}
- func (d Definition) Cause() error
- func (d Definition) Code() uint32
- func (d Definition) CorrelationID() string
- func (d Definition) Details() []proto.Message
- func (d Definition) Error() string
- func (d Definition) FormatMessage(attributes map[string]interface{}) string
- func (d Definition) FullName() string
- func (d Definition) GRPCStatus() *status.Status
- func (d Definition) MarshalJSON() ([]byte, error)
- func (d Definition) MessageFormat() string
- func (d Definition) Name() string
- func (d Definition) Namespace() string
- func (d Definition) New() Error
- func (d Definition) PublicAttributes() map[string]interface{}
- func (d Definition) String() string
- func (d *Definition) UnmarshalJSON(data []byte) error
- func (d Definition) WithAttributes(kv ...interface{}) Error
- func (d Definition) WithCause(cause error) Error
- func (d Definition) WithDetails(details ...proto.Message) Error
- type DefinitionInterface
- type Error
- func (e Error) Attributes() map[string]interface{}
- func (e Error) Cause() error
- func (e Error) Code() uint32
- func (e Error) CorrelationID() string
- func (e Error) Details() (details []proto.Message)
- func (e Error) Error() string
- func (e Error) Fields() map[string]interface{}
- func (e *Error) GRPCStatus() *status.Status
- func (e Error) MarshalJSON() ([]byte, error)
- func (e Error) PublicAttributes() map[string]interface{}
- func (s Error) StackTrace() errors.StackTrace
- func (e Error) String() string
- func (e *Error) UnmarshalJSON(data []byte) error
- func (e Error) WithAttributes(kv ...interface{}) Error
- func (e Error) WithCause(cause error) Error
- func (e Error) WithDetails(details ...proto.Message) Error
- type ErrorDetails
- type Interface
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Definitions = make(map[string]*Definition)
Definitions of registered errors. Errors that are defined in init() funcs will be collected for translation.
var ErrorDetailsFromProto func(msg ...proto.Message) (details ErrorDetails, rest []proto.Message)
ErrorDetailsFromProto ranges over the given protobuf-encoded messages to extract the ErrorDetails. It returns details if present, as well as the rest of the details.
This variable is set by pkg/ttnpb.
var ErrorDetailsToProto func(e ErrorDetails) (msg proto.Message)
ErrorDetailsToProto converts the given ErrorDetails into a protobuf-encoded message.
This variable is set by pkg/ttnpb.
var JSONCodec interface { Marshal(v interface{}) ([]byte, error) Unmarshal(data []byte, v interface{}) error } = jsonpb.TTN()
JSONCodec can be used to override the default gogoproto/jsonpb.
Functions ¶
func Attributes ¶
Attributes returns the attributes of the errors, if they implement Attributes(). If more than one error is passed, subsequent error attributes will be added if not set.
func Code ¶
Code gets the code of an error. If the error doesn't implement codes, Unknown is returned.
func HasCode ¶
HasCode returns whether the given error has the given error code. If the error doesn't implement codes, it doesn't have any code.
func IsAlreadyExists ¶
IsAlreadyExists returns whether the given error is of type AlreadyExists.
func IsCanceled ¶
IsCanceled returns whether the givenerror is context.Canceled or of type Canceled.
func IsDataLoss ¶
IsDataLoss returns whether the given error is of type DataLoss.
func IsDeadlineExceeded ¶
IsDeadlineExceeded returns whether the givenerror is context.DeadlineExceeded or of type DeadlineExceeded.
func IsFailedPrecondition ¶
IsFailedPrecondition returns whether the given error is of type FailedPrecondition.
func IsInternal ¶
IsInternal returns whether the given error is of type Internal.
func IsInvalidArgument ¶
IsInvalidArgument returns whether the given error is of type InvalidArgument.
func IsNotFound ¶
IsNotFound returns whether the given error is of type NotFound.
func IsPermissionDenied ¶
IsPermissionDenied returns whether the given error is of type PermissionDenied.
func IsResourceExhausted ¶
IsResourceExhausted returns whether the given error is of type ResourceExhausted.
func IsUnauthenticated ¶
IsUnauthenticated returns whether the given error is of type Unauthenticated.
func IsUnavailable ¶
IsUnavailable returns whether the given error is of type Unavailable.
func PublicAttributes ¶
PublicAttributes returns the public attributes of the errors, if they implement PublicAttributes(). If more than one error is passed, subsequent error attributes will be added if not set.
func Resemble ¶
Resemble returns true iff the given errors resemble, meaning that the Code, Namespace and Name of the errors are equal. A nil error only resembles nil. Invalid errors or definitions (including typed nil) never resemble anything.
func StreamClientInterceptor ¶
func StreamClientInterceptor() grpc.StreamClientInterceptor
StreamClientInterceptor converts gRPC errors to regular errors.
func StreamServerInterceptor ¶
func StreamServerInterceptor() grpc.StreamServerInterceptor
StreamServerInterceptor makes sure that returned TTN errors contain a CorrelationID.
func ToHTTP ¶
func ToHTTP(in error, w http.ResponseWriter) error
ToHTTP writes the error to the HTTP response.
func ToHTTPStatusCode ¶
ToHTTPStatusCode maps an error to HTTP response codes.
func UnaryClientInterceptor ¶
func UnaryClientInterceptor() grpc.UnaryClientInterceptor
UnaryClientInterceptor converts gRPC errors to regular errors.
func UnaryServerInterceptor ¶
func UnaryServerInterceptor() grpc.UnaryServerInterceptor
UnaryServerInterceptor makes sure that returned TTN errors contain a CorrelationID.
Types ¶
type Definition ¶
type Definition struct {
// contains filtered or unexported fields
}
Definition of a registered error.
func Define ¶
func Define(name, messageFormat string, publicAttributes ...string) Definition
Define defines a registered error of type Unknown.
func DefineAborted ¶
func DefineAborted(name, messageFormat string, publicAttributes ...string) Definition
DefineAborted defines a registered error of type Aborted.
func DefineAlreadyExists ¶
func DefineAlreadyExists(name, messageFormat string, publicAttributes ...string) Definition
DefineAlreadyExists defines a registered error of type AlreadyExists.
func DefineCorruption ¶
func DefineCorruption(name, messageFormat string, publicAttributes ...string) Definition
DefineCorruption is the same as DefineDataLoss.
func DefineDataLoss ¶
func DefineDataLoss(name, messageFormat string, publicAttributes ...string) Definition
DefineDataLoss defines a registered error of type DataLoss.
func DefineFailedPrecondition ¶
func DefineFailedPrecondition(name, messageFormat string, publicAttributes ...string) Definition
DefineFailedPrecondition defines a registered error of type FailedPrecondition. Use Unavailable if the client can retry just the failing call. Use Aborted if the client should retry at a higher-level.
func DefineInternal ¶
func DefineInternal(name, messageFormat string, publicAttributes ...string) Definition
DefineInternal defines a registered error of type Internal.
func DefineInvalidArgument ¶
func DefineInvalidArgument(name, messageFormat string, publicAttributes ...string) Definition
DefineInvalidArgument defines a registered error of type InvalidArgument.
func DefineNotFound ¶
func DefineNotFound(name, messageFormat string, publicAttributes ...string) Definition
DefineNotFound defines a registered error of type NotFound.
func DefinePermissionDenied ¶
func DefinePermissionDenied(name, messageFormat string, publicAttributes ...string) Definition
DefinePermissionDenied defines a registered error of type PermissionDenied.
It should be used when a client attempts to perform an authorized action using incorrect credentials or credentials with insufficient rights. If the client attempts to perform the action without providing any form of authentication, Unauthenticated should be used instead.
func DefineResourceExhausted ¶
func DefineResourceExhausted(name, messageFormat string, publicAttributes ...string) Definition
DefineResourceExhausted defines a registered error of type ResourceExhausted.
func DefineUnauthenticated ¶
func DefineUnauthenticated(name, messageFormat string, publicAttributes ...string) Definition
DefineUnauthenticated defines a registered error of type Unauthenticated. It should be used when a client attempts to perform an authenticated action without providing any form of authentication. If the client attempts to perform the action using incorrect credentials or credentials with insufficient rights, PermissionDenied should be used instead.
func DefineUnavailable ¶
func DefineUnavailable(name, messageFormat string, publicAttributes ...string) Definition
DefineUnavailable defines a registered error of type Unavailable.
func DefineUnimplemented ¶
func DefineUnimplemented(name, messageFormat string, publicAttributes ...string) Definition
Unimplemented defines a registered error of type Unimplemented.
func (Definition) Attributes ¶
func (d Definition) Attributes() map[string]interface{}
Attributes are not present in the error definition, so this just returns nil.
func (Definition) Cause ¶
func (d Definition) Cause() error
Cause returns ret root cause of the error, in this case the descriptor itself.
func (Definition) Code ¶
func (d Definition) Code() uint32
Code of the error. This code is consistent with google.golang.org/genproto/googleapis/rpc/code and google.golang.org/grpc/codes.
func (Definition) CorrelationID ¶
func (d Definition) CorrelationID() string
CorrelationID is not present in the error definition, so this just returns an empty string.
func (Definition) Details ¶
func (d Definition) Details() []proto.Message
Details are not present in the error definition, so this just returns nil.
func (Definition) FormatMessage ¶
func (d Definition) FormatMessage(attributes map[string]interface{}) string
FormatMessage formats the message using the given attributes.
func (Definition) FullName ¶
func (d Definition) FullName() string
FullName returns the full name (namespace:name) of the error.
func (Definition) GRPCStatus ¶
func (d Definition) GRPCStatus() *status.Status
GRPCStatus returns the Definition as a gRPC status message.
func (Definition) MarshalJSON ¶
func (d Definition) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (Definition) MessageFormat ¶
func (d Definition) MessageFormat() string
MessageFormat of the error.
func (Definition) New ¶
func (d Definition) New() Error
New returns a new error from the definition. This is not required, but will add a stack trace for improved debugging.
func (Definition) PublicAttributes ¶
func (d Definition) PublicAttributes() map[string]interface{}
PublicAttributes are not present in the error definition, so this just returns nil.
func (Definition) String ¶
func (d Definition) String() string
func (*Definition) UnmarshalJSON ¶
func (d *Definition) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler.
This func is purely implemented for consistency. In practice, you probably want to unmarshal into an *Error instead of a *Definition.
func (Definition) WithAttributes ¶
func (d Definition) WithAttributes(kv ...interface{}) Error
WithAttributes returns a new error from the definition, and sets the given attributes.
func (Definition) WithCause ¶
func (d Definition) WithCause(cause error) Error
WithCause returns a new error from the definition, and sets the cause of the error.
func (Definition) WithDetails ¶
func (d Definition) WithDetails(details ...proto.Message) Error
WithDetails returns a new error from the definition, and sets the given details.
type DefinitionInterface ¶
type DefinitionInterface interface { error fmt.Stringer Namespace() string Name() string FullName() string MessageFormat() string Code() uint32 }
DefinitionInterface is the interface of an error definition.
type Error ¶
type Error struct { Definition // contains filtered or unexported fields }
Error is a rich error implementation.
func From ¶
From returns an *Error if it can be derived from the given input. For a nil error, false will be returned.
func FromGRPCStatus ¶
FromGRPCStatus converts the gRPC status message into an Error.
func FromHTTPStatusCode ¶
FromHTTPStatusCode maps an HTTP response code to an error.
func New ¶
New returns an error that formats as the given text. This way of creating errors should be avoided if possible.
func (Error) Attributes ¶
Attributes of the error.
func (Error) Code ¶
Code of the error. If the code is invalid or unknown, this tries to get the code from the cause of this error. This code is consistent with google.golang.org/genproto/googleapis/rpc/code and google.golang.org/grpc/codes.
func (Error) Details ¶
Details of the error. Usually structs from ttnpb or google.golang.org/genproto/googleapis/rpc/errdetails.
func (*Error) GRPCStatus ¶
GRPCStatus converts the Error into a gRPC status message.
func (Error) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Error) PublicAttributes ¶
PublicAttributes of the error.
func (Error) StackTrace ¶
func (s Error) StackTrace() errors.StackTrace
StackTrace implements the errors.stackTracer interface.
func (*Error) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (Error) WithAttributes ¶
WithAttributes returns the error with the given attributes set. Any conflicting attributes in the Error will be overwritten.