The errs
package provides utilities for error handling, wrapping, and structured logging in Go. It enhances the standard error handling capabilities by adding additional context and structured logging options.
To install the package, run:
go get github.com/sulton0011/errs
- Structured Error Handling: Create and manage errors with detailed context.
- Error Wrapping: Use
Wrap
andWrapF
to add context to existing errors. - Custom Loggers: Support for JSON, text, and file-based logging through
LogType
configuration. - Error Joining: Combine multiple errors into a single message.
- Structured Logging: Log errors in a structured manner using the
slog
library with various output formats.
You can create new errors using New
or NewF
:
package main
import "github.com/sulton0011/errs"
func main() {
err := errs.New("simple error")
errF := errs.NewF("formatted error: %s", "some detail")
println(err.Error()) // Output: simple error
println(errF.Error()) // Output: formatted error: some detail
}
To add context to existing errors, use Wrap
:
package main
import (
"github.com/sulton0011/errs"
"fmt"
)
func main() {
baseErr := errs.New("file not found")
wrappedErr := errs.Wrap(baseErr, "unable to process config file")
fmt.Println(wrappedErr.Error()) // Output: file not found
}
For formatted context, use WrapF
:
formattedErr := errs.WrapF(baseErr, "error in %s operation", "read")
fmt.Println(formattedErr.Error()) // Output: error in read operation: file not found
Use Is
to compare errors:
if errs.Is(wrappedErr, baseErr) {
fmt.Println("wrappedErr is equal to baseErr")
}
You can join multiple errors into a single message:
err1 := errs.New("first error")
err2 := errs.New("second error")
joinedErr := errs.Join(" | ", err1, err2)
fmt.Println(joinedErr.Error()) // Output: first error | second error
Configure logging for JSON, text, or file-based loggers:
import "github.com/sulton0011/errs"
// Set up a JSON logger
errs.SetLogTypes(errs.LogTypeJSON)
// Set up a file logger
if err := errs.SetLogFile("logs/app.log"); err != nil {
panic(err)
}
errs.SetLogTypes(errs.LogTypeFile)
The LogType
enum supports three logging formats:
LogTypeJSON
: JSON format logging.LogTypeText
: Human-readable text format logging.LogTypeFile
: Logging to a specified file.
Configure multiple logging types with:
errs.SetLogTypes(errs.LogTypeJSON, errs.LogTypeText)
To specify a log file path for file-based logging:
if err := errs.SetLogFile("log/app.log"); err != nil {
panic("Failed to set log file: " + err.Error())
}
func New(message string) error
Creates a new error with the specified message.
func NewF(format string, a ...any) error
Creates a new formatted error.
func SetLogTypes(types ...logType)
Configures the logging types (e.g., JSON, text, file).
func SetLogFile(filePath string) error
Sets the log file path for logging.
func Wrap(err error, args ...any) error
Wraps an existing error with additional context.
func Wrapf(err error, format string, args ...any) error
Formats a message and wraps it around an existing error.
func Unwrap(err error) string
Returns the original error message from a wrapped error.
func UnwrapE(err error) error
Returns the original error from a wrapped error.
func Log(err error, req any, msgs ...any)
Logs an error asynchronously with additional context messages.
func Join(sep string, errors ...error) error
Joins multiple errors into a single error.
func JoinMsg(sep string, a ...any) string
Joins multiple arguments into a single string.
func Is(err error, target error) bool
Checks if any error in the chain matches the target error.
func IsNil(err error) bool
Checks if an error is nil.