errcol

package module
v0.9.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 3 Imported by: 0

README

errcol - A wide-event inspired error collector

Go Reference

Inspired by wide-event logging, I wanted to be able to collect all errors that occurred in a request flow to then output them at the conclusion of the event.

A core goal of this package is having errors that tell me exactly what went wrong, where, was this expected (i.e. a malformed input), and the context around the error. Unexpected errors are items that should warrant attention.

Design

The error collector contains a slice of errors, a mutex for locking, and a boolean representing the file and line information where an error was generated should be included.

Each error contains the following attribute:

  • Error - String value of the collected error
  • Severity - Severity of an error, using Zerolog's levels
  • Expected - Boolean indicating whether an error is expected (e.g. malformed user input, invalid ID, etc.) or not (issue with the server).
  • Fields - Map that can contain additional context
  • Message - String representing the error (e.g. "failed to retrieve object")
  • CallerInfo - Optional attribute that includes the file path and line from where the error was called

Usage

For every event, a new error collector should be created and then passed through the event lifecycle. I recommend using a context to store the error collector.

When an error occurs, it should be added in the following way:

// Assume the errCol variable represents an ErrorCollector
errCol.Add(err, zerolog.ErrorLevel, false, nil, "failed to perform foo")

At the conclusion of an event, apply the error collector to a Zerolog event:

logEvent := errCol.ApplyToEvent(logEvent)
Example
// Assume that the ok value is always true
errCol, _ := ctx.Value(errorCollectorKey).(*errcol.ErrorCollector)

// Handling expected errors
var example any
err := json.Unmarshal(input, &example)
if err != nil {
    errCol.Add(err, zerolog.InfoLevel, true, nil, "input value is in invalid format")
}

var inputText string
if len(inputText) > 128 {
    errCol.Add(nil, zerolog.InfoLevel, true, map[string]any{"input_text": inputText}, "input text length is greater than 128")
}

// Handling unexpected errors
err := UpdateActionInDB(actionId)
if err != nil {
    errCol.Add(err, zerolog.ErrorLevel, false, map[string]any{"action_id": actionId}, "failed to update action in database")
}

err := generateSecureHash(plaintext)
if err != nil {
    errCol.Add(err, zerolog.ErrorLevel, false, nil, "failed to generate secure hash")
}

Feel free to look throughout the Cierge server package for examples, specifically including the logger middleware.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallerInfo

type CallerInfo struct {
	File string `json:"file"`
	Line int    `json:"line"`
}

Stores the information on the caller, getting the last frame. The File is the full path of the file that called the error and Line is the line number that the error was added from

type ErrorCollector

type ErrorCollector struct {
	// contains filtered or unexported fields
}

An error collector that stores a list of errors

func NewErrorCollector

func NewErrorCollector(callerInfo bool) *ErrorCollector

Creates a new error collector If callerInfo is set to true, the file and line information of where an error was called is stored with an error

func (*ErrorCollector) Add

func (e *ErrorCollector) Add(err error, severity zerolog.Level, expected bool, fields map[string]any, message string)

Add an error to the error collector

func (*ErrorCollector) ApplyToEvent

func (e *ErrorCollector) ApplyToEvent(event *zerolog.Event) *zerolog.Event

Applies all of the errors to a given zerolog event, setting them all to the errors field

func (*ErrorCollector) HasErrors

func (e *ErrorCollector) HasErrors() bool

Checks if the error collector has any errors and returns true if so

func (*ErrorCollector) HighestSeverity

func (e *ErrorCollector) HighestSeverity() ErrorInfo

Returns the highest severity error that the error collector has. If there are multiple of the same severity it will return the last one added

type ErrorInfo

type ErrorInfo struct {
	Error      string         `json:"error"`
	Severity   zerolog.Level  `json:"severity"`
	Expected   bool           `json:"expected"`
	Fields     map[string]any `json:"fields,omitempty"`
	Message    string         `json:"message"`
	CallerInfo CallerInfo     `json:"caller_info"`
}

Stores information about an error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL