errors

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrCodePanic is used when a panic is caught, the original panic error will be wrapped in the new error
	ErrCodePanic = "PANIC"

	// ErrCodeUnknown is used when an error occurs that is not known
	ErrCodeUnknown = "UNKNOWN"

	// ErrCodeBadArgument is used when an argument is invalid. This should be used when the argument is provided by
	// an external source. This error will be mapped to HTTP 409.
	ErrCodeBadArgument = "BAD_ARGUMENT"

	// ErrCodeBadState is used when the application is in a state that is not expected, this can be used when
	// the configuration is invalid. This error will be mapped to HTTP 500.
	ErrCodeBadState = "BAD_STATE"

	// ErrCodeNotFound is used when a resource is not found. This error will be mapped to HTTP 404.
	ErrCodeNotFound = "NOT_FOUND"

	// ErrCodeNotAuthenticated is used when the caller is not authenticated. This error will be mapped to HTTP 401.
	ErrCodeNotAuthenticated = "NOT_AUTHENTICATED"

	// ErrCodeNotAllowed is used when the caller is not allowed to perform the action. This error will be mapped to HTTP 403.
	ErrCodeNotAllowed = "NOT_ALLOWED"

	// ErrCodeValidationFailed is used when a validation error occurs. This error will be mapped to HTTP 422.
	ErrCodeValidationFailed = "NOT_VALID"

	// ErrCodeConflict is used when there is a conflict with the current state. This error will be mapped to HTTP 409.
	ErrCodeConflict = "CONFLICT"
)

Common errors

Variables

View Source
var As = errors.As

As is a copy of the errors.As function from the go std core.

View Source
var ErrUnsupported = errors.ErrUnsupported

ErrUnsupported is a copy of the errors.ErrUnsupported variable from the go std core.

View Source
var Is = errors.Is

Is is a copy of the errors.Is function from the go std core.

View Source
var Join = errors.Join

Join is a copy of the errors.Join function from the go std core.

View Source
var Unwrap = errors.Unwrap

Unwrap is a copy of the errors.Unwrap function from the go std core.

Functions

func AsCode

func AsCode(err error, target **Error, code string) bool

AsCode returns true if the error, or any wrapped error, is of type Error and has the given code. The found error is assigned to target.

func IsCode

func IsCode(err error, code string) bool

IsCode returns true if the error, or any wrapped error, is of type Error and has the given code.

func SetDefaultCodeKey added in v0.4.0

func SetDefaultCodeKey(key string)

SetDefaultCodeKey sets the default key used to represent the error code in the JSON and slog.Value representations of the error. Empty value is not allowed

func SetDefaultMessageKey added in v0.4.0

func SetDefaultMessageKey(key string)

SetDefaultMessageKey sets the default key used to represent the error message in the JSON and slog.Value representations of the error. Empty value means do not include the message in the JSON and slog.Value representations.

func SetDefaultStackKey added in v0.4.0

func SetDefaultStackKey(key string)

SetDefaultStackKey sets the default key used to represent the error stacktrace in the JSON and slog.Value representations of the error. Empty value means do not include the stacktrace in the JSON and slog.Value representations.

func UnwrapMulti

func UnwrapMulti(err error) []error

UnwrapMulti returns all the errors that have been directly wrapped by err, if any. This is similar to Unwrap, but it also unwraps errors that implement:

interface { Unwrap() []error} }

Types

type Error

type Error struct {
	Code    string
	Message string
	// contains filtered or unexported fields
}

Error is this framework's error type. It adds on top of the go std lib errors package: - A Code field to categorize the error. - A Stacktrace that pretty prints with wrapped errors. - A Conversion to slog.Value to log the error in a structured way. - A Conversion to JSON to log the error in a structured way.

func NewUnknownf

func NewUnknownf(format string, args ...any) *Error

NewUnknownf creates a new error with the ErrCodeUnknown code and the given message format/args.

func Newf

func Newf(code string, format string, args ...any) *Error

Newf creates a new error with the given code and message format/args. The error will have a stacktrace attached to it. It follows the same rules as fmt.Errorf, where the message is formatted with fmt.Sprintf.

func (*Error) Copy added in v0.4.0

func (e *Error) Copy() *Error

Copy creates a deep copy of the error.

func (*Error) Error

func (e *Error) Error() string

func (*Error) LogValue

func (e *Error) LogValue() slog.Value

LogValue returns a slog.Value that can be used to log the error. The default format is compatible with DataDog.

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. The default format is compatible with DataDog.

func (*Error) SelfStacktrace

func (e *Error) SelfStacktrace() string

SelfStacktrace returns just the error stack trace as a string. The output us produced by calling WriteSelfStacktrace.

func (*Error) SetCodeKey

func (e *Error) SetCodeKey(key string)

SetCodeKey sets the key used to represent the error code in the JSON and slog.Value representations of the error. Empty value is not allowed

func (*Error) SetMessageKey

func (e *Error) SetMessageKey(key string)

SetMessageKey sets the key used to represent the error message in the JSON and slog.Value representations of the error. Empty value means do not include the message in the JSON and slog.Value representations.

func (*Error) SetStackKey

func (e *Error) SetStackKey(key string)

SetStackKey sets the key used to represent the error stacktrace in the JSON and slog.Value representations of the error. Empty value means do not include the stacktrace in the JSON and slog.Value representations.

func (*Error) Stacktrace

func (e *Error) Stacktrace() string

Stacktrace returns the error stack trace as a string. The output us produced by calling WriteStacktrace.

func (*Error) Unwrap

func (e *Error) Unwrap() []error

Unwrap returns the errors that have been directly wrapped by err, if any.

func (*Error) WriteSelfStacktrace

func (e *Error) WriteSelfStacktrace(w io.Writer) error

WriteSelfStacktrace writes just the error stack trace to the writer.

func (*Error) WriteStacktrace

func (e *Error) WriteStacktrace(w io.Writer) error

WriteStacktrace writes the error stack trace to the writer. It also includes the stack trace of any wrapped errors.

Jump to

Keyboard shortcuts

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