Documentation
¶
Overview ¶
Package errs provides a structured, transport-agnostic error type for domain and application layers.
Errors carry a machine-readable Code, a human-readable Message, optional per-field validation failures, and optional internal metadata. They contain no HTTP-specific concepts — the HTTP layer maps codes to status codes.
Usage ¶
// standard constructors
errs.NotFound("user not found")
errs.InvalidInput("bad request", errs.NewFieldError("email", errs.CodeRequired, "required"))
// app-specific codes
errs.New("EMAIL_ALREADY_EXISTS", "email is already taken")
// attach internal context (never exposed to clients)
errs.Internal("unexpected error").WithCause(dbErr).WithMeta("user_id", id)
// errors.As traversal works through fmt.Errorf wrapping
wrapped := fmt.Errorf("UserService.GetByID: %w", errs.NotFound("user not found"))
var e *errs.Error
errors.As(wrapped, &e) // true
Index ¶
- Constants
- type Error
- func Conflict(message string) *Error
- func Forbidden(message string) *Error
- func Internal(message string) *Error
- func InvalidInput(message string, fields ...FieldError) *Error
- func New(code, message string) *Error
- func NotFound(message string) *Error
- func RateLimitExceeded(message string) *Error
- func Unauthorized(message string) *Error
- type FieldError
Constants ¶
const ( CodeNotFound = "not_found" CodeForbidden = "forbidden" CodeInvalidInput = "invalid_input" CodeConflict = "conflict" CodeInternalServer = "internal_error" CodeRateLimit = "rate_limit_exceeded" )
Standard error codes.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct {
Code string
Message string
Fields []FieldError
Meta map[string]any
// contains filtered or unexported fields
}
Error is a structured domain error carrying a machine-readable Code, a human-readable Message, optional per-field validation failures, and optional internal metadata. It contains no HTTP-specific concepts.
func Internal ¶
Internal returns an Error with CodeInternalServer. Attach the real cause with WithCause so it can be logged without leaking internals.
func InvalidInput ¶
func InvalidInput(message string, fields ...FieldError) *Error
InvalidInput returns an Error with CodeInvalidInput and optional field errors.
func New ¶
New constructs an Error with the given code and message. Use this for application-specific codes not covered by the standard constructors.
func RateLimitExceeded ¶
RateLimitExceeded returns an Error with CodeRateLimit.
func Unauthorized ¶
Unauthorized returns an Error with CodeUnauthorized.
func (*Error) Unwrap ¶
Unwrap returns the underlying cause, enabling errors.Is / errors.As traversal.
func (*Error) WithCause ¶
WithCause attaches an underlying error for internal logging without exposing it to clients.
func (*Error) WithFields ¶
func (e *Error) WithFields(fields ...FieldError) *Error
WithFields appends field-level validation errors.
type FieldError ¶
FieldError describes a validation failure for a single field. It carries no HTTP-specific concepts.
func NewFieldError ¶
func NewFieldError(field, code, message string) FieldError
NewFieldError constructs a FieldError without params.
func NewFieldErrorWithParams ¶
func NewFieldErrorWithParams(field, code, message string, params map[string]any) FieldError
NewFieldErrorWithParams constructs a FieldError with extra context params (e.g. min_length, max).