Documentation
¶
Overview ¶
Package faults provides structured application fault errors backed by errbuilder-go and assertion reporting backed by assert-lib.
Fault codes are stable project labels such as "validation.failed" or a project-specific code registered with RegisterCode. Transport codes are the errbuilder status class used at process boundaries.
Index ¶
- Constants
- func Check(ctx context.Context, truth bool, code Code, msg string, fields ...any) error
- func IsCode(err error, code Code) bool
- func Never(ctx context.Context, code Code, msg string, fields ...any) error
- func New(code Code, msg string, fields ...any) error
- func NewDetails(fields ...any) errbuilder.ErrDetails
- func NoError(ctx context.Context, err error, code Code, msg string, fields ...any) error
- func Recover(ctx context.Context, recovered any, code Code, msg string, fields ...any) error
- func RegisterCode(code Code, transport TransportCode)
- func RegisterCodes(mapping Mapping)
- func Wrap(code Code, msg string, cause error, fields ...any) error
- type Code
- type Field
- type Handler
- func (h *Handler) AddData(key string, value any)
- func (h *Handler) AddFlush(flusher assert.AssertFlush)
- func (h *Handler) AssertHandler() *assert.AssertHandler
- func (h *Handler) Check(ctx context.Context, truth bool, code Code, msg string, fields ...any) error
- func (h *Handler) Never(ctx context.Context, code Code, msg string, fields ...any) error
- func (h *Handler) NoError(ctx context.Context, err error, code Code, msg string, fields ...any) error
- func (h *Handler) Operation(ctx context.Context, code Code, fields ...any) Operation
- func (h *Handler) ProcessDeferred(ctx context.Context)
- func (h *Handler) Recover(ctx context.Context, recovered any, code Code, msg string, fields ...any) error
- type Mapping
- type Operation
- type Option
- func WithDebugMode() Option
- func WithDeferMode() Option
- func WithExitFunc(exitFunc func(int)) Option
- func WithFormatter(formatter assert.Formatter) Option
- func WithMetadata(key string, value any) Option
- func WithTextFormatter() Option
- func WithVerboseMode() Option
- func WithWriter(writer io.Writer) Option
- type TransportCode
Constants ¶
const ( FieldErrorKey = "fault_field_error" MissingFieldValue = "<missing>" NilFieldValue = "<nil>" )
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check reports and returns a structured fault with a default Handler when truth is false.
func NewDetails ¶
func NewDetails(fields ...any) errbuilder.ErrDetails
NewDetails converts key/value fields into errbuilder details.
func RegisterCode ¶
func RegisterCode(code Code, transport TransportCode)
RegisterCode registers or replaces a process-wide transport mapping for a project-specific fault code. Call it during process initialization. Empty codes are ignored.
func RegisterCodes ¶
func RegisterCodes(mapping Mapping)
RegisterCodes registers or replaces multiple code mappings.
Types ¶
type Code ¶
type Code string
Code is a stable, project-owned fault label stored on structured errors and assertion events. Codes are application semantics; transport codes are mapped separately through errbuilder.
const ( CodeUnknown Code = "unknown" CodeConfigRequired Code = "config.required" CodeConfigInvalid Code = "config.invalid" CodeValidationFailed Code = "validation.failed" CodeAuthRequired Code = "auth.required" CodeDependencyMissing Code = "dependency.missing" CodeInvariantFailed Code = "invariant.failed" CodeNotFound Code = "not_found" CodeResourceLimit Code = "resource.limit" CodeDatabaseRead Code = "database.read" CodeDatabaseWrite Code = "database.write" CodeStorageFailed Code = "storage.failed" CodeRuntimeFailed Code = "runtime.failed" )
func CodeOf ¶
CodeOf returns the application fault code stored on err. It returns CodeUnknown when err is nil or is not a faults/errbuilder error.
func NormalizeCode ¶
NormalizeCode returns CodeUnknown for empty labels and trims surrounding whitespace from user-provided codes.
func (Code) ErrCode ¶
func (c Code) ErrCode() TransportCode
ErrCode returns the errbuilder transport code for c.
type Field ¶
Field is a normalized detail key/value pair.
func NormalizeFields ¶
NormalizeFields converts variadic key/value fields into stable string keys. Odd field counts are preserved with FieldErrorKey and a MissingFieldValue for the last key.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler integrates structured faults with assert-lib assertion reporting.
func NewHandler ¶
NewHandler returns a Handler with production-safe defaults: JSON assertions to stderr and a no-op exit function.
func (*Handler) AddFlush ¶
func (h *Handler) AddFlush(flusher assert.AssertFlush)
AddFlush registers an assert-lib flusher.
func (*Handler) AssertHandler ¶
func (h *Handler) AssertHandler() *assert.AssertHandler
AssertHandler returns the underlying assert-lib handler.
func (*Handler) Check ¶
func (h *Handler) Check(ctx context.Context, truth bool, code Code, msg string, fields ...any) error
Check reports and returns a structured fault when truth is false.
func (*Handler) NoError ¶
func (h *Handler) NoError(ctx context.Context, err error, code Code, msg string, fields ...any) error
NoError reports and wraps err when it is non-nil.
func (*Handler) ProcessDeferred ¶
ProcessDeferred flushes deferred assertions.
type Mapping ¶
type Mapping map[Code]TransportCode
Mapping declares fault-code to transport-code mappings. Use RegisterCodes at process startup for project-specific fault codes.
type Operation ¶
type Operation struct {
// contains filtered or unexported fields
}
Operation is a scoped assertion/fault helper.
type Option ¶
type Option func(*handlerOptions)
Option configures a Handler.
func WithDebugMode ¶
func WithDebugMode() Option
WithDebugMode enables stack traces in assertion output.
func WithDeferMode ¶
func WithDeferMode() Option
WithDeferMode defers assertion failures until ProcessDeferred is called.
func WithExitFunc ¶
WithExitFunc sets the assertion failure exit function.
func WithFormatter ¶
WithFormatter sets the assert-lib formatter.
func WithMetadata ¶
WithMetadata adds static assertion metadata to every handler assertion.
func WithTextFormatter ¶
func WithTextFormatter() Option
WithTextFormatter uses assert-lib's text formatter.
func WithVerboseMode ¶
func WithVerboseMode() Option
WithVerboseMode enables verbose assert-lib output.
func WithWriter ¶
WithWriter sets the assertion output writer.
type TransportCode ¶
type TransportCode = errbuilder.ErrCode
TransportCode is the errbuilder transport/status class used by RPC, HTTP, and other boundaries.
const ( TransportCanceled TransportCode = errbuilder.CodeCanceled TransportUnknown TransportCode = errbuilder.CodeUnknown TransportInvalidArgument TransportCode = errbuilder.CodeInvalidArgument TransportDeadlineExceeded TransportCode = errbuilder.CodeDeadlineExceeded TransportNotFound TransportCode = errbuilder.CodeNotFound TransportAlreadyExists TransportCode = errbuilder.CodeAlreadyExists TransportPermissionDenied TransportCode = errbuilder.CodePermissionDenied TransportResourceExhausted TransportCode = errbuilder.CodeResourceExhausted TransportFailedPrecondition TransportCode = errbuilder.CodeFailedPrecondition TransportAborted TransportCode = errbuilder.CodeAborted TransportOutOfRange TransportCode = errbuilder.CodeOutOfRange TransportUnimplemented TransportCode = errbuilder.CodeUnimplemented TransportInternal TransportCode = errbuilder.CodeInternal TransportDataLoss TransportCode = errbuilder.CodeDataLoss TransportUnauthenticated TransportCode = errbuilder.CodeUnauthenticated )
func ErrCodeOf ¶
func ErrCodeOf(err error) TransportCode
ErrCodeOf returns the errbuilder transport code carried by err.
func TransportCodeOf ¶
func TransportCodeOf(code Code) TransportCode
TransportCodeOf returns the registered transport mapping for code.