errors

package
v0.0.0-...-6b15219 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2023 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Package errors is a package that implements FriendlyError. FriendlyError implements the error interface, but also has a FriendlyError() method that returns a message that is reasonable to show to end-users.

Every method that returns a FriendlyError also accepts 0 to n Fields objects may be provided and they will be combined into a single Fields, with later values overwriting newer ones.

FriendlyErrors also have With* methods that return a copy of the error with the given changes. This allows a pattern where a method defines a default FriendlyError with the friendly error message to use and then when it needs to return an error uses WithError() to add the specific error message.

Index

Constants

View Source
const DefaultFriendlyError = "An error occurred"

DefaultFriendlyError is the message that will be shown by default unless a more specific friendly value is returned.

Variables

This section is empty.

Functions

func NewWrapped

func NewWrapped(err error, fields ...Fields) error

NewWrapped takes a generic error. It returns either a new error with that message or, if nil, returns nil. Returns a generic error so if it returns nil it will no be a nil interface value to a *Friendly.

Types

type Fields

type Fields map[string]interface{}

Fields is the place we store extra field/value information on an error.

type Friendly

type Friendly struct {
	// contains filtered or unexported fields
}

Friendly is the core type of the error, representing an error that knows how to render a default string and a "friendly" string, appropriate for showing to end users.

func Extend

func Extend(err error, msg string, fields ...Fields) *Friendly

Extend returns a new FriendlyError where the Error() message is prepended with this new message and a delimiter. If err is an *errors.Friendly then the secure message, friendly message, and fields will all be included unchanged.

func New

func New(msg string, fields ...Fields) *Friendly

New creates a new errors.Friendly with the given msg.

func NewFriendly

func NewFriendly(friendlyMsg string, fields ...Fields) *Friendly

NewFriendly returns a new errors.Friendly with the given FriendlyError.

func NewSecure

func NewSecure(secureMsg string, fields ...Fields) *Friendly

NewSecure returns a new errors.Friendly with the given SecureError.

func (*Friendly) Error

func (f *Friendly) Error() string

Error returns the error message, implementing the error interface. If no specific message has been provided, will fall back on FriendlyError(). The Error() value is OK to show in insecure contexts (i.e. on the client) but it just might be confusing to users.

func (*Friendly) Fields

func (f *Friendly) Fields() Fields

Fields returns the Fields object for this error. Fields should only be used in secure contexts (e.g. logging on a server) because they may contain private information.

func (*Friendly) FriendlyError

func (f *Friendly) FriendlyError() string

FriendlyError is the error string that is OK to show in insecure contexts to end-users. It is generally a much simplified version of the message. If no specific FriendlyMessage has been provided, will return DefaultFriendlyError.

func (*Friendly) SecureError

func (f *Friendly) SecureError() string

SecureError returns the error message that should only be shown in secure contexts, because it may include secret information. If no SecureError message has been provided, will return Error()

func (*Friendly) WithError

func (f *Friendly) WithError(msg string, fields ...Fields) *Friendly

WithError returns a copy of err where the Error() is set to msg. See a;so Extend, which prepends a new message to the front of the existing message.

func (*Friendly) WithFriendly

func (f *Friendly) WithFriendly(friendlyMsg string, fields ...Fields) *Friendly

WithFriendly returns a copy of err where the friendlyMsg is set to friendlyMsg

func (*Friendly) WithSecure

func (f *Friendly) WithSecure(secureMsg string, fields ...Fields) *Friendly

WithSecure returns a copy of err where the SecureError() is set to secureMsg.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL