errors

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogger *zap.Logger

DefaultLogger is the default zap logger instance

Functions

func As

func As(err error, target interface{}) bool

As is a wrapper around errors.As for better error type assertion

func Error

func Error(w http.ResponseWriter, message string, code int)

Error is a drop-in replacement for http.Error

func ErrorHandler

func ErrorHandler(logger *zap.Logger) func(http.Handler) http.Handler

ErrorHandler wraps an http.Handler and provides error handling

func ErrorWithType

func ErrorWithType(w http.ResponseWriter, message string, errType ErrorType, code int)

ErrorWithType is like Error but allows specifying the error type

func LogError

func LogError(logger *zap.Logger, err error, requestID string)

LogError logs an error with its context

func SetLogger

func SetLogger(logger *zap.Logger)

SetLogger allows setting a custom logger

func WriteError

func WriteError(w http.ResponseWriter, err *HapaxError)

WriteError writes the error to the http.ResponseWriter

Types

type ErrorType

type ErrorType string

ErrorType represents different categories of errors

const (
	AuthError       ErrorType = "authentication_error"
	ValidationError ErrorType = "validation_error"
	RateLimitError  ErrorType = "rate_limit_error"
	ProviderError   ErrorType = "provider_error"
	InternalError   ErrorType = "internal_error"
)

type HapaxError

type HapaxError struct {
	Type      ErrorType              `json:"type"`
	Message   string                 `json:"message"`
	Code      int                    `json:"-"`
	RequestID string                 `json:"request_id"`
	Details   map[string]interface{} `json:"details,omitempty"`
	// contains filtered or unexported fields
}

HapaxError is our custom error type that carries additional context

func NewAuthError

func NewAuthError(requestID string, message string, err error) *HapaxError

NewAuthError creates a new authentication error

func NewInternalError

func NewInternalError(requestID string, err error) *HapaxError

NewInternalError creates a new internal server error

func NewProviderError

func NewProviderError(requestID string, message string, err error) *HapaxError

NewProviderError creates a new provider-related error

func NewRateLimitError

func NewRateLimitError(requestID string, retryAfter int) *HapaxError

NewRateLimitError creates a new rate limit error

func NewValidationError

func NewValidationError(requestID string, message string, validationDetails map[string]interface{}) *HapaxError

NewValidationError creates a new validation error

func (*HapaxError) Error

func (e *HapaxError) Error() string

Error implements the error interface

func (*HapaxError) Is

func (e *HapaxError) Is(target error) bool

Is implements error matching for errors.Is

func (*HapaxError) Unwrap

func (e *HapaxError) Unwrap() error

Unwrap returns the underlying error

Jump to

Keyboard shortcuts

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