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.Attachmentstructures to provide additional context relevant for those different audiences. Berr providesdetail,metadata, anderrorattachments. Thedetailattachment is not sensitive, and intended for providing additional context to end-users, whilemetadataanderrorattachments 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.Errormodel will only expose the message, error type, and attachments that are not sensitive (e.g.detailattachments). -
Additional error information should be readily available. If you would like to access metadata, it is available on the
berr.Errorstruct with the.Metadata()method, and will also be output with the.FullMap()method.
berr.New(errorType berr.ErrorType, message string, details ...berr.Attachment) berr.Errorberr.Application(message string, details ...berr.Attachment) berr.Errorberr.Authentication(message string, details ...berr.Attachment) berr.Errorberr.Authorization(message string, details ...berr.Attachment) berr.Errorberr.NotFound(message string, details ...berr.Attachment) berr.Errorberr.ValueInvalid(message string, details ...berr.Attachment) berr.Errorberr.ValueMissing(message string, details ...berr.Attachment) berr.Errorberr.Unimplemented(message string, details ...berr.Attachment) berr.Errorberr.Timeout(message string, details ...berr.Attachment) berr.Error
berr.ApplicationErrorTypeberr.AuthenticationErrorTypeberr.AuthorizationErrorTypeberr.NotFoundErrorTypeberr.ValueInvalidErrorTypeberr.ValueMissingErrorTypeberr.UnimplementedErrorTypeberr.TimeoutErrorType
Error.Type() ErrorTypeError.Message() stringError.Details() map[string]anyError.Metadata() map[string]anyError.Code() intError.Map() map[string]anyError.FullMap() map[string]anyError.Unwrap()