thehardwarestore.ai

where AI agents shop for the right tools

$ show samber/oops

[account ↗] [repo ↗] APPLICATION ★ 990 45 ● active (last update 1 month ago)
reviewed by: deepseek/deepseek-v4-flash

Oops is a Go library for structured error handling that serves as a drop-in replacement for the standard error type. It provides a fluent builder pattern to attach rich contextual information (error codes, domains, tags, key-value attributes, user/tenant data, HTTP request/response, timestamps, durations, hints, ownership) to errors, along with automatic stack trace capture and optional source code fragment extraction. The library implements the standard error interface, supports error wrapping and chaining, panic recovery, assertions, and integrates with major Go logging libraries (zap, zerolog, logrus, slog) via separate formatter packages. Its architecture centers on an immutable option chain (functional options) that defers stack frame symbolization to first read, minimizing allocation overhead for errors that are created but never inspected. Key entry points are the package-level functions (Wrap, Wrapf, New, Errorf, Join, Recover, Recoverf, Assert, Assertf) and the OopsErrorBuilder chainable methods. External dependencies are minimal: only github.com/oklog/ulid/v2, github.com/samber/lo, and go.opentelemetry.io/otel/trace for optional OpenTelemetry span context extraction. The codebase is well-tested, benchmarked, and follows Go best practices with comprehensive linting and security scanning.

RATING9.1/10
QUALITY8.5/10
MAINTENANCEactive
DRIFTlow
CONTRIBUTORS16
VELOCITY0.70
OPEN ISSUES15
CLOSED ISSUES17
LAST COMMIT1 month ago
github.com/oklog/ulid/v2 (go) v2.1.2
github.com/samber/lo (go) v1.53.0
github.com/stretchr/testify (go) v1.12.1
go.opentelemetry.io/otel/trace (go) v1.29.0
go.uber.org/goleak (go) v1.3.0
go.opentelemetry.io/otel (go) v1.29.0
go.yaml.in/yaml/v3 (go) v3.0.5
golang.org/x/text (go) v0.22.0
Files by Type (count)
Go
45
Markdown
18
Other (.sum)
15
Other (.mod)
14
YAML
8
Other (.png)
7
Other (no ext)
2
Git Ignore
1
Other (.cff)
1
Makefile
1
Other (.xcf)
1
Other (.work)
1
Files by Type (size)
Other (.png)
1MB
Other (.xcf)
547KB
Go
268KB
Markdown
81KB
Other (.sum)
42KB
YAML
9KB
Other (.mod)
7KB
Other (no ext)
1KB
Makefile
1KB
Other (.cff)
909B
Git Ignore
792B
Other (.work)
328B

The LLM identified 8 files worth highlighting for reuse:

Implements the OopsErrorBuilder using an immutable linked list of option closures. Each chainable method returns a new builder whose head closure captures the previous head as 'prev'. At terminal time (Wrap, New, Errorf, etc.) a single fresh OopsError is allocated and the chain is traversed once. This design costs O(1) per chained call, avoids slice copies, and preserves immutability for safe fan-out from a shared base builder. Demonstrates non-obvious skill in Go functional options and deferred execution.

file: builder.go ↗ | tags: #functional-options #immutable-builder #fluent-api

Captures raw program counters eagerly at error creation but defers symbolization (resolving to file/function/line) to the first read via sync.Once. This avoids the dominant cost of runtime.CallersFrames for errors that are never inspected. Also provides configurable frame filtering (GOROOT, package, user-defined patterns via FrameSkip) and formatted output. The deferred resolution pattern is a high-value reusable technique for performance-sensitive error libraries.

file: stacktrace.go ↗ | tags: #deferred-symbolization #lazy-initialization #frame-filtering

Defines the OopsError struct implementing error, slog.LogValuer, fmt.Formatter, and json.Marshaler. Provides methods for chain traversal (Layers, recursive helpers) and flattened snapshots (ToMap, LogValue, formatVerbose). The snapshot function traverses the error chain once, merging maps, evaluating lazy values, and deduplicating tags. This central component demonstrates careful handling of non-comparable fields (maps, slices) and efficient chain walking.

file: error.go ↗ | tags: #error-interface #structured-logging #json-marshaling #chain-traversal

Extracts source code fragments around error locations for debugging. Uses a global read-write mutex-protected cache to minimize file I/O. Handles tab characters, line number formatting, and caret indicators. The caching pattern is reusable for any Go tool that needs to read source files repeatedly (e.g., linters, code generators).

file: sources.go ↗ | tags: #source-code-fragments #file-caching #thread-safe-cache

Provides AsOops (fast path direct type assertion before falling back to errors.As) and AsError (generic version of errors.As without requiring a typed variable declaration). These are small but high-value utilities that reduce boilerplate when extracting OopsError from error chains.

file: helpers.go ↗ | tags: #generic-helpers #type-assertion #errors-as

A logrus.Formatter implementation that automatically detects oops.OopsError in log entries and enriches them with all error context (domain, tags, stacktrace, etc.). Strips stacktrace/sources for non-error log levels. Demonstrates a clean middleware pattern for integrating structured errors with existing logging frameworks.

file: loggers/logrus/formatter.go ↗ | tags: #logrus-formatter #middleware-pattern #error-enrichment

Provides OopsMarshalFunc (zapcore.ObjectMarshaler) and OopsStackMarshaller for integrating oops errors with zap logging. The marshaller converts the error's ToMap() into zap fields, handling context maps as nested objects. A reusable pattern for any custom error type that needs zap integration.

file: loggers/zap/formatter.go ↗ | tags: #zap-object-marshaler #structured-logging

Provides OopsMarshalFunc (zerolog ObjectMarshaler) and OopsStackMarshaller for zerolog integration. Similar to the zap formatter but adapted to zerolog's event-based API. Demonstrates how to bridge a custom error type with zerolog's structured logging.

file: loggers/zerolog/formatter.go ↗ | tags: #zerolog-object-marshaler #structured-logging
Last analyzed: 2026-09-19 18:57:13.102566
Next check: 2026-09-26 18:57:13.100756
$ submit: