Documentation
¶
Overview ¶
Package libError provides structured error handling with action and source tracking.
Index ¶
- func Append(err error, errChild error, format string, args ...any) error
- func Join(err error, format string, args ...any) error
- func ToSnakeCase(str string) string
- type Action
- type Error
- func Add(err Error, status status.StatusCode, desc string, message any) Error
- func Convert(err error, status status.StatusCode, desc string, message any) Error
- func New(status status.StatusCode, desc string, msg any) Error
- func NewWithDescription(status status.StatusCode, desc string, format string, a ...any) Error
- func Unwrap(err error) (bool, Error)
- type ErrorData
- type Source
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶ added in v0.5.7
Append joins errChild to err with a formatted message, handling nil err gracefully.
func ToSnakeCase ¶ added in v0.12.0
ToSnakeCase converts a camelCase or PascalCase string to UPPER_SNAKE_CASE.
Types ¶
type Action ¶ added in v0.12.0
type Action struct {
Status status.StatusCode `json:"status"`
Description string `json:"description"` // public code
PublicDescription string `json:"publicDescription"` // optional client-visible description
Message any `json:"message"` // internal only, never sent to client
}
Action holds error action data. Description is the public code (e.g. API_OK_RESP_JSON). PublicDescription, when non-empty, is used as the client-visible description; otherwise the response layer resolves description from ErrorDesc/safe fallback. Message is internal-only (logs/tracing).
func (Action) Format ¶ added in v0.12.0
Format writes the action's status, description, and message into the given string builder.
func (Action) LogValue ¶ added in v0.15.0
LogValue implements slog.LogValuer and returns a grouped value with fields redacted. See https://pkg.go.dev/log/slog#LogValuer
type Error ¶ added in v0.12.0
type Error interface {
error
Action() Action
Src() *Source
Format(*strings.Builder)
LogValue() slog.Value
}
Error is the interface for structured errors with action, source, and logging support.
func Convert ¶ added in v0.12.0
Convert wraps a standard error into a structured Error with the given status, description, and message.
func New ¶ added in v0.12.0
func New(status status.StatusCode, desc string, msg any) Error
New creates a new Error with the given status, description, and message.
func NewWithDescription ¶ added in v0.15.0
NewWithDescription creates a new Error with the given status, description, and formatted message.
type ErrorData ¶ added in v0.12.0
type ErrorData struct {
Time time.Time
ActionData Action `json:"action"`
Source *Source `json:"source,omitempty"`
Child Error `json:"child"`
}
ErrorData is the concrete implementation of Error, carrying time, action, source, and child error.
func (ErrorData) Action ¶ added in v0.12.0
Action returns the action data associated with this error.
func (ErrorData) Error ¶ added in v0.12.0
Error returns the formatted string representation of the full error chain.
func (ErrorData) Format ¶ added in v0.12.0
Format writes the full error chain (action, source, and child) into the given string builder.
func (ErrorData) LogValue ¶ added in v0.15.0
LogValue implements slog.LogValuer and returns a grouped value with fields redacted. See https://pkg.go.dev/log/slog#LogValuer
type Source ¶ added in v0.12.0
Source identifies the file and line where an error originated.
func GetStack ¶ added in v0.12.0
func GetStack() *Source
GetStack returns the Source of the immediate caller.