berr
is an errors package that provides simple functions for creating more descriptive errors.
go get github.com/rheisen/berr
When handling errors in Go you typically encounter either the standard library error (a string for all intents and
purposes), or a custom error struct tailored for a specific use case. The berr
package sits in between, providing
general purpose error structures that provide more descriptive errors with minimal configuration, with a host of options
for creating really powerful errors.
-
Errors need to serve the needs of different audiences, audiences with often conflicting interests (E.g. developers vs. end-users of APIs and Applications). When creating a
berr.Error
, provide a message that is safe and valuable to end-users, and addberr.Attachment
structures to provide additional context relevant for those different audiences. Berr providesdetail
,metadata
, anderror
attachments. Thedetail
attachment is not sensitive, and intended for providing additional context to end-users, whilemetadata
anderror
attachments are sensitive (e.g. intended for developers / debugging). -
Errors should be sensitive by default, and not reveal or leak sensitive information (sensitive attachments) without an explicit call to do so.
berr.Error.String()
will output the error type and the message it was initially suplied with, whileberr.Error.Error()
will output theberr.Error.String()
in addition to the output of.Error()
on the next error (if one exists). A JSON rendering of aberr.Error
model will only expose the message, error type, and attachments that are not sensitive (e.g.detail
attachments). -
Additional error information should be readily available. If you would like to access metadata, it is available on the
berr.Error
struct with the.Metadata()
method, and will also be output with the.FullMap()
method.
berr.New(errorType berr.ErrorType, message string, details ...berr.Attachment) berr.Error
berr.Application(message string, details ...berr.Attachment) berr.Error
berr.Authentication(message string, details ...berr.Attachment) berr.Error
berr.Authorization(message string, details ...berr.Attachment) berr.Error
berr.NotFound(message string, details ...berr.Attachment) berr.Error
berr.ValueInvalid(message string, details ...berr.Attachment) berr.Error
berr.ValueMissing(message string, details ...berr.Attachment) berr.Error
berr.Unimplemented(message string, details ...berr.Attachment) berr.Error
berr.Timeout(message string, details ...berr.Attachment) berr.Error
berr.ApplicationErrorType
berr.AuthenticationErrorType
berr.AuthorizationErrorType
berr.NotFoundErrorType
berr.ValueInvalidErrorType
berr.ValueMissingErrorType
berr.UnimplementedErrorType
berr.TimeoutErrorType
Error.Type() ErrorType
Error.Message() string
Error.Details() map[string]any
Error.Metadata() map[string]any
Error.Code() int
Error.Map() map[string]any
Error.FullMap() map[string]any
Error.Unwrap()