Documentation
¶
Index ¶
- Variables
- func Is(err error) bool
- func SetDefaultCaller(enabled bool)
- func SetFileConverter(fn FileConverter)
- type Error
- func As(err error) (*Error, bool)
- func BadGateway(message string, opts ...Option) *Error
- func BadRequest(message string, opts ...Option) *Error
- func Conflict(message string, opts ...Option) *Error
- func Forbidden(message string, opts ...Option) *Error
- func From(err error) *Error
- func GatewayTimeout(message string, opts ...Option) *Error
- func Internal(message string, opts ...Option) *Error
- func New(message string, opts ...Option) *Error
- func NotFound(message string, opts ...Option) *Error
- func RequestTimeout(message string, opts ...Option) *Error
- func ServiceUnavailable(message string, opts ...Option) *Error
- func TooManyRequests(message string, opts ...Option) *Error
- func Unauthorized(message string, opts ...Option) *Error
- func UnprocessableEntity(message string, opts ...Option) *Error
- func UnsupportedMediaType(message string, opts ...Option) *Error
- func Wrap(err error, opts ...Option) *Error
- func (e *Error) Append(errs ...error) *Error
- func (e *Error) Clone() *Error
- func (e *Error) Error() string
- func (e *Error) Format(s fmt.State, verb rune)
- func (e *Error) Is(target error) bool
- func (e *Error) IsEmpty() bool
- func (e *Error) IsNotEmpty() bool
- func (e *Error) MarshalPublic() ([]byte, error)
- func (e *Error) Public() *Error
- func (e *Error) StatusOr(fallback int) int
- func (e *Error) StatusOrInternal() int
- func (e *Error) ToJSON() ([]byte, error)
- func (e *Error) Unwrap() error
- func (e *Error) WithCategory(category string) *Error
- func (e *Error) WithCause(cause error) *Error
- func (e *Error) WithCode(code string) *Error
- func (e *Error) WithException(exception bool) *Error
- func (e *Error) WithExtra(key string, value any) *Error
- func (e *Error) WithMessage(message string) *Error
- func (e *Error) WithStatus(statusCode int) *Error
- func (e *Error) WithSubCategory(subCategory string) *Error
- func (e *Error) WithTitle(title string) *Error
- func (e *Error) WriteJSON(w http.ResponseWriter) error
- func (e *Error) WriteJSONTo(w io.Writer) error
- type FileConverter
- type Option
- func WithCaller(skip int) Option
- func WithCategory(category string) Option
- func WithCause(cause error) Option
- func WithCode(code string) Option
- func WithException() Option
- func WithExtra(key string, value any) Option
- func WithExtras(extra map[string]any) Option
- func WithStatus(statusCode int) Option
- func WithSubCategory(subCategory string) Option
- func WithTitle(title string) Option
Constants ¶
This section is empty.
Variables ¶
var ErrNil = errors.New("hes: nil error")
ErrNil is returned when a method is called on a nil *Error receiver that cannot produce a meaningful HTTP response.
Functions ¶
func SetDefaultCaller ¶ added in v1.0.0
func SetDefaultCaller(enabled bool)
SetDefaultCaller enables or disables automatic caller capture for New/Wrap.
func SetFileConverter ¶ added in v1.0.0
func SetFileConverter(fn FileConverter)
SetFileConverter sets a path rewriter used when capturing callers. Pass nil to clear.
Types ¶
type Error ¶
type Error struct {
// StatusCode is the HTTP status code.
StatusCode int `json:"statusCode,omitempty"`
// Code is an application-defined error code.
Code string `json:"code,omitempty"`
// Category groups related errors (e.g. package or domain name).
Category string `json:"category,omitempty"`
// SubCategory further classifies the error within Category.
SubCategory string `json:"subCategory,omitempty"`
// Title is a short human-readable summary.
Title string `json:"title,omitempty"`
// Message is the primary error description.
Message string `json:"message,omitempty"`
// Exception marks unexpected server-side failures.
Exception bool `json:"exception,omitempty"`
// File is the source file from caller capture, if enabled.
File string `json:"file,omitempty"`
// Line is the source line from caller capture, if enabled.
Line int `json:"line,omitempty"`
// Extra holds optional structured metadata.
Extra map[string]any `json:"extra,omitempty"`
// Errs holds aggregated sub-errors (e.g. validation).
Errs []*Error `json:"errs,omitempty"`
// Cause is the underlying error, omitted from JSON.
Cause error `json:"-"`
}
Error is a structured HTTP error.
After construction, treat values as immutable: use With* / Append / Clone to derive new instances instead of mutating fields in place.
func BadGateway ¶ added in v1.0.0
BadGateway creates a 502 exception error.
func BadRequest ¶ added in v1.0.0
BadRequest creates a 400 error (alias of New).
func From ¶ added in v1.0.0
From returns err as *Error using errors.As when possible; otherwise Wrap(err).
func GatewayTimeout ¶ added in v1.0.0
GatewayTimeout creates a 504 exception error.
func RequestTimeout ¶ added in v1.0.0
RequestTimeout creates a 408 error.
func ServiceUnavailable ¶ added in v1.0.0
ServiceUnavailable creates a 503 exception error.
func TooManyRequests ¶ added in v1.0.0
TooManyRequests creates a 429 error.
func Unauthorized ¶ added in v1.0.0
Unauthorized creates a 401 error.
func UnprocessableEntity ¶ added in v1.0.0
UnprocessableEntity creates a 422 error.
func UnsupportedMediaType ¶ added in v1.0.0
UnsupportedMediaType creates a 415 error.
func Wrap ¶
Wrap converts err into an *Error.
- nil → nil
- error chain containing *Error, no opts → that *Error (same pointer)
- error chain containing *Error, with opts → deep clone with options applied
- other errors → new *Error with status 500, Exception=true, Cause=err
func (*Error) Append ¶ added in v1.0.0
Append returns a clone with additional sub-errors. Nil errors are skipped. Nested multi-errors are flattened. Duplicates (same logical error per Is/same) are ignored.
Sub-error pointers are shared (errors are treated as immutable). Use Clone on a child first if you need an isolated copy.
func (*Error) Format ¶
Format implements fmt.Formatter.
%s, %v — Error() %q — quoted Message %+v — Error() plus cause chain and call site
func (*Error) Is ¶ added in v1.0.0
Is reports whether target matches this error for errors.Is.
Matching rules:
- pointer identity is handled by errors.Is before calling Is
- if both sides have a non-empty Code: Codes are equal
- otherwise: same StatusCode, Category, SubCategory, Code, Title, and Message
The structural rule matches Append dedup so identical client-facing data is treated as the same error.
func (*Error) IsNotEmpty ¶
IsNotEmpty reports whether there is at least one sub-error.
func (*Error) MarshalPublic ¶ added in v1.0.0
MarshalPublic marshals the client-facing view (no file/line) without cloning.
func (*Error) Public ¶ added in v1.0.0
Public returns a deep clone safe for client responses: caller File/Line are stripped from the error and all sub-errors.
Prefer MarshalPublic / WriteJSON when you only need JSON output; those paths avoid a full tree clone.
func (*Error) StatusOr ¶ added in v1.0.0
StatusOr returns StatusCode, or fallback when StatusCode is zero.
func (*Error) StatusOrInternal ¶ added in v1.0.0
StatusOrInternal returns StatusCode, or 500 when zero.
func (*Error) Unwrap ¶ added in v1.0.0
Unwrap returns the underlying cause, or joined sub-errors when present.
func (*Error) WithCategory ¶ added in v1.0.0
WithCategory returns a clone with an updated category.
func (*Error) WithCode ¶ added in v1.0.0
WithCode returns a clone with an updated application error code.
func (*Error) WithException ¶ added in v1.0.0
WithException returns a clone with Exception set.
func (*Error) WithMessage ¶ added in v1.0.0
WithMessage returns a clone with an updated message.
func (*Error) WithStatus ¶ added in v1.0.0
WithStatus returns a clone with an updated HTTP status code.
func (*Error) WithSubCategory ¶ added in v1.0.0
WithSubCategory returns a clone with an updated sub-category.
type FileConverter ¶ added in v1.0.0
FileConverter rewrites captured source paths (e.g. strip build prefixes).
type Option ¶ added in v1.0.0
type Option func(*Error)
Option configures an Error at construction time.
func WithCaller ¶ added in v1.0.0
WithCaller captures the call site when the option is created. skip is additional frames above the WithCaller call itself (0 = direct caller).
func WithCategory ¶ added in v1.0.0
WithCategory sets the error category.
func WithException ¶ added in v1.0.0
func WithException() Option
WithException marks the error as an unexpected exception.
func WithExtras ¶ added in v1.0.0
WithExtras merges metadata entries into Extra.
func WithStatus ¶ added in v1.0.0
WithStatus sets the HTTP status code.
func WithSubCategory ¶ added in v1.0.0
WithSubCategory sets the error sub-category.