30 lines
1.0 KiB
Go
30 lines
1.0 KiB
Go
|
|
// Package xerrors provides structured application errors with stable typed codes,
|
||
|
|
// cause chaining, and key-value context fields.
|
||
|
|
//
|
||
|
|
// Every error carries a machine-readable Code (gRPC-aligned wire value), a
|
||
|
|
// human-readable message, an optional cause, and optional structured fields.
|
||
|
|
// *Err implements errs.CodedError and errs.ContextualError from contracts,
|
||
|
|
// enabling logz to enrich log records automatically without importing this package.
|
||
|
|
//
|
||
|
|
// Usage:
|
||
|
|
//
|
||
|
|
// // Named constructors for common codes
|
||
|
|
// err := xerrors.NotFound("user %s not found", userID)
|
||
|
|
// err := xerrors.InvalidInput("email is required")
|
||
|
|
//
|
||
|
|
// // Builder pattern for structured context
|
||
|
|
// err := xerrors.New(xerrors.ErrInvalidInput, "validation failed").
|
||
|
|
// WithContext("field", "email").
|
||
|
|
// WithContext("rule", "required").
|
||
|
|
// WithError(cause)
|
||
|
|
//
|
||
|
|
// // Inspecting errors
|
||
|
|
// var e *xerrors.Err
|
||
|
|
// if errors.As(err, &e) {
|
||
|
|
// switch e.Code() {
|
||
|
|
// case xerrors.ErrNotFound:
|
||
|
|
// // handle 404
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
package xerrors
|