Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandleFmtWriteError ¶
func HandleFmtWriteError(handler func(err error))
HandleFmtWriteError handles (rare) errors when writing to fmt.State. It defaults to printing the errors.
func SlogRecord ¶
SlogRecord traverses the error chain, calling Unwrap(), to look for slog Records All records will be merged and mesage strings are joined together The message string may contain some of the structured information This depends on defining ErrorNoUnwrap from the interface ErrorNotUnwrapped
if record := errors.SlogRecord(errIn); record != nil {
record.Add(logArgs...)
if err := slog.Default().Handler().Handle(ctx, *record); err != nil {
slog.ErrorContext(ctx, fmt.Sprintf("%+v", err))
}
}
Example ¶
package main
import (
"errors"
"fmt"
"github.com/gregwebs/errors/slogerr"
)
func main() {
err := slogerr.Wraps(
errors.New("cause"),
"structured",
"key", "value",
"int", 1,
)
rec := slogerr.SlogRecord(err)
fmt.Println(rec.Message)
}
Output: structured: cause
func SlogTextBuffer ¶
func SlogTextBuffer(opts *slog.HandlerOptions) (slog.Handler, func() string)
SlogTextBuffer produces a Handler that writes to a buffer The buffer output can be retrieved with the returned function
if record := errors.SlogRecord(err); record != nil {
handler, output := errors.SlogTextBuffer(slog.HandlerOptions{AddSource: false})
if err := handler.Handle(ctx, *record); err != nil {
fmt.Println(fmt.Sprintf("%+v", err))
} else {
fmt.Println(output())
}
}
Types ¶
type HasSlogRecord ¶
HasSlogRecord is used to retrieve an slog.Record. A StructuredError defines it. Alternatively SlogMessager and LogValuer can be defined.
type SlogMessager ¶
type SlogMessager interface {
SlogMsg() string
}
SlogMessager provides a string that can be used as the slog message. Normally LogValuer is defined as well. When defining error types this may be simpler to use than defining HasSlogRecord.
type StructuredError ¶
type StructuredError interface {
error
HasSlogRecord
}
StructuredError is returned by Wraps and Slog
Example ¶
package main
import (
"errors"
"fmt"
"github.com/gregwebs/errors/slogerr"
)
func main() {
err := slogerr.Wraps(
errors.New("cause"),
"structured",
"key", "value",
"int", 1,
)
fmt.Println(err.Error())
}
Output: structured key=value int=1: cause
func New ¶
func New(msg string, args ...interface{}) StructuredError
New creates an error that instead of generating a format string generates a structured slog Record. Accepts as args any valid slog args. Also accepts `[]slog.Attr` as a single argument to avoid having to cast that argument. The slog Record can be retrieved with SlogRecord. Structured errors are more often created by wrapping existing errors with Wraps.
Example ¶
package main
import (
"fmt"
"github.com/gregwebs/errors/slogerr"
)
func main() {
err := slogerr.New(
"cause",
"key", "value",
"int", 1,
)
fmt.Println(err.Error())
}
Output: cause key=value int=1
func Wraps ¶
func Wraps(err error, msg string, args ...interface{}) StructuredError
Wraps ends with an "s" to indicate it is Structured. Accepts as args any valid slog args. These will generate an slog Record Also accepts []slog.Attr as a single argument to avoid having to cast that argument.
Example ¶
package main
import (
"fmt"
"github.com/gregwebs/errors/slogerr"
)
func main() {
// Create a base error
baseErr := fmt.Errorf("database connection failed")
// Wrap the error with additional structured information
wrappedErr := slogerr.Wraps(baseErr, "user authentication failed",
"user_id", "123",
"attempt", 3,
)
// Print the error
fmt.Println(wrappedErr)
}
Output: user authentication failed user_id=123 attempt=3: database connection failed