Documentation
¶
Overview ¶
Package goerr is a structured error type for application-level errors that carry user-facing copy, an error code, contextual key/value data, trace correlation IDs, and a severity level. It is designed to flow from your service layer to your HTTP / gRPC layer without lossy stringification.
Two messages: the Error.Error() return is the operator-facing message (logs, alerts), the ErrorUser is the user-facing message (HTTP response body, GraphQL error). Use SetMessage / SetMessageUser, or the constructor options, to populate them independently.
Global callbacks can be registered with AddCallback to forward every error to an external sink (Sentry, Loki, Slack). Callbacks fire on Error.Error() and ErrorUser.Error() so they capture every formatted surface.
Index ¶
- func AddCallback(callback callbackFn)
- func ResetCallbacks()
- type Error
- type ErrorOption
- func WithCode(code string) ErrorOption
- func WithCtx(ctx context.Context) ErrorOption
- func WithData(data any) ErrorOption
- func WithKV(key string, value any) ErrorOption
- func WithKVMap(kv map[string]any) ErrorOption
- func WithLevel(level string) ErrorOption
- func WithMessageUser(message string) ErrorOption
- func WithTraceID(traceID string) ErrorOption
- type ErrorResult
- type ErrorUser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddCallback ¶
func AddCallback(callback callbackFn)
AddCallback registers a global hook that runs on every Error.Error() and ErrorUser.Error() invocation. Callbacks fire synchronously in registration order — long-running observers should fan out to a goroutine themselves.
func ResetCallbacks ¶
func ResetCallbacks()
ResetCallbacks removes all registered callbacks. Mainly useful for tests to keep callback state from leaking between table-driven cases.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is the structured error type. Construct with New / NewDefault and the With* options.
func New ¶
func New(message string, options ...ErrorOption) *Error
New constructs an Error with the given operator-facing message. The user- facing message defaults to the same string; override with WithMessageUser.
func NewDefault ¶
func NewDefault(options ...ErrorOption) *Error
NewDefault is New with a generic "An error occurred" message. Use it as a starting point for a per-call site that injects all its data via SetMessage / SetMessageUser at error time.
func (*Error) ErrorUser ¶
ErrorUser returns a separate sub-error that surfaces the user-facing message.
func (*Error) Response ¶
func (e *Error) Response() ErrorResult
Response returns a copy of the underlying ErrorResult.
func (*Error) SetMessage ¶
SetMessage updates the operator-facing message after construction.
func (*Error) SetMessageUser ¶
SetMessageUser updates the user-facing message after construction.
type ErrorOption ¶
type ErrorOption func(*Error)
ErrorOption configures a New / NewDefault Error.
func WithCode ¶
func WithCode(code string) ErrorOption
WithCode tags the error with a stable code identifier (e.g. "E_NOT_FOUND").
func WithCtx ¶
func WithCtx(ctx context.Context) ErrorOption
WithCtx attaches a context.Context to the error. Callbacks receive this context, so observability hooks can extract trace IDs, request IDs, and the active logger from it.
func WithData ¶
func WithData(data any) ErrorOption
WithData attaches an arbitrary payload that will appear in ToJSON output.
func WithKV ¶
func WithKV(key string, value any) ErrorOption
WithKV adds a single key/value pair to the error's KV map.
func WithKVMap ¶
func WithKVMap(kv map[string]any) ErrorOption
WithKVMap adds every entry from kv to the error's KV map. Existing keys are overwritten.
func WithLevel ¶
func WithLevel(level string) ErrorOption
WithLevel overrides the default "error" severity.
func WithMessageUser ¶
func WithMessageUser(message string) ErrorOption
WithMessageUser overrides the default user-facing message.
func WithTraceID ¶
func WithTraceID(traceID string) ErrorOption
WithTraceID sets the trace correlation ID. Typically populated from your tracing library.
type ErrorResult ¶
type ErrorResult struct {
MessageUser string `json:"messageUser,omitempty"`
Message string `json:"message,omitempty"`
Code string `json:"code,omitempty"`
Data any `json:"data,omitempty"`
TraceID string `json:"traceId,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Level string `json:"level,omitempty"`
KV map[string]any `json:"kv,omitempty"`
}
ErrorResult is the wire-format payload of an error.
func (*ErrorResult) ToJSON ¶
func (e *ErrorResult) ToJSON() string
ToJSON returns the ErrorResult marshalled as a JSON string. On marshal failure (which should be impossible for the documented field types) it returns the empty string.