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 ¶
- func As(err error, target interface{}) bool
- func DefaultRecoveryFunc(p interface{}) error
- func GRPCStatus(err error) *status.Status
- func HTTPStatusCode(err error) int
- func Is(err, target error) bool
- func IsInvalidInput(err error) bool
- func IsNotFound(err error) bool
- func IsPermanent(err error) bool
- func IsTemporary(err error) bool
- func IsUnauthorized(err error) bool
- func NewInvalidInput(field, msg string) error
- func NewInvalidInputWithCause(field, msg string, cause error) error
- func NewNotFound(resource, id string) error
- func NewNotFoundWithCause(resource, id string, cause error) error
- func NewPermanent(msg string, cause error) error
- func NewTemporary(msg string, cause error) error
- func NewUnauthorized(msg string) error
- func NewUnauthorizedWithCause(msg string, cause error) error
- func RecoveryMiddleware(recoveryFunc RecoveryFunc) func(http.Handler) http.Handler
- func StreamServerInterceptor(recoveryFunc RecoveryFunc) grpc.StreamServerInterceptor
- func ToGRPCError(err error) error
- func UnaryServerInterceptor(recoveryFunc RecoveryFunc) grpc.UnaryServerInterceptor
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, args ...interface{}) error
- func WriteHTTPError(w http.ResponseWriter, err error)
- type InvalidInputError
- type NotFoundError
- type PermanentError
- type RecoveryFunc
- type TemporaryError
- type UnauthorizedError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultRecoveryFunc ¶
func DefaultRecoveryFunc(p interface{}) error
DefaultRecoveryFunc is the default recovery function that converts panics to PermanentErrors.
func GRPCStatus ¶
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 ¶
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 IsInvalidInput ¶
IsInvalidInput checks if an error is or wraps an InvalidInputError.
func IsNotFound ¶
IsNotFound checks if an error is or wraps a NotFoundError.
func IsPermanent ¶
IsPermanent checks if an error is or wraps a PermanentError.
func IsTemporary ¶
IsTemporary checks if an error is or wraps a TemporaryError.
func IsUnauthorized ¶
IsUnauthorized checks if an error is or wraps an UnauthorizedError.
func NewInvalidInput ¶
NewInvalidInput creates a new invalid input error for the given field and message.
func NewInvalidInputWithCause ¶
NewInvalidInputWithCause creates a new invalid input error with an underlying cause.
func NewNotFound ¶
NewNotFound creates a new not found error for the given resource and ID.
func NewNotFoundWithCause ¶
NewNotFoundWithCause creates a new not found error with an underlying cause.
func NewPermanent ¶
NewPermanent creates a new permanent error with the given message and optional cause.
func NewTemporary ¶
NewTemporary creates a new temporary error with the given message and optional cause.
func NewUnauthorized ¶
NewUnauthorized creates a new unauthorized error with the given message.
func NewUnauthorizedWithCause ¶
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 ¶
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 ¶
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 ¶
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