Documentation
¶
Overview ¶
Package errors provides standard-compatible errors with stack traces.
New, Errorf, and Join capture the calling goroutine's stack. Ordinary formatting preserves the error message; %+v prints the error tree and traces. Use Formatter when a standard-library wrapper is the outermost error.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupported = stderrors.ErrUnsupported
ErrUnsupported is the standard library's unsupported-operation sentinel.
Functions ¶
func Errorf ¶
Errorf formats an error using fmt.Errorf, preserving its message and unwrap shape, and captures a stack at the call site. Use %w to wrap causes.
Example ¶
package main
import (
"fmt"
"io"
"github.com/Songmu/errors"
)
func main() {
err := errors.Errorf("read config: %w", io.EOF)
fmt.Println(err)
fmt.Println(errors.Is(err, io.EOF))
}
Output: read config: EOF true
func Formatter ¶
Formatter returns an output-only adapter for err. It does not capture a stack or change the error tree. %+v traverses both forms of Unwrap, including standard-library wrappers; other string formats use err.Error().
A nil error formats as "<nil>". Traversal stops at a depth of 64 or 1024 nodes and prints an omission marker, including for cyclic error trees. The root message is complete when Error succeeds. Nested Join messages are summarized; other nested messages are limited to four lines and 256 bytes with an omission marker. Panics are marked without suppressing later siblings. Error and Unwrap methods must themselves terminate.
Example ¶
package main
import (
"fmt"
"github.com/Songmu/errors"
)
func main() {
err := fmt.Errorf("start service: %w", errors.New("read failed"))
// Use %+v instead of %s to include the retained stack trace.
fmt.Printf("%s\n", errors.Formatter(err))
}
Output: start service: read failed
func Join ¶
Join joins non-nil errors using errors.Join and captures a stack at the call site. It returns nil if every argument is nil.
Example ¶
package main
import (
"fmt"
"github.com/Songmu/errors"
)
func main() {
err := errors.Join(errors.New("read failed"), errors.New("cleanup failed"))
fmt.Println(err)
}
Output: read failed cleanup failed
Types ¶
This section is empty.