errors

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package errors provides structured error types for the CQI infrastructure library. It defines error categories (Permanent, Temporary, NotFound, InvalidInput, Unauthorized) that enable consistent error handling across all infrastructure components.

Example usage:

if err := db.Query(ctx, sql); err != nil {
    return errors.NewTemporary("database connection lost", err)
}

if user == nil {
    return errors.NewNotFound("user", userID)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

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

As is a re-export of errors.As for convenient access in error handling code.

func DefaultRecoveryFunc

func DefaultRecoveryFunc(p interface{}) error

DefaultRecoveryFunc is the default recovery function that converts panics to PermanentErrors.

func GRPCStatus

func GRPCStatus(err error) *status.Status

GRPCStatus converts an error to a gRPC status. It maps error types to appropriate gRPC status codes:

  • NotFoundError -> codes.NotFound
  • InvalidInputError -> codes.InvalidArgument
  • UnauthorizedError -> codes.Unauthenticated
  • TemporaryError -> codes.Unavailable
  • PermanentError -> codes.Internal
  • Unknown errors -> codes.Unknown

func HTTPStatusCode

func HTTPStatusCode(err error) int

HTTPStatusCode returns the appropriate HTTP status code for the given error. It maps error types to standard HTTP status codes:

  • NotFoundError -> 404 Not Found
  • InvalidInputError -> 400 Bad Request
  • UnauthorizedError -> 401 Unauthorized
  • TemporaryError -> 503 Service Unavailable
  • PermanentError -> 500 Internal Server Error
  • Unknown errors -> 500 Internal Server Error

func Is

func Is(err, target error) bool

Is is a re-export of errors.Is for convenient access in error handling code.

func IsInvalidInput

func IsInvalidInput(err error) bool

IsInvalidInput checks if an error is or wraps an InvalidInputError.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound checks if an error is or wraps a NotFoundError.

func IsPermanent

func IsPermanent(err error) bool

IsPermanent checks if an error is or wraps a PermanentError.

func IsTemporary

func IsTemporary(err error) bool

IsTemporary checks if an error is or wraps a TemporaryError.

func IsUnauthorized

func IsUnauthorized(err error) bool

IsUnauthorized checks if an error is or wraps an UnauthorizedError.

func NewInvalidInput

func NewInvalidInput(field, msg string) error

NewInvalidInput creates a new invalid input error for the given field and message.

func NewInvalidInputWithCause

func NewInvalidInputWithCause(field, msg string, cause error) error

NewInvalidInputWithCause creates a new invalid input error with an underlying cause.

func NewNotFound

func NewNotFound(resource, id string) error

NewNotFound creates a new not found error for the given resource and ID.

func NewNotFoundWithCause

func NewNotFoundWithCause(resource, id string, cause error) error

NewNotFoundWithCause creates a new not found error with an underlying cause.

func NewPermanent

func NewPermanent(msg string, cause error) error

NewPermanent creates a new permanent error with the given message and optional cause.

func NewTemporary

func NewTemporary(msg string, cause error) error

NewTemporary creates a new temporary error with the given message and optional cause.

func NewUnauthorized

func NewUnauthorized(msg string) error

NewUnauthorized creates a new unauthorized error with the given message.

func NewUnauthorizedWithCause

func NewUnauthorizedWithCause(msg string, cause error) error

NewUnauthorizedWithCause creates a new unauthorized error with an underlying cause.

func RecoveryMiddleware

func RecoveryMiddleware(recoveryFunc RecoveryFunc) func(http.Handler) http.Handler

RecoveryMiddleware is an HTTP middleware that recovers from panics and converts them to errors. It takes an optional recovery function; if nil, DefaultRecoveryFunc is used.

func StreamServerInterceptor

func StreamServerInterceptor(recoveryFunc RecoveryFunc) grpc.StreamServerInterceptor

StreamServerInterceptor returns a gRPC stream server interceptor that recovers from panics. It takes an optional recovery function; if nil, DefaultRecoveryFunc is used.

func ToGRPCError

func ToGRPCError(err error) error

ToGRPCError converts an error to a gRPC error that can be returned from a gRPC handler.

func UnaryServerInterceptor

func UnaryServerInterceptor(recoveryFunc RecoveryFunc) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a gRPC unary server interceptor that recovers from panics. It takes an optional recovery function; if nil, DefaultRecoveryFunc is used.

func Wrap

func Wrap(err error, msg string) error

Wrap wraps an error with additional context while preserving the original error type. If err is already a typed error (Permanent, Temporary, etc.), it wraps it with the same type. Otherwise, it returns a PermanentError.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf wraps an error with a formatted message while preserving the original error type.

func WriteHTTPError

func WriteHTTPError(w http.ResponseWriter, err error)

WriteHTTPError writes an error response to an HTTP response writer. It automatically determines the status code based on the error type and writes a JSON error response.

Types

type InvalidInputError

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

InvalidInputError represents an error due to invalid user input. Examples: validation failures, malformed requests, missing required fields.

func (*InvalidInputError) Error

func (e *InvalidInputError) Error() string

func (*InvalidInputError) Field

func (e *InvalidInputError) Field() string

Field returns the field name that had invalid input.

func (*InvalidInputError) Message

func (e *InvalidInputError) Message() string

Message returns the validation error message.

func (*InvalidInputError) Unwrap

func (e *InvalidInputError) Unwrap() error

type NotFoundError

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

NotFoundError represents an error when a requested resource doesn't exist. Examples: user not found, asset not found, record not in database.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

func (*NotFoundError) ID

func (e *NotFoundError) ID() string

ID returns the identifier of the resource that wasn't found.

func (*NotFoundError) Resource

func (e *NotFoundError) Resource() string

Resource returns the type of resource that wasn't found.

func (*NotFoundError) Unwrap

func (e *NotFoundError) Unwrap() error

type PermanentError

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

PermanentError represents an error that won't succeed even if retried. Examples: invalid configuration, programming errors, data corruption.

func (*PermanentError) Error

func (e *PermanentError) Error() string

func (*PermanentError) Unwrap

func (e *PermanentError) Unwrap() error

type RecoveryFunc

type RecoveryFunc func(interface{}) error

RecoveryFunc is a function that handles a recovered panic. It receives the recovered value and returns an error.

type TemporaryError

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

TemporaryError represents an error that might succeed if retried. Examples: network timeouts, temporary service unavailability, rate limiting.

func (*TemporaryError) Error

func (e *TemporaryError) Error() string

func (*TemporaryError) Unwrap

func (e *TemporaryError) Unwrap() error

type UnauthorizedError

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

UnauthorizedError represents an authentication or authorization failure. Examples: invalid API key, expired JWT token, insufficient permissions.

func (*UnauthorizedError) Error

func (e *UnauthorizedError) Error() string

func (*UnauthorizedError) Unwrap

func (e *UnauthorizedError) Unwrap() error

Jump to

Keyboard shortcuts

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