Documentation
¶
Overview ¶
Package errors provides a structured error handling system with rich context. It includes error codes, HTTP/gRPC status mapping, validation helpers, and detail support. It is designed to be idiomatic and interoperate with standard library errors and google.golang.org/grpc/status.
Index ¶
- func FromGRPCError(err error) error
- func GetGRPCCode(err error) codes.Code
- func GetHTTPStatus(err error) int
- func Is(err error, code Code) bool
- type Code
- type Detail
- type Error
- func AlreadyExists(resource string, identifier interface{}, opts ...Option) Error
- func As(err error) (Error, bool)
- func Conflict(message string, opts ...Option) Error
- func Forbidden(message string, opts ...Option) Error
- func InternalError(message string, opts ...Option) Error
- func InvalidArgument(message string, opts ...Option) Error
- func InvalidFormat(field string, value interface{}, expectedFormat string, opts ...Option) Error
- func MissingField(field string, opts ...Option) Error
- func New(code Code, opts ...Option) Error
- func NotFound(resource string, identifier interface{}, opts ...Option) Error
- func OutOfRange(field string, value interface{}, min, max interface{}, opts ...Option) Error
- func RateLimited(message string, opts ...Option) Error
- func ServiceUnavailable(message string, opts ...Option) Error
- func Timeout(message string, opts ...Option) Error
- func Unauthenticated(message string, opts ...Option) Error
- func Unauthorized(message string, opts ...Option) Error
- func ValidationError(opts ...ValidationOption) Error
- func ValidationFailed(message string, opts ...Option) Error
- func Wrap(err error, code Code, opts ...Option) Error
- func WrapInternal(err error, message string, opts ...Option) Error
- func WrapNotFound(err error, resource string, identifier interface{}, opts ...Option) Error
- func WrapValidation(err error, message string, opts ...Option) Error
- type Option
- func WithDetail(field string, code Code, message string, value interface{}) Option
- func WithDetails(details ...Detail) Option
- func WithGRPCCode(code codes.Code) Option
- func WithHTTPStatus(status int) Option
- func WithMessage(message string) Option
- func WithRequestID(requestID string) Option
- func WithService(service string) Option
- type ValidationOption
- func WithCustomValidation(field string, code Code, message string, value interface{}) ValidationOption
- func WithInvalidFormat(field string, value interface{}, expectedFormat string) ValidationOption
- func WithOutOfRange(field string, value interface{}, min, max interface{}) ValidationOption
- func WithRequiredField(field string) ValidationOption
- func WithValidationDetail(detail Detail) ValidationOption
- func WithValidationDetails(details ...Detail) ValidationOption
- func WithValidationMessage(message string) ValidationOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromGRPCError ¶
FromGRPCError extracts our custom error from a gRPC status error
func GetGRPCCode ¶
GetGRPCCode extracts gRPC code from error, defaults to Unknown
func GetHTTPStatus ¶
GetHTTPStatus extracts HTTP status from error, defaults to 500
Types ¶
type Code ¶
type Code string
Code represents an internal error code
const ( // Validation errors CodeValidationFailed Code = "VALIDATION_FAILED" CodeInvalidArgument Code = "INVALID_ARGUMENT" CodeMissingField Code = "MISSING_FIELD" CodeInvalidFormat Code = "INVALID_FORMAT" CodeOutOfRange Code = "OUT_OF_RANGE" // Resource errors CodeNotFound Code = "NOT_FOUND" CodeAlreadyExists Code = "ALREADY_EXISTS" CodeConflict Code = "CONFLICT" CodeTaken Code = "TAKEN" // Authentication/Authorization errors CodeUnauthenticated Code = "UNAUTHENTICATED" CodeForbidden Code = "FORBIDDEN" // System errors CodeInternalError Code = "INTERNAL_ERROR" CodeTimeout Code = "TIMEOUT" CodeRateLimited Code = "RATE_LIMITED" // Business logic errors CodeBusinessRuleViolation Code = "BUSINESS_RULE_VIOLATION" CodeOperationNotAllowed Code = "OPERATION_NOT_ALLOWED" CodeResourceLocked Code = "RESOURCE_LOCKED" // Rate limiting and capacity CodeQuotaExceeded Code = "QUOTA_EXCEEDED" CodeCapacityExceeded Code = "CAPACITY_EXCEEDED" // Data integrity CodeDataCorruption Code = "DATA_CORRUPTION" CodeChecksumMismatch Code = "CHECKSUM_MISMATCH" // Network and connectivity CodeNetworkError Code = "NETWORK_ERROR" CodeConnectionFailed Code = "CONNECTION_FAILED" CodeDNSResolutionFailed Code = "DNS_RESOLUTION_FAILED" // File and storage CodeFileNotFound Code = "FILE_NOT_FOUND" CodeStorageError Code = "STORAGE_ERROR" CodeInsufficientSpace Code = "INSUFFICIENT_SPACE" // Configuration and setup CodeConfigurationError Code = "CONFIGURATION_ERROR" CodeMissingDependency Code = "MISSING_DEPENDENCY" CodeVersionMismatch Code = "VERSION_MISMATCH" // Resource state errors CodeGone Code = "GONE" CodePreconditionFailed Code = "PRECONDITION_FAILED" // External service errors CodeExternalService Code = "EXTERNAL_SERVICE_ERROR" CodeDatabaseError Code = "DATABASE_ERROR" )
Common error codes
type Detail ¶
type Detail interface {
Field() string // Field name (if applicable)
Code() Code // Detail-specific error code
Message() string // Detail-specific message
Value() interface{} // The value that caused the error
}
Detail represents a specific error detail (e.g., field validation error)
type Error ¶
type Error interface {
error // Embeds standard error interface
// Core error information
Code() Code // Internal error code (e.g., VALIDATION_FAILED)
Message() string // Human-readable message
// Protocol-specific codes
HTTPStatus() int // HTTP status code (400, 500, etc.)
GRPCCode() codes.Code // gRPC status code
// Context and details
Details() []Detail // Field-level or additional error details
Cause() error // Underlying error (for wrapping)
// Metadata
Timestamp() time.Time // When the error occurred
RequestID() string // Request correlation ID
Service() string // Service that generated the error
}
Error represents a structured error with rich context
func AlreadyExists ¶
AlreadyExists creates an already exists error
func InternalError ¶
InternalError creates an internal error
func InvalidArgument ¶
InvalidArgument creates an invalid input error
func InvalidFormat ¶
InvalidFormat creates an invalid format validation error
func MissingField ¶
MissingField creates a missing field validation error
func OutOfRange ¶
OutOfRange creates an out of range validation error
func RateLimited ¶
RateLimited creates a rate limited error
func ServiceUnavailable ¶
ServiceUnavailable creates a service unavailable error
func Unauthenticated ¶
Unauthenticated creates an unauthenticated error
func Unauthorized ¶
Unauthorized creates an unauthorized error (alias for Unauthenticated for clarity)
func ValidationError ¶
func ValidationError(opts ...ValidationOption) Error
ValidationError creates a validation error using functional options
func ValidationFailed ¶
ValidationFailed creates a validation error with field details
func WrapInternal ¶
WrapInternal wraps an error as an internal error
func WrapNotFound ¶
WrapNotFound wraps an error as a not found error
type Option ¶
type Option func(*errorImpl)
Option represents a functional option for configuring an Error
func WithDetail ¶
WithDetail adds a detail to the error
func WithDetails ¶
WithDetails adds multiple details to the error
func WithGRPCCode ¶
WithGRPCCode sets the gRPC status code
func WithHTTPStatus ¶
WithHTTPStatus sets the HTTP status code
func WithMessage ¶
WithMessage sets the human-readable error message
func WithRequestID ¶
WithRequestID sets the request correlation ID
func WithService ¶
WithService sets the service that generated the error
type ValidationOption ¶
type ValidationOption func(*validationConfig)
ValidationOption configures validation errors
func WithCustomValidation ¶
func WithCustomValidation(field string, code Code, message string, value interface{}) ValidationOption
WithCustomValidation adds a custom field error
func WithInvalidFormat ¶
func WithInvalidFormat(field string, value interface{}, expectedFormat string) ValidationOption
WithInvalidFormat adds an invalid format error
func WithOutOfRange ¶
func WithOutOfRange(field string, value interface{}, min, max interface{}) ValidationOption
WithOutOfRange adds an out of range error
func WithRequiredField ¶
func WithRequiredField(field string) ValidationOption
WithRequiredField adds a required field error
func WithValidationDetail ¶
func WithValidationDetail(detail Detail) ValidationOption
WithValidationDetail adds a pre-built detail
func WithValidationDetails ¶
func WithValidationDetails(details ...Detail) ValidationOption
WithValidationDetails adds multiple pre-built details
func WithValidationMessage ¶
func WithValidationMessage(message string) ValidationOption
WithValidationMessage sets the overall validation message