errors

package
v1.0.65 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package errors provides rich error handling with context for AI provider operations.

This package re-exports the public API from internal/common/errors, providing external consumers with rich error context, credential masking, and debugging capabilities for AI provider operations.

Rich Errors

RichError wraps errors with comprehensive debugging context:

err := doAPICall()
if err != nil {
    richErr := errors.Wrap(err).
        WithRequestID(requestID).
        WithProvider(types.ProviderTypeOpenAI).
        WithModel("gpt-4").
        WithRequestSnapshot(req).
        WithResponseSnapshot(resp).
        WithTimingStart(startTime)

    log.Error(richErr.Format())
    return richErr
}

Error Context

ErrorContext captures structured information about when and where an error occurred:

ctx := errors.NewErrorContext().
    WithRequestID("req-123").
    WithCorrelationID("corr-456").
    WithProvider(types.ProviderTypeAnthropic).
    WithModel("claude-3-opus").
    WithOperation("chat_completion").
    WithDuration(150 * time.Millisecond)

Credential Masking

The package provides automatic masking of sensitive information in logs:

masker := errors.DefaultCredentialMasker()
safe := masker.MaskString(`{"api_key": "secret123"}`)
// Result: {"api_key": "***MASKED***"}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultCredentialMasker

func DefaultCredentialMasker() *errors.DefaultMasker

DefaultCredentialMasker creates a new credential masker with default patterns. The default masker handles common sensitive data patterns including:

  • Bearer tokens
  • API keys
  • Authorization headers
  • Passwords and secrets
  • AWS access keys

func DefaultSnapshotConfig

func DefaultSnapshotConfig() *errors.SnapshotConfig

DefaultSnapshotConfig returns the default snapshot configuration. The default configuration captures up to 4KB of body content with headers and uses the default credential masker.

func GetErrorContext

func GetErrorContext(ctx context.Context) *errors.ErrorContext

GetErrorContext retrieves the error context from a context.Context. Returns nil if no error context is stored in the context. This is typically used with middleware that captures error context.

func MaskURL

func MaskURL(url string) string

MaskURL masks sensitive information in URLs (query parameters, etc.).

func NewCredentialMasker

func NewCredentialMasker() *errors.DefaultMasker

NewCredentialMasker creates a new credential masker with no default patterns. Use this if you want complete control over what gets masked.

func NewErrorContext

func NewErrorContext() *errors.ErrorContext

NewErrorContext creates a new error context with the timestamp set to now.

func NewRequestSnapshot

func NewRequestSnapshot(req *http.Request, config *errors.SnapshotConfig) *errors.RequestSnapshot

NewRequestSnapshot creates a snapshot of an HTTP request. The request body will be read and restored so the request can still be used. Returns nil if req is nil.

func NewResponseSnapshot

func NewResponseSnapshot(resp *http.Response, config *errors.SnapshotConfig) *errors.ResponseSnapshot

NewResponseSnapshot creates a snapshot of an HTTP response. The response body will be read and restored so the response can still be used. Returns nil if resp is nil.

func NewRichError

func NewRichError(err error) *errors.RichError

NewRichError creates a new RichError wrapping the given error. The returned error has a default ErrorContext and SnapshotConfig attached.

func NewRichErrorWithConfig

func NewRichErrorWithConfig(err error, config *errors.SnapshotConfig) *errors.RichError

NewRichErrorWithConfig creates a new RichError with a custom snapshot configuration. Use this when you need to control snapshot behavior (body size limits, headers, etc.).

func Wrap

func Wrap(err error) *errors.RichError

Wrap wraps an error with rich context. This is a convenience function for NewRichError(err). Returns nil if err is nil.

func WrapWithContext

func WrapWithContext(err error, ctx *errors.ErrorContext) *errors.RichError

WrapWithContext wraps an error with an existing error context. Use this when you have already built an ErrorContext and want to attach it to an error. Returns nil if err is nil.

Types

type CredentialMasker

type CredentialMasker = errors.CredentialMasker

CredentialMasker provides methods to mask sensitive information in logs and errors.

type DefaultMasker

type DefaultMasker = errors.DefaultMasker

DefaultMasker is the default implementation of CredentialMasker.

type ErrorContext

type ErrorContext = errors.ErrorContext

ErrorContext contains contextual information about an error. This includes request/response snapshots, timing information, provider details, and correlation identifiers for tracing across services.

type RequestSnapshot

type RequestSnapshot = errors.RequestSnapshot

RequestSnapshot captures key information from an HTTP request for debugging. Sensitive information (API keys, tokens, etc.) is automatically masked.

type ResponseSnapshot

type ResponseSnapshot = errors.ResponseSnapshot

ResponseSnapshot captures key information from an HTTP response for debugging. Sensitive information (API keys, tokens, etc.) is automatically masked.

type RichError

type RichError = errors.RichError

RichError is an enhanced error type with context for debugging and tracing. It wraps an underlying error with structured context including request/response snapshots, timing information, correlation IDs, and provider details.

type SnapshotConfig

type SnapshotConfig = errors.SnapshotConfig

SnapshotConfig controls how request/response snapshots are created.

Jump to

Keyboard shortcuts

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