Documentation ¶
Index ¶
- Constants
- Variables
- func AsCode(err error, target **Error, code string) bool
- func IsCode(err error, code string) bool
- func SetDefaultCodeKey(key string)
- func SetDefaultMessageKey(key string)
- func SetDefaultStackKey(key string)
- func UnwrapMulti(err error) []error
- type Error
- func (e *Error) Copy() *Error
- func (e *Error) Error() string
- func (e *Error) LogValue() slog.Value
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) SelfStacktrace() string
- func (e *Error) SetCodeKey(key string)
- func (e *Error) SetMessageKey(key string)
- func (e *Error) SetStackKey(key string)
- func (e *Error) Stacktrace() string
- func (e *Error) Unwrap() []error
- func (e *Error) WriteSelfStacktrace(w io.Writer) error
- func (e *Error) WriteStacktrace(w io.Writer) error
Constants ¶
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 ¶
var As = errors.As
As is a copy of the errors.As function from the go std core.
var ErrUnsupported = errors.ErrUnsupported
ErrUnsupported is a copy of the errors.ErrUnsupported variable from the go std core.
var Is = errors.Is
Is is a copy of the errors.Is function from the go std core.
var Join = errors.Join
Join is a copy of the errors.Join function from the go std core.
var Unwrap = errors.Unwrap
Unwrap is a copy of the errors.Unwrap function from the go std core.
Functions ¶
func AsCode ¶
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 ¶
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 ¶
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 ¶
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 ¶
NewUnknownf creates a new error with the ErrCodeUnknown code and the given message format/args.
func Newf ¶
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) LogValue ¶
LogValue returns a slog.Value that can be used to log the error. The default format is compatible with DataDog.
func (*Error) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. The default format is compatible with DataDog.
func (*Error) SelfStacktrace ¶
SelfStacktrace returns just the error stack trace as a string. The output us produced by calling WriteSelfStacktrace.
func (*Error) SetCodeKey ¶
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 ¶
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 ¶
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 ¶
Stacktrace returns the error stack trace as a string. The output us produced by calling WriteStacktrace.
func (*Error) WriteSelfStacktrace ¶
WriteSelfStacktrace writes just the error stack trace to the writer.