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/v3/pkg/errors" "go.thethings.network/lorawan-stack/v3/pkg/ttnpb" ) func main() { errApplicationNotFound := errors.DefineNotFound( "application_not_found", "Application with ID `{id}` not found", // Public attribute "id" is parsed from the message format. ) 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.GetApplicationId()) } createDevice := func(dev *ttnpb.EndDevice) error { app, err := findApplication(dev.Ids.ApplicationIds) if err != nil { return err // you can just pass errors up } // create device _ = app return nil } if err := createDevice(&ttnpb.EndDevice{Ids: &ttnpb.EndDeviceIdentifiers{}}); 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]any
- func Cause(err error) error
- func Code(err error) uint32
- func Details(err error) []proto.Message
- func FromHTTP(resp *http.Response) error
- func GenerateCorrelationIDs(enable bool)
- 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 IsUnimplemented(err error) bool
- func IsUnknown(err error) bool
- func PublicAttributes(err ...error) map[string]any
- 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 DefineCanceled(name, messageFormat string, publicAttributes ...string) *Definition
- func DefineCorruption(name, messageFormat string, publicAttributes ...string) *Definition
- func DefineDataLoss(name, messageFormat string, publicAttributes ...string) *Definition
- func DefineDeadlineExceeded(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 (*Definition) Attributes() map[string]any
- func (*Definition) Cause() error
- func (d *Definition) Code() uint32
- func (*Definition) CorrelationID() string
- func (*Definition) Details() []proto.Message
- func (d *Definition) Error() string
- func (d *Definition) FormatMessage(attributes map[string]any) string
- func (d *Definition) FullName() string
- func (d *Definition) GRPCStatus() *status.Status
- func (d *Definition) Is(target error) bool
- 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 (*Definition) PublicAttributes() map[string]any
- func (d *Definition) String() string
- func (d *Definition) UnmarshalJSON(data []byte) error
- func (*Definition) Unwrap() error
- func (d *Definition) WithAttributes(kv ...any) *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]any
- 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]any
- func (e *Error) GRPCStatus() *status.Status
- func (e *Error) Is(target error) bool
- func (e *Error) MarshalJSON() ([]byte, error)
- func (*Error) New()
- func (e *Error) PublicAttributes() map[string]any
- func (s Error) StackTrace() errors.StackTrace
- func (e *Error) String() string
- func (e *Error) UnmarshalJSON(data []byte) error
- func (e *Error) Unwrap() error
- func (e *Error) WithAttributes(kv ...any) *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 ( // ErrContextCanceled is the Definition of the standard context.Cancelled error. // This variant exists in order to allow the error code to be properly propagated // over gRPC calls, otherwise the error code is unknown. ErrContextCanceled = DefineCanceled("context_canceled", "context canceled") // ErrContextDeadlineExceeded is the definition of the standard context.DeadlineExceeded error. // This variant exists in order to allow the error code to be properly propagated // over gRPC calls, otherwise the error code is unknown. ErrContextDeadlineExceeded = DefineDeadlineExceeded( "context_deadline_exceeded", "context deadline exceeded", ) )
Alias standard library error functions.
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 any) ([]byte, error) Unmarshal(data []byte, v any) error } = jsonpb.TTN()
JSONCodec can be used to override the jsonpb codec.
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 GenerateCorrelationIDs ¶ added in v3.14.2
func GenerateCorrelationIDs(enable bool)
GenerateCorrelationIDs configures whether random correlation IDs are generated for each error. This is enabled by default.
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 IsUnimplemented ¶ added in v3.9.0
IsUnimplemented returns whether the given error is of type Unimplemented.
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 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 DefineCanceled ¶ added in v3.8.2
func DefineCanceled(name, messageFormat string, publicAttributes ...string) *Definition
DefineCanceled defines a registered error of type Canceled.
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 DefineDeadlineExceeded ¶ added in v3.8.2
func DefineDeadlineExceeded(name, messageFormat string, publicAttributes ...string) *Definition
DefineDeadlineExceeded defines a registered error of type DeadlineExceeded.
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
DefineUnimplemented defines a registered error of type Unimplemented.
func (*Definition) Attributes ¶
func (*Definition) Attributes() map[string]any
Attributes are not present in the error definition, so this just returns nil.
func (*Definition) Cause ¶
func (*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 (*Definition) CorrelationID() string
CorrelationID is not present in the error definition, so this just returns an empty string.
func (*Definition) Details ¶
func (*Definition) Details() []proto.Message
Details are not present in the error definition, so this just returns nil.
func (*Definition) Error ¶
func (d *Definition) Error() string
Error implements the error interface.
func (*Definition) FormatMessage ¶
func (d *Definition) FormatMessage(attributes map[string]any) 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) Is ¶ added in v3.10.1
func (d *Definition) Is(target error) bool
Is makes the Definition implement error comparison.
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 (*Definition) PublicAttributes() map[string]any
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) Unwrap ¶ added in v3.10.1
func (*Definition) Unwrap() error
Unwrap makes the Definition implement error unwrapping.
func (*Definition) WithAttributes ¶
func (d *Definition) WithAttributes(kv ...any) *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) 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) New ¶ added in v3.29.1
func (*Error) New()
New exists in order to prevent accidentally calling Definition.New(). If you are sure that you want to create a fresh error, use .Definition.New().
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.