Documentation
¶
Overview ¶
Package errors provides unified error handling for Go services. It implements structured error types with error codes, HTTP status mapping, and retryable detection following RFC 7807 and Google AIP-193.
Package errors provides unified error handling for Go services. It implements structured error types with error codes, HTTP status mapping, and retryable detection following RFC 7807 and Google AIP-193.
Index ¶
- func IsAppError(err error) bool
- func IsRetryableCode(code ErrorCode) bool
- type AppError
- func AlreadyExists(resource string) *AppError
- func AsAppError(err error) (*AppError, bool)
- func Conflict(reason string) *AppError
- func ConnectionFailed(service string) *AppError
- func DatabaseError(cause error) *AppError
- func ExternalServiceError(service string, cause error) *AppError
- func Forbidden(reason string) *AppError
- func FormatResourceError(resource string, id any) *AppError
- func Internal(cause error) *AppError
- func InvalidFormat(field, expectedFormat string) *AppError
- func InvalidInput(field, reason string) *AppError
- func InvalidToken() *AppError
- func MissingField(field string) *AppError
- func New(code ErrorCode, message string, httpStatus int) *AppError
- func NotFound(resource, id string) *AppError
- func RateLimited() *AppError
- func ServiceUnavailable(service string) *AppError
- func Timeout(operation string) *AppError
- func TokenExpired() *AppError
- func Unauthorized(reason string) *AppError
- func Validation(message string) *AppError
- func Wrap(err error) *AppError
- type ErrorBody
- type ErrorCode
- type ErrorResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsRetryableCode ¶
IsRetryableCode returns true if the error code indicates a retryable error.
Types ¶
type AppError ¶
type AppError struct {
// Code is a machine-readable error code.
Code ErrorCode `json:"code"`
// Message is a human-readable error message.
Message string `json:"message"`
// Retryable indicates if the operation can be retried.
Retryable bool `json:"retryable"`
// HTTPStatus is the recommended HTTP status code for this error.
HTTPStatus int `json:"-"`
// Details contains additional context for the error.
Details map[string]any `json:"details,omitempty"`
// Cause is the underlying error that caused this error.
Cause error `json:"-"`
}
AppError is the unified application error type.
func AlreadyExists ¶
AlreadyExists creates a new AppError for a resource that already exists.
func AsAppError ¶
AsAppError converts an error to an AppError if possible.
func Conflict ¶
Conflict creates a new AppError for a conflict with the current state of the resource.
func ConnectionFailed ¶
ConnectionFailed creates a new AppError for a failed connection to a service.
func DatabaseError ¶
DatabaseError creates a new AppError for a database error.
func ExternalServiceError ¶
ExternalServiceError creates a new AppError for an error from an external service.
func FormatResourceError ¶
FormatResourceError creates a not-found error with a formatted identifier.
func InvalidFormat ¶
InvalidFormat creates a new AppError for an invalid field format.
func InvalidInput ¶
InvalidInput creates a new AppError for invalid input.
func InvalidToken ¶
func InvalidToken() *AppError
InvalidToken creates a new AppError for an invalid authentication token.
func MissingField ¶
MissingField creates a new AppError for a missing required field.
func RateLimited ¶
func RateLimited() *AppError
RateLimited creates a new AppError for too many requests.
func ServiceUnavailable ¶
ServiceUnavailable creates a new AppError for a service that is temporarily unavailable.
func TokenExpired ¶
func TokenExpired() *AppError
TokenExpired creates a new AppError for an expired authentication token.
func Unauthorized ¶
Unauthorized creates a new AppError for unauthorized access.
func Validation ¶
Validation creates a new AppError for validation errors.
func Wrap ¶
Wrap converts a standard error to an AppError. If the error is already an AppError it is returned as-is; otherwise it is wrapped as Internal. Returns nil when err is nil.
func (*AppError) ToResponse ¶
func (e *AppError) ToResponse() ErrorResponse
ToResponse converts an AppError to an ErrorResponse for JSON serialization.
func (*AppError) WithCause ¶
WithCause sets the underlying cause of the error and returns the receiver.
func (*AppError) WithDetail ¶
WithDetail sets a single detail key-value pair and returns the receiver.
type ErrorBody ¶
type ErrorBody struct {
// Code is a machine-readable error code.
Code ErrorCode `json:"code"`
// Message is a human-readable error message.
Message string `json:"message"`
// Retryable indicates if the operation can be retried.
Retryable bool `json:"retryable"`
// Details contains additional context for the error.
Details map[string]any `json:"details,omitempty"`
}
ErrorBody contains the error details sent to clients.
type ErrorCode ¶
type ErrorCode string
ErrorCode represents a machine-readable error code.
const ( ErrCodeServiceUnavailable ErrorCode = "SERVICE_UNAVAILABLE" // ErrCodeConnectionFailed indicates a failed connection to a service. ErrCodeConnectionFailed ErrorCode = "CONNECTION_FAILED" // ErrCodeTimeout indicates the request timed out. ErrCodeTimeout ErrorCode = "TIMEOUT" // ErrCodeRateLimited indicates the client is rate limited. ErrCodeRateLimited ErrorCode = "RATE_LIMITED" )
Connection/Availability errors (retryable)
const ( // ErrCodeNotFound indicates the requested resource was not found. ErrCodeNotFound ErrorCode = "NOT_FOUND" // ErrCodeAlreadyExists indicates the resource already exists. ErrCodeAlreadyExists ErrorCode = "ALREADY_EXISTS" // ErrCodeConflict indicates a conflict with the current state of the resource. ErrCodeConflict ErrorCode = "CONFLICT" )
Resource errors
const ( // ErrCodeInvalidInput indicates the input is invalid. ErrCodeInvalidInput ErrorCode = "INVALID_INPUT" // ErrCodeMissingField indicates a required field is missing. ErrCodeMissingField ErrorCode = "MISSING_FIELD" // ErrCodeInvalidFormat indicates a field has an invalid format. ErrCodeInvalidFormat ErrorCode = "INVALID_FORMAT" )
Validation errors
const ( ErrCodeUnauthorized ErrorCode = "UNAUTHORIZED" // ErrCodeForbidden indicates the request is forbidden. ErrCodeForbidden ErrorCode = "FORBIDDEN" // ErrCodeTokenExpired indicates the authentication token has expired. ErrCodeTokenExpired ErrorCode = "TOKEN_EXPIRED" // ErrCodeInvalidToken indicates the authentication token is invalid. ErrCodeInvalidToken ErrorCode = "INVALID_TOKEN" )
Authentication/Authorization errors
const ( // ErrCodeInternal indicates an internal server error. ErrCodeInternal ErrorCode = "INTERNAL_ERROR" // ErrCodeDatabaseError indicates a database error. ErrCodeDatabaseError ErrorCode = "DATABASE_ERROR" // ErrCodeExternalService indicates an error from an external service. ErrCodeExternalService ErrorCode = "EXTERNAL_SERVICE_ERROR" )
Internal errors
type ErrorResponse ¶
type ErrorResponse struct {
// Error contains the error details.
Error ErrorBody `json:"error"`
}
ErrorResponse is the JSON structure returned to clients following RFC 7807.