Wherr

Phillip England_

Wherr 🚨

Wherr is a Go package designed for structured error reporting
that automatically captures file path and line number information,
making debugging and logging easier. It wraps standard Go errors while
providing methods for clear console output and immediate program exit on
failure.

📦 Installation

To use wherr in your Go project, run:

1go get github.com/phillip-england/wherr

🚀 Usage

1. Capturing Location

The core function is Here(), which captures the file and line number
of the call site.

1import "wherr"
2
3location := wherr.Here()
4// location now contains the file path, line number, and working directory

2. Creating New Errors (Err)

Use Err to create a new *Wherr error with a custom message,
embedding the location information.

1func someFunction() error {
2  // ... logic ...
3  return wherr.Err(wherr.Here(), "failed to initialize resource: %s", "db")
4}

3. Wrapping Existing Errors (Consume)

Use Consume to wrap an existing error (e.g., from an I/O operation),
prepending a context message and embedding the location. Returns nil
if the input error is nil.

1func readFile(path string) error {
2  data, err := os.ReadFile(path)
3  if err != nil {
4    // Automatically wraps 'err' and adds the context
5    return wherr.Consume(wherr.Here(), err, "could not read file")
6  }
7  return nil
8}

✨ Features and Methods

The *Wherr type implements the standard error interface and provides
additional utility methods.

Error() string

Implements the error interface, returning a formatted string with ANSI
escape codes for colored terminal output.

Format: [WHERR][relative/path/to/file.go:123]: Your error message

Unwrap() error

Implements the standard Go errors.Wrapper interface, returning the
original wrapped error (e.Err). This allows external tools like
errors.Is() and errors.As() to function correctly.

Print()

Prints the error message, relative path, and line number to
os.Stderr.

1func execute() {
2  if err := someFunction(); err != nil {
3    if w, ok := err.(*wherr.Wherr); ok {
4      w.Print() // Prints to stderr: 🚨 path/to/file.go:123 — error message
5    }
6  }
7}

Fail()

Calls e.Print() to display the error and then calls os.Exit(1)
to immediately terminate the program with a non-zero exit code.