Documentation
¶
Overview ¶
Package errors provides application error types and codes, and the request-scoped error sink that keeps a domain error's real cause behind its sanitized transport error.
The transport layer maps a domain error to a client-safe error (hiding SQL errors, stack traces, etc.) before returning it to the caller, discarding the real cause. The mapper Captures the real error into the request's sink (WithSink), and the request-logging interceptor reads it back with Captured after the handler returns — one log line per request, with the true cause, without every layer logging its own copy.
Index ¶
- Constants
- func Append(left, right error) error
- func As(err error, target any) bool
- func Capture(ctx context.Context, err error)
- func Captured(ctx context.Context) error
- func Combine(errs ...error) error
- func Errors(err error) []error
- func GRPCStatus(code ErrorCode) uint32
- func HidesMessage(code ErrorCode) bool
- func Is(err error, code ErrorCode) bool
- func IsPermanent(err error) bool
- func IsTransient(err error) bool
- func Join(errs ...error) error
- func LogError(logger *zap.Logger, msg string, err error)
- func LogErrors(logger *zap.Logger, msg string, combined error)
- func SafeMessage(code ErrorCode) string
- func Sentinel(text string) error
- func SplitCode(text string) (code, rest string, ok bool)
- func StdIs(err, target error) bool
- func ToHTTPStatus(code ErrorCode) int
- func Unwrap(err error) error
- func WithSink(ctx context.Context) context.Context
- func Wrap(err error, code ErrorCode, message string) error
- func ZapError(err error) zap.Field
- func ZapFields(err *AppError) []zap.Field
- type AppError
- func Conflict(message string) *AppError
- func Forbidden(message string) *AppError
- func Ingestion(message string) *AppError
- func Internal(message string) *AppError
- func InvalidInput(message string) *AppError
- func New(code ErrorCode, message string) *AppError
- func NotFound(message string) *AppError
- func QualityFailed(message string) *AppError
- func Timeout(message string) *AppError
- func Unauthorized(message string) *AppError
- func Unavailable(message string) *AppError
- func Upstream(message string) *AppError
- func WithDetails(err *AppError, details map[string]string) *AppError
- type ErrorCode
Constants ¶
const ( // ErrUnknown aliases CodeUnknown. ErrUnknown = CodeUnknown // ErrInternal aliases CodeInternal. ErrInternal = CodeInternal // ErrNotFound aliases CodeNotFound. ErrNotFound = CodeNotFound ErrUnauthorized = CodeUnauthorized // ErrForbidden aliases CodeForbidden. ErrForbidden = CodeForbidden // ErrInvalidInput aliases CodeInvalidInput. ErrInvalidInput = CodeInvalidInput // ErrConflict aliases CodeConflict. ErrConflict = CodeConflict // ErrTimeout aliases CodeTimeout. ErrTimeout = CodeTimeout ErrUnavailable = CodeUnavailable // ErrIngestion aliases CodeIngestion. ErrIngestion = CodeIngestion // ErrQualityFailed aliases CodeQualityFailed. ErrQualityFailed = CodeQualityFailed )
Aliases of the error codes (codes.go), kept for callers that name them so.
Variables ¶
This section is empty.
Functions ¶
func Append ¶
Append appends right into left, returning the combined error. Either value may be nil, in which case the other is returned unchanged. It delegates to multierr.Append.
func As ¶
As finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true. Re-export of errors.As.
func Capture ¶ added in v0.0.7
Capture records err as the real error for the current request when a sink is present. It is a no-op if no sink was installed, so callers need not know whether they run inside a request.
func Captured ¶ added in v0.0.7
Captured returns the real error recorded for the current request, or nil if none was captured (or no sink is installed).
func Combine ¶
Combine merges multiple errors into a single error. nil errors are discarded. If all errors are nil, it returns nil. It delegates to multierr.Combine.
func Errors ¶
Errors extracts individual errors from a combined error. If the error is not a combined error, returns a single-element slice.
func GRPCStatus ¶ added in v0.0.31
GRPCStatus returns the gRPC / Connect status number for code; a code the taxonomy does not declare is INTERNAL (13).
func HidesMessage ¶ added in v0.0.31
HidesMessage reports whether the REST edge hides the own message of an error carrying code; a code the taxonomy does not declare is shown.
func IsPermanent ¶
IsPermanent returns true if the error represents a permanent failure that will not succeed on retry (not found, invalid input, unauthorized, forbidden, conflict). CodeResourceExhausted is not permanent: the request succeeds once the quota window resets.
func IsTransient ¶
IsTransient returns true if the error represents a transient failure that may succeed on retry (timeout, unavailable). CodeResourceExhausted is deliberately not transient: a spent quota does not recover within a retry backoff, only when its window resets. The retry class of each code is the taxonomy's (codes.go).
func Join ¶
Join returns an error that wraps the given errors. Any nil error values are discarded. Re-export of errors.Join.
func LogError ¶
LogError logs a single error with structured AppError fields. Transient errors (timeout, unavailable) are logged at Warn level. All other errors are logged at Error level.
func LogErrors ¶
LogErrors logs a combined multi-error, emitting one log entry per individual error. Each error is logged via LogError with its own structured fields.
func SafeMessage ¶ added in v0.0.31
SafeMessage returns code's fixed client-safe message, or "" when the code has none and an edge sends its generic internal message instead.
func Sentinel ¶
Sentinel creates a simple sentinel error value with the given message. Use this instead of importing the standard "errors" package for errors.New. For application errors with error codes, use New() instead.
func SplitCode ¶ added in v0.0.19
SplitCode splits a coded error's text, AppError.Error()'s "<CODE>: <rest>" form, into the code and the rest. ok is false when text has no ": " separator or its prefix is not code-shaped, so an uncoded message is never mistaken for a coded one. It is the one parser for that form, shared by a display edge that drops the code and a caller that recovers it.
func StdIs ¶
StdIs reports whether any error in err's tree matches target. Re-export of errors.Is. Named StdIs to avoid collision with the package-level Is(err, ErrorCode) function.
func ToHTTPStatus ¶
ToHTTPStatus maps an ErrorCode to the corresponding HTTP status code; a code the taxonomy does not declare is 500.
func Unwrap ¶
Unwrap returns the result of calling the Unwrap method on err. Re-export of errors.Unwrap.
func WithSink ¶ added in v0.0.7
WithSink returns a context carrying a fresh error sink. The request-logging interceptor installs one at the start of every request.
Types ¶
type AppError ¶
type AppError struct {
// Err is the underlying wrapped error, if any.
Err error
// Details holds optional key-value context about the error.
Details map[string]string
// Code is the application-level error code.
Code ErrorCode
// Message is a human-readable description of the error.
Message string
// contains filtered or unexported fields
}
AppError represents an application error with code and context.
func InvalidInput ¶
InvalidInput creates an invalid input error.
func QualityFailed ¶
QualityFailed creates a quality gate failure error.
func Unauthorized ¶
Unauthorized creates an unauthorized error.
func Unavailable ¶
Unavailable creates an unavailable error.
func WithDetails ¶
WithDetails returns a copy of the error with additional details.
func (*AppError) MarshalLogObject ¶
func (e *AppError) MarshalLogObject(enc zapcore.ObjectEncoder) error
MarshalLogObject implements zapcore.ObjectMarshaler for AppError. Encodes code, message, the wrapped cause, the origin stack, and details (nested object). The cause and stack are what make a wrapped error diagnosable: without them, a generic "failed to query database" hides the real underlying failure (e.g. a Postgres "column does not exist").
func (*AppError) StackTrace ¶
StackTrace renders the captured origin stack as a newline-separated "func\n\tfile:line" trace, skipping the leading frames that belong to this errors package so the trace starts at the application code that created the error. Returns an empty string when no stack was captured.
type ErrorCode ¶
type ErrorCode string
ErrorCode represents application error codes.
const ( // CodeUnknown is the fallback for an error that is not an AppError or carries no code: // HTTP 500, gRPC INTERNAL; neither transient nor permanent. CodeUnknown ErrorCode = "UNKNOWN" // CodeInternal is an unexpected server-side failure: HTTP 500, gRPC INTERNAL; neither // transient nor permanent. CodeInternal ErrorCode = "INTERNAL" // CodeNotFound is a missing resource: HTTP 404, gRPC NOT_FOUND; permanent. CodeNotFound ErrorCode = "NOT_FOUND" // UNAUTHENTICATED; permanent. CodeUnauthorized ErrorCode = "UNAUTHORIZED" // CodeForbidden is an authenticated caller lacking permission for the action: HTTP 403, // gRPC PERMISSION_DENIED; permanent. CodeForbidden ErrorCode = "FORBIDDEN" // CodeInvalidInput is a malformed or otherwise rejected request payload: HTTP 400, gRPC // INVALID_ARGUMENT; permanent. CodeInvalidInput ErrorCode = "INVALID_INPUT" // CodeConflict is a state conflict, such as a duplicate or a concurrent modification: // HTTP 409, gRPC ALREADY_EXISTS; permanent. CodeConflict ErrorCode = "CONFLICT" // CodeTimeout is an operation that exceeded its deadline: HTTP 504, gRPC // DEADLINE_EXCEEDED; transient. CodeTimeout ErrorCode = "TIMEOUT" // CodeCanceled is an operation the caller canceled: HTTP 500, gRPC CANCELLED; neither // transient nor permanent. CodeCanceled ErrorCode = "CANCELED" // transient. CodeUnavailable ErrorCode = "UNAVAILABLE" // CodeIngestion is a failure in the document ingestion pipeline: HTTP 500, gRPC // INTERNAL; neither transient nor permanent. CodeIngestion ErrorCode = "INGESTION_ERROR" // CodeQualityFailed is a failed quality gate: HTTP 500, gRPC INTERNAL; neither transient // nor permanent. CodeQualityFailed ErrorCode = "QUALITY_FAILED" // CodeUpstream is a failure originating in an upstream dependency: HTTP 502, gRPC // UNAVAILABLE; neither transient nor permanent. CodeUpstream ErrorCode = "UPSTREAM" // CodeResourceExhausted is a request rejected because a quota or budget is spent: HTTP // 429, gRPC RESOURCE_EXHAUSTED; neither transient nor permanent. // // It is neither transient (retrying before the window resets only burns the quota check) // nor permanent (the request succeeds once the window resets). CodeResourceExhausted ErrorCode = "RESOURCE_EXHAUSTED" )
The error codes, one per row of taxonomy/error-codes.yaml.
func Code ¶
Code extracts the ErrorCode from an error. Returns CodeUnknown if the error is not an AppError.
func CodeOr ¶
CodeOr extracts err's ErrorCode like Code, but returns fallback when err carries no code (an uncoded third-party error, or nil). Use it to wrap a client error without discarding a code the client already chose.
func Codes ¶ added in v0.0.31
func Codes() []ErrorCode
Codes returns every error code, in taxonomy order.
func ContextCode ¶
ContextCode extracts err's ErrorCode like CodeOr, but first maps an uncoded context error in the chain: context.DeadlineExceeded to CodeTimeout and context.Canceled to CodeCanceled. Use it to wrap the error of a wait that a context ended (ctx.Err(), a library's deadline error) with the code that keeps retry classification and the transport status right.