Documentation
¶
Overview ¶
Package errs provides a single, transport-agnostic error type with stable codes, an HTTP (and later gRPC) status mapping, request validation helpers, and automatic redaction of secrets in user-facing messages.
An *Error carries a machine-readable Code, an optional Title, a human Message (the "detail"), and optional Args used for i18n. It implements the standard error interface and unwraps to any underlying cause.
Index ¶
Constants ¶
const MaxMessageLen = 1024
MaxMessageLen bounds the length of a sanitized message to keep logs and API responses tidy.
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check validates val using struct tags (github.com/go-playground/validator). On failure it returns an *Error with code InvalidArgument whose Fields list the offending fields. On success it returns nil.
Types ¶
type Code ¶
type Code uint32
Code is a stable, transport-agnostic error classification. Its integer values are intentionally aligned with google.golang.org/grpc/codes so that gRPC mapping is an identity and HTTP mapping is a small lookup table.
const ( OK Code = 0 Canceled Code = 1 Unknown Code = 2 InvalidArgument Code = 3 DeadlineExceeded Code = 4 NotFound Code = 5 AlreadyExists Code = 6 PermissionDenied Code = 7 ResourceExhausted Code = 8 FailedPrecondition Code = 9 Aborted Code = 10 OutOfRange Code = 11 Unimplemented Code = 12 Internal Code = 13 DataLoss Code = 15 Unauthenticated Code = 16 )
Canonical error codes, value-aligned with gRPC status codes.
func (Code) HTTPStatus ¶
HTTPStatus maps the code to an HTTP status code.
type Error ¶
type Error struct {
Code Code `json:"-"`
CodeStr string `json:"code"`
Title string `json:"title,omitempty"`
Message string `json:"detail"`
Fields []FieldError `json:"fields,omitempty"`
Args map[string]any `json:"-"`
// MessageID is an optional i18n message key. When set it overrides CodeStr as
// the lookup key the i18n package uses to localize Message; Args supply the
// template data. Left empty, translation falls back to CodeStr.
MessageID string `json:"-"`
// contains filtered or unexported fields
}
Error is the canonical application error.
The JSON shape is intentionally small and stable so it can be returned directly to API clients:
{"code":"not_found","title":"...","detail":"widget 42 not found","fields":[...]}
func From ¶
From normalizes any error into an *Error. If err already is (or wraps) an *Error it is returned as-is; otherwise it becomes an Internal error.
func New ¶
New creates an *Error with the given code, wrapping err. The message is taken from err. The call site (function and file:line) is captured for logging.
func NewFieldErrors ¶
func NewFieldErrors(msg string, fields ...FieldError) *Error
NewFieldErrors builds an InvalidArgument error from an explicit field list.
func (*Error) Encode ¶
Encode renders the error as JSON, redacting any secrets in the detail. It satisfies the web encoder contract: (data, contentType, error).
func (*Error) HTTPStatus ¶
HTTPStatus returns the HTTP status code for this error.
func (*Error) WithMessageID ¶
WithMessageID sets the i18n lookup key and returns the error for chaining.
type FieldError ¶
FieldError describes a single failed validation constraint on an input field.