Documentation
¶
Overview ¶
Package beterr provides structured error handling and debugging utilities for Go applications. It offers enhanced error formatting with function call context and argument inspection.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StructString ¶
StructString converts any value to a JSON string representation. If JSON marshaling fails, it falls back to the default string format.
Types ¶
type Error ¶ added in v0.4.0
type Error struct {
// contains filtered or unexported fields
}
Error is the error returned by Wrap.E. It carries the original underlying error alongside a JSON-serialised description of the wrap (function name, arguments, message, and any nested chain), plus a pre-built shallow view of the immediate previous error for Top to return.
func (*Error) Error ¶ added in v0.4.0
Error returns the JSON-serialised wrap including any nested chain.
func (*Error) Top ¶ added in v0.4.0
Top returns the immediate previous error this wrap was applied to, with any further beterr chain stripped. If the wrapped error was itself a beterr Error, Top returns a shallow rendition of it — that layer's own fn_name, args, and msg, with its inner set to null. If the wrapped error was a plain error, Top returns it as-is. If the wrap was created over a nil error, Top returns nil.
Use Top when forwarding an error to a caller who shouldn't see the full nested error stack — for example, returning errors from an RPC handler:
wrapped := beterr.W(userID).E(err, "failed to process request") log.Println(wrapped) // full nested debug JSON return connect.NewError(code, wrapped.Top()) // only the last error
type Wrap ¶ added in v0.1.2
type Wrap struct {
// A holds arguments to be included in debug output
A []any
}
Wrap provides debugging functionality with argument tracking. The A field stores arguments that will be included in error output.
func W ¶ added in v0.1.4
W creates a new Wrap instance with the provided arguments. This is a convenience function that internally calls Wrap{A: []any{...}}. It accepts any number of arguments which will be included in error output for debugging.
Example usage:
w := W(userID, requestData, config) return w.E(err, "failed to process request")
func (*Wrap) E ¶ added in v0.1.2
E formats an error with debugging context including function name, arguments, and message. It wraps the original error with structured debugging information that can be chained. The returned *Error implements the error interface, exposes Unwrap so errors.Is/As traverse the chain, and exposes Top to release only the immediate previous error without leaking the deeper stack.