Documentation
¶
Overview ¶
Package code provides business error codes with HTTP status mapping.
This package defines business-specific error codes and provides convenient functions for creating and wrapping errors with these codes.
Usage:
import (
"github.com/NSObjects/go-kit/errors" // for low-level operations
"github.com/NSObjects/go-kit/code" // for business errors
)
// Create business errors
err := code.NewNotFoundError("user")
err := code.WrapDatabaseError(dbErr, "query")
// Low-level operations (use errors package)
code := errors.GetCode(err)
httpStatus := errors.HTTPStatus(code)
Index ¶
- Constants
- func IsClientError(errCode int) bool
- func IsServerError(errCode int) bool
- func NewBadRequestError(message string) error
- func NewError(code int, message string) error
- func NewErrorf(code int, format string, args ...any) error
- func NewForbiddenError() error
- func NewNotFoundError(resource string) error
- func NewPermissionDeniedError(resource, action string) error
- func NewTokenExpiredError() error
- func NewTokenInvalidError() error
- func NewUnauthorizedError() error
- func NewValidationError(field, message string) error
- func WrapBadRequestError(err error, message string) error
- func WrapBindError(err error, message string) error
- func WrapDatabaseError(err error, operation string) error
- func WrapError(err error, code int, message string) error
- func WrapErrorf(err error, code int, format string, args ...any) error
- func WrapExternalError(err error, service, operation string) error
- func WrapForbiddenError(err error, message string) error
- func WrapInternalServerError(err error, message string) error
- func WrapKafkaError(err error, operation string) error
- func WrapNotFoundError(err error, message string) error
- func WrapRedisError(err error, operation string) error
- func WrapUnauthorizedError(err error, message string) error
- func WrapValidationError(err error, message string) error
- type ErrorCategory
- type ErrorInfo
- type ErrorType
Constants ¶
const ( // ErrSuccess - 200: OK. ErrSuccess int = iota + 100001 // ErrUnknown - 500: Internal server error. ErrUnknown // ErrBind - 400: Error occurred while binding the request body to the struct. ErrBind // ErrValidation - 400: Validation failed. ErrValidation // ErrTokenInvalid - 401: Token invalid. ErrTokenInvalid )
Basic errors (100001-100099)
const ( // ErrDatabase - 500: Database error. ErrDatabase int = iota + 100101 // ErrRedis - 500: Redis error. ErrRedis // ErrKafka - 500: Kafka error. ErrKafka // ErrExternalService - 500: External service error. ErrExternalService )
Database/Infrastructure errors (100101-100199)
const ( // ErrBadRequest - 400: Bad request. ErrBadRequest int = 100400 ErrUnauthorized int = 100401 // ErrForbidden - 403: Forbidden. ErrForbidden int = 100403 // ErrNotFound - 404: Not found. ErrNotFound int = 100404 // ErrInternalServer - 500: Internal server error. ErrInternalServer int = 100500 )
HTTP status code related errors (explicit values for clarity)
const ( // ErrEncrypt - 401: Error occurred while encrypting the user password. ErrEncrypt int = iota + 100201 // ErrSignatureInvalid - 401: Signature is invalid. ErrSignatureInvalid // ErrExpired - 401: Token expired. ErrExpired // ErrInvalidAuthHeader - 401: Invalid authorization header. ErrInvalidAuthHeader // ErrMissingHeader - 401: The Authorization header was empty. ErrMissingHeader // ErrPasswordIncorrect - 401: Password was incorrect. ErrPasswordIncorrect // ErrPermissionDenied - 403: Permission denied. ErrPermissionDenied // ErrAccountLocked - 403: Account is locked. ErrAccountLocked // ErrAccountDisabled - 403: Account is disabled. ErrAccountDisabled // ErrTooManyAttempts - 403: Too many login attempts. ErrTooManyAttempts )
Authentication/Authorization errors (100201-100299)
const ( // ErrEncodingFailed - 500: Encoding failed due to an error with the data. ErrEncodingFailed int = iota + 100301 // ErrDecodingFailed - 500: Decoding failed due to an error with the data. ErrDecodingFailed // ErrInvalidJSON - 500: Data is not valid JSON. ErrInvalidJSON // ErrEncodingJSON - 500: JSON data could not be encoded. ErrEncodingJSON // ErrDecodingJSON - 500: JSON data could not be decoded. ErrDecodingJSON // ErrInvalidYaml - 500: Data is not valid Yaml. ErrInvalidYaml // ErrEncodingYaml - 500: Yaml data could not be encoded. ErrEncodingYaml // ErrDecodingYaml - 500: Yaml data could not be decoded. ErrDecodingYaml )
Encoding/Decoding errors (100301-100399)
Variables ¶
This section is empty.
Functions ¶
func IsClientError ¶
IsClientError returns true if the error code represents a client error (4xx).
func IsServerError ¶
IsServerError returns true if the error code represents a server error (5xx).
func NewBadRequestError ¶
NewBadRequestError creates a bad request error.
func NewForbiddenError ¶
func NewForbiddenError() error
NewForbiddenError creates a forbidden error.
func NewNotFoundError ¶
NewNotFoundError creates a not found error.
func NewPermissionDeniedError ¶
NewPermissionDeniedError creates a permission denied error.
func NewTokenExpiredError ¶
func NewTokenExpiredError() error
NewTokenExpiredError creates a token expired error.
func NewTokenInvalidError ¶
func NewTokenInvalidError() error
NewTokenInvalidError creates a token invalid error.
func NewUnauthorizedError ¶
func NewUnauthorizedError() error
NewUnauthorizedError creates an unauthorized error.
func NewValidationError ¶
NewValidationError creates a validation error.
func WrapBadRequestError ¶
WrapBadRequestError wraps a 400 error.
func WrapBindError ¶
WrapBindError wraps a request binding error.
func WrapDatabaseError ¶
WrapDatabaseError wraps a database error.
func WrapErrorf ¶
WrapErrorf wraps an error with a code and formatted message.
func WrapExternalError ¶
WrapExternalError wraps an external service error.
func WrapForbiddenError ¶
WrapForbiddenError wraps a 403 error.
func WrapInternalServerError ¶
WrapInternalServerError wraps a 500 error.
func WrapKafkaError ¶
WrapKafkaError wraps a Kafka error.
func WrapNotFoundError ¶
WrapNotFoundError wraps a 404 error.
func WrapRedisError ¶
WrapRedisError wraps a Redis error.
func WrapUnauthorizedError ¶
WrapUnauthorizedError wraps a 401 error.
func WrapValidationError ¶
WrapValidationError wraps a validation error.
Types ¶
type ErrorCategory ¶
type ErrorCategory string
ErrorCategory represents the category of error.
const ( CategoryDatabase ErrorCategory = "database" CategoryRedis ErrorCategory = "redis" CategoryKafka ErrorCategory = "kafka" CategoryExternal ErrorCategory = "external" CategorySystem ErrorCategory = "system" CategoryAuth ErrorCategory = "auth" CategoryPermission ErrorCategory = "permission" CategoryValidation ErrorCategory = "validation" CategoryBusiness ErrorCategory = "business" )
type ErrorInfo ¶
type ErrorInfo struct {
Type ErrorType `json:"type"`
Category ErrorCategory `json:"category"`
Code int `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
}
ErrorInfo provides structured error information.
func NewErrorInfo ¶
NewErrorInfo creates ErrorInfo from an error.
func (*ErrorInfo) IsBusiness ¶
IsBusiness returns true if this is a business error.
func (*ErrorInfo) IsInternal ¶
IsInternal returns true if this is an internal error.