| sidebar_position | 6 |
|---|
The erratt package provides an error type with structured key-value attributes, designed for use with slog and the EventHandler.Warn method.
// Simple error
err := erratt.New("something went wrong")
// Error with attributes
err := erratt.New("error opening file", "filename", filename)
err := erratt.New("authentication failed", "username", user, "password", pass)Wrap existing Go errors while preserving attributes:
err := callFunction()
wrapped := erratt.Errorf("unexpected error: %w", err)Chain With() to add context:
err := connectToServer(url, user, pass)
wrapped := erratt.Errorf("cannot connect: %w", err).
With("url", url, "username", user)func auth() erratt.Error {
return erratt.New("authentication failure",
"username", "test-user",
"password", "test-password",
)
}
func connect() erratt.Error {
err := auth()
if err != nil {
return erratt.Errorf("connect failed: %w", err).
With("url", "https://example.com")
}
return nil
}Output via slog:
ERRO connect failed: authentication failure url=https://example.com username=test-user password=test-password
The Warn method handles erratt.Error values, preserving structured attributes in the log output:
events.Warn(erratt.New("resource skipped", "kind", kind, "name", name))