libError

package
v0.28.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package libError provides structured error handling with action and source tracking.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append added in v0.5.7

func Append(err error, errChild error, format string, args ...any) error

Append joins errChild to err with a formatted message, handling nil err gracefully.

func Join

func Join(err error, format string, args ...any) error

Join wraps err with a formatted message, preserving the original error for unwrapping.

func ToSnakeCase added in v0.12.0

func ToSnakeCase(str string) string

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

func (a Action) Format(stack *strings.Builder)

Format writes the action's status, description, and message into the given string builder.

func (Action) LogValue added in v0.15.0

func (a Action) LogValue() slog.Value

LogValue implements slog.LogValuer and returns a grouped value with fields redacted. See https://pkg.go.dev/log/slog#LogValuer

func (Action) SLog added in v0.12.0

func (a Action) SLog() slog.Attr

SLog returns a grouped slog.Attr representing the action for structured logging.

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 Add added in v0.12.0

func Add(err Error, status status.StatusCode, desc string, message any) Error

Add wraps the given error with a parent status code and description.

func Convert added in v0.12.0

func Convert(err error, status status.StatusCode, desc string, message any) Error

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

func NewWithDescription(status status.StatusCode, desc string, format string, a ...any) Error

NewWithDescription creates a new Error with the given status, description, and formatted message.

func Unwrap added in v0.15.0

func Unwrap(err error) (bool, Error)

Unwrap attempts to extract a structured Error from a standard error, returning whether it succeeded.

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

func (e ErrorData) Action() Action

Action returns the action data associated with this error.

func (ErrorData) Error added in v0.12.0

func (e ErrorData) Error() string

Error returns the formatted string representation of the full error chain.

func (ErrorData) Format added in v0.12.0

func (e ErrorData) Format(stack *strings.Builder)

Format writes the full error chain (action, source, and child) into the given string builder.

func (ErrorData) LogValue added in v0.15.0

func (e ErrorData) LogValue() slog.Value

LogValue implements slog.LogValuer and returns a grouped value with fields redacted. See https://pkg.go.dev/log/slog#LogValuer

func (ErrorData) Src added in v0.12.0

func (e ErrorData) Src() *Source

Src returns the source location where this error was created.

type Source added in v0.12.0

type Source struct {
	File string `json:"file"`
	Line int    `json:"line"`
}

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.

func (Source) Format added in v0.12.0

func (s Source) Format(stack *strings.Builder)

Format writes the source file and line number into the given string builder.

func (Source) LogValue added in v0.15.0

func (s Source) LogValue() slog.Value

LogValue implements slog.LogValuer and returns a grouped value with fields redacted. See https://pkg.go.dev/log/slog#LogValuer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL