Documentation
¶
Overview ¶
Package errors defines the error taxonomy shared by every frontend of the kit: one coded error drives the CLI exit code, the HTTP status code, and the MCP JSON-RPC error code alike, so transport implementations never need to interpret command-specific error strings.
Two layers of identity ride on one error:
- Kind — the coarse taxonomy (invalid_input, not_found, …). It is what frontends translate into an HTTP status / CLI exit code / JSON-RPC code. Stable and small; frontends switch on it.
- Code — an optional free-form business identifier ("USER_NOT_FOUND", "QUOTA_EXCEEDED"). It never affects transport mapping; it is carried verbatim to the caller so clients can branch on domain semantics without parsing messages.
On top of those, an error may carry Detail (structured key/value context) and an explicit Status override (HTTP only). The simplest possible handler error — errors.New("boom") or fmt.Errorf(...) — needs none of this: it is classified KindInternal and rendered with its message. Richness is opt-in and composes:
return errors.New("boom") // 最简
return errs.NotFound("user %s", id) // 带 Kind
return errs.NotFound("user %s", id). // 带业务标识符
WithCode("USER_NOT_FOUND").WithDetail("user_id", id)
return errs.Wrap(errs.KindUnavailable, err).WithCode("DB_DOWN")
Index ¶
- func Cause(err error) error
- func Code(err error) string
- func Detail(err error) map[string]any
- func Errorf(kind Kind, format string, a ...any) error
- func ExitCode(k Kind) int
- func HTTPStatus(k Kind) int
- func JSONRPCCode(k Kind) int
- func New(kind Kind, msg string) error
- func RPCCodeFor(err error) int
- func StatusFor(err error) int
- func Wrap(kind Kind, cause error) error
- func WrapMsg(kind Kind, cause error, msg string) error
- type Body
- type CodedError
- func Canceled(format string, a ...any) *CodedError
- func Conflict(format string, a ...any) *CodedError
- func Err(kind Kind, format string, a ...any) *CodedError
- func Forbidden(format string, a ...any) *CodedError
- func From(err error) *CodedError
- func Internal(format string, a ...any) *CodedError
- func InvalidInput(format string, a ...any) *CodedError
- func NotFound(format string, a ...any) *CodedError
- func Unauthorized(format string, a ...any) *CodedError
- func Unavailable(format string, a ...any) *CodedError
- func (e *CodedError) Error() string
- func (e *CodedError) Unwrap() error
- func (e *CodedError) WithCause(err error) *CodedError
- func (e *CodedError) WithCode(code string) *CodedError
- func (e *CodedError) WithDetail(key string, value any) *CodedError
- func (e *CodedError) WithDetails(m map[string]any) *CodedError
- func (e *CodedError) WithStatus(status int) *CodedError
- type Kind
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Detail ¶ added in v0.4.2
Detail returns the structured detail of the first coded layer (nil if none).
func HTTPStatus ¶
HTTPStatus maps a Kind to its natural HTTP status code.
func JSONRPCCode ¶
JSONRPCCode maps a Kind to a JSON-RPC 2.0 error code. Standard codes are negative; application-defined server errors live in the -32000..-32099 range both JSON-RPC and MCP reserve for this purpose.
func RPCCodeFor ¶ added in v0.4.2
RPCCodeFor returns the JSON-RPC error code for an error (Kind-derived).
func StatusFor ¶ added in v0.4.2
StatusFor returns the HTTP status for an error: the explicit Status override when set, otherwise the status derived from its Kind. A non-coded error classifies as KindInternal (500).
Types ¶
type Body ¶ added in v0.4.2
type Body struct {
Error string `json:"error"`
Kind Kind `json:"kind,omitempty"`
Code string `json:"code,omitempty"`
Detail map[string]any `json:"detail,omitempty"`
}
Body is the enriched machine-readable form of an error, shared by every frontend so HTTP bodies, CLI machine-mode stderr and MCP _meta agree on one shape. The flat Error string keeps the pre-v0.4.2 HTTP contract ({"error":"<message>"}) intact for existing clients; Kind/Code/Detail are additive.
type CodedError ¶
type CodedError struct {
Kind Kind
Message string
// Code is a free-form business identifier (e.g. "USER_NOT_FOUND").
// It is carried verbatim to callers and never affects transport mapping.
Code string
// Detail is structured context attached to the error. It is rendered
// into the HTTP error body and the MCP result _meta as-is.
Detail map[string]any
// Status overrides the HTTP status derived from Kind (0 = derive).
// It only affects the HTTP frontend.
Status int
// contains filtered or unexported fields
}
CodedError wraps a cause with a Kind, an optional message, an optional business Code, structured Detail and an optional HTTP Status override. Its methods are chainable and return the same pointer, so a handler can build a rich error in one expression.
func Canceled ¶ added in v0.4.2
func Canceled(format string, a ...any) *CodedError
func Conflict ¶ added in v0.4.2
func Conflict(format string, a ...any) *CodedError
func Err ¶ added in v0.4.2
func Err(kind Kind, format string, a ...any) *CodedError
Err builds a chainable *CodedError with a formatted message and no cause. It is the constructor meant for one-expression rich errors:
return errs.Err(errs.KindConflict, "%s exists", name).WithCode("DUPLICATE")
func Forbidden ¶ added in v0.4.2
func Forbidden(format string, a ...any) *CodedError
func From ¶ added in v0.4.2
func From(err error) *CodedError
From extracts the first *CodedError in the chain, or nil when the error carries no coded layer (a plain errors.New / fmt.Errorf). Frontends use it to read Code/Detail/Status without re-walking the chain.
func Internal ¶ added in v0.4.2
func Internal(format string, a ...any) *CodedError
func InvalidInput ¶ added in v0.4.2
func InvalidInput(format string, a ...any) *CodedError
Per-Kind shortcuts. Each returns a chainable *CodedError so the common "kind + message + optional code/detail" reads in one line.
func NotFound ¶ added in v0.4.2
func NotFound(format string, a ...any) *CodedError
func Unauthorized ¶ added in v0.4.2
func Unauthorized(format string, a ...any) *CodedError
func Unavailable ¶ added in v0.4.2
func Unavailable(format string, a ...any) *CodedError
func (*CodedError) Error ¶
func (e *CodedError) Error() string
func (*CodedError) Unwrap ¶
func (e *CodedError) Unwrap() error
Unwrap exposes the cause for errors.Is / errors.As.
func (*CodedError) WithCause ¶ added in v0.4.2
func (e *CodedError) WithCause(err error) *CodedError
WithCause attaches (or replaces) the underlying cause. Chainable.
func (*CodedError) WithCode ¶ added in v0.4.2
func (e *CodedError) WithCode(code string) *CodedError
WithCode attaches a free-form business identifier. Chainable.
func (*CodedError) WithDetail ¶ added in v0.4.2
func (e *CodedError) WithDetail(key string, value any) *CodedError
WithDetail attaches one structured detail entry. Chainable; a nil Detail map is created on first use.
func (*CodedError) WithDetails ¶ added in v0.4.2
func (e *CodedError) WithDetails(m map[string]any) *CodedError
WithDetails merges a batch of structured detail entries. Chainable.
func (*CodedError) WithStatus ¶ added in v0.4.2
func (e *CodedError) WithStatus(status int) *CodedError
WithStatus overrides the HTTP status derived from Kind (HTTP only). Chainable.
type Kind ¶
type Kind string
Kind classifies an error. Frontends translate a Kind into their own error representation via HTTPStatus, ExitCode and JSONRPCCode.
const ( // KindInvalidInput means the caller provided malformed or invalid // arguments (missing required fields, failed validation, bad enums). KindInvalidInput Kind = "invalid_input" KindUnauthorized Kind = "unauthorized" // KindForbidden means the caller authenticated but lacks permission. KindForbidden Kind = "forbidden" // KindNotFound means the target of the operation does not exist. KindNotFound Kind = "not_found" // KindConflict means the operation collides with existing state. KindConflict Kind = "conflict" // KindCanceled means the operation was canceled by the caller. KindCanceled Kind = "canceled" KindUnavailable Kind = "unavailable" // KindInternal is the fallback for unclassified failures. KindInternal Kind = "internal" // KindNone is what Classify returns for a nil error. KindNone Kind = "" )