Documentation
¶
Index ¶
- func Category(err error) (string, bool)
- func CheckEmpty(val, errMsg string, opts ...checkOpt)
- func CheckErr(err error, opts ...checkOpt)
- func Details(err error) map[string]any
- func FormatPanic(op, message string) string
- func GetDetail(err error, key string) (any, bool)
- func GetErrorRegistry() map[string]int
- func IsHerror(err error) bool
- func JSONFormatter(h *Herror) string
- func Must[T any](v T, err error) T
- func NewCategorizedHerror(op, category, msg string, err error, details map[string]any) error
- func NewHerror(op, msg string, err error, details map[string]any) error
- func NewHerrorErrorf(op, fmtStr string, args ...any) error
- func OneLineErr(er string) string
- func Operation(err error) (string, bool)
- func PlainFormatter(h *Herror) string
- func PropagateErr(op, category, message string, err error, details map[string]any) error
- func PseudoJSONFormatter(h *Herror) string
- func RegisterError(err error)
- func RootCause(err error) error
- func RootCauseHelper(err error) error
- func SimpleColoredFormatter(h *Herror) string
- func StackTrace(err error) (string, bool)
- func UserMessage(err error) (string, bool)
- func WithCategory(cat string) checkOpt
- func WithDetail(err error, k string, v any) error
- func WithDetails(d map[string]any) checkOpt
- func WithExitCode(code int) checkOpt
- func WithFormatter(f FormatterFunc) checkOpt
- func WithMessage(msg string) checkOpt
- func WithOp(opName string) checkOpt
- func WithWriter(w io.Writer) checkOpt
- func Wrap(err error, op, message string) error
- type CollectingError
- type FormatterFunc
- type HErrorer
- type Herror
- type LogNotFoundOption
- type NotFoundAction
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckEmpty ¶ added in v1.1.0
func CheckEmpty(val, errMsg string, opts ...checkOpt)
CheckEmpty will call CheckErr if the given string is empty.
val – the string to validate errMsg – the error text to wrap (e.g. "`--script` is required") opts – any CheckErr options (WithOp, WithMessage, etc.)
func CheckErr ¶
func CheckErr(err error, opts ...checkOpt)
CheckErr registers, wraps, formats and logs a fatal error via Horus. If err is non-nil it prints using the configured FormatterFunc, then exits.
func Details ¶
Details returns all details associated with an Herror, if present. Details returns all details associated with an Herror, or an empty (non-nil) map if there is none.
func FormatPanic ¶
FormatPanic returns a red-formatted panic message for the given operation and message.
func GetErrorRegistry ¶
GetErrorRegistry returns a copy of the current error counts.
func JSONFormatter ¶
JSONFormatter generates a JSON representation of an Herror.
func Must ¶ added in v1.1.0
Must returns the value v if err is nil; otherwise it calls CheckErr(err) and exits.
func NewCategorizedHerror ¶
NewCategorizedHerror creates a new Herror with a category.
func NewHerrorErrorf ¶
TODO: set the API for one-liner here NewHerrorErrorf creates a new Herror with a formatted message.
func OneLineErr ¶ added in v1.2.0
OneLineErr returns a bold, red version of the input string. Useful for displaying a single‑line error message without the full Herror structure.
func PlainFormatter ¶
func PropagateErr ¶
PropagateErr wraps a non-nil error in an Herror with the given context. If err is already an Herror, its Category and Details are optionally preserved (unless overridden) and merged with the new details. If err is nil, PropagateErr returns nil.
func PseudoJSONFormatter ¶
func RegisterError ¶
func RegisterError(err error)
RegisterError increments the count for this error’s category.
func RootCauseHelper ¶
func SimpleColoredFormatter ¶
SimpleColoredFormatter generates a colored representation of an Herror using the chalk library. You can extend this function to use different colors based on the error category.
func StackTrace ¶
StackTrace returns the formatted stack trace from an error if it's an Herror.
func UserMessage ¶
UserMessage returns the user-friendly message associated with an Herror, if present.
func WithCategory ¶
func WithCategory(cat string) checkOpt
WithCategory overrides the error category.
func WithDetail ¶
WithDetail adds a key-value detail to an existing Herror. If the error is not an Herror, a new Herror wrapping the original will be returned with the detail.
func WithDetails ¶
WithDetails replaces the metadata map. If you want to merge instead of replace, read your existing details with Details(err) first.
func WithExitCode ¶
func WithExitCode(code int) checkOpt
WithExitCode sets a custom exit code (defaults to 1).
func WithFormatter ¶ added in v1.1.0
func WithFormatter(f FormatterFunc) checkOpt
WithFormatter lets you choose any FormatterFunc (JSONFormatter, PlainFormatter, your own custom FormatterFunc, etc). Defaults to PseudoJSONFormatter.
func WithMessage ¶
func WithMessage(msg string) checkOpt
WithMessage overrides the user‐facing message.
func WithOp ¶
func WithOp(opName string) checkOpt
WithOp lets you override the operation name that CheckErr will wrap with.
func WithWriter ¶
WithWriter redirects the error output (defaults to stderr).
Types ¶
type CollectingError ¶
type CollectingError struct {
// contains filtered or unexported fields
}
CollectingError implements both io.Writer and error, accumulating writes into an internal buffer. It is safe for concurrent use.
func NewCollectingError ¶
func NewCollectingError() *CollectingError
NewCollectingError returns an empty CollectingError.
func (*CollectingError) Bytes ¶
func (ce *CollectingError) Bytes() []byte
Bytes returns a copy of the internal buffer's bytes. It is safe for concurrent calls.
func (*CollectingError) Error ¶
func (ce *CollectingError) Error() string
Error returns the accumulated contents as a string. It is safe for concurrent calls.
func (*CollectingError) Reset ¶
func (ce *CollectingError) Reset()
Reset clears the internal buffer, allowing reuse of the CollectingError. It is safe for concurrent calls.
func (*CollectingError) Write ¶
func (ce *CollectingError) Write(p []byte) (n int, err error)
Write appends p to the internal buffer. It is safe for concurrent calls.
func (*CollectingError) WriteString ¶
func (ce *CollectingError) WriteString(s string) (n int, err error)
WriteString appends s to the internal buffer. It is safe for concurrent calls.
type FormatterFunc ¶
FormatterFunc defines a function type for custom error formatting.
type Herror ¶
type Herror struct {
Op string // The operation being performed (e.g., "read file", "connect db")
Message string // A user-friendly message providing more context
Err error // The underlying error, if any
Details map[string]any // Optional details for more specific context
Category string // Error category (e.g., validation, IO, etc.)
Stack []uintptr // Stack trace captured at the time of error creation.
}
Herror represents a generalized error with added context.
func (*Herror) Format ¶
Format generates a custom representation of the error using a formatter function.
func (*Herror) MarshalJSON ¶
MarshalJSON ensures Err is emitted as its Error() string, not an object.
func (*Herror) StackTrace ¶
StackTrace returns a formatted stack trace captured when the error was created.
type LogNotFoundOption ¶
type LogNotFoundOption func(*logNotFoundConfig)
LogNotFoundOption customizes how LogNotFound prints.
func WithLogWriter ¶
func WithLogWriter(w io.Writer) LogNotFoundOption
WithLogWriter directs the warning message to a different io.Writer.
func WithNotFoundTemplate ¶
func WithNotFoundTemplate(tmpl string) LogNotFoundOption
WithNotFoundTemplate lets you override the printf-style template.
type NotFoundAction ¶
NotFoundAction is invoked when a lookup fails. It should return (resolved, err), where `resolved==false` means “we didn’t fix it,” and any non-nil error will be propagated.
func LogNotFound ¶
func LogNotFound(contextMsg string, opts ...LogNotFoundOption) NotFoundAction
LogNotFound returns a NotFoundAction that logs a warning when a resource isn’t found. By default it prints to stderr in yellow:
Warning: Data address '...' not found. Context: ...
func NullAction ¶
func NullAction(resolved bool) NotFoundAction
NullAction returns a NotFoundAction that does nothing. It simply returns false (meaning the missing resource remains unresolved) and no error. Use this when you want to provide a custom action that doesn't attempt any remediation.