Documentation
¶
Index ¶
- Constants
- func GetCategoryDescription(xyy string) string
- func GetCategoryHTTPStatus(xyy string) int
- func GetFullCode(code string) string
- func GetServicePrefix() string
- func IsValidCategory(xyy string) bool
- func NewAuthenticationError(message string, data interface{}) error
- func NewBadRequestError(message string, data interface{}) error
- func NewClientError(message string, data interface{}) error
- func NewConflictError(message string, data interface{}) error
- func NewDatabaseError(message string, data interface{}) error
- func NewForbiddenError(message string, data interface{}) error
- func NewInternalServerError(message string, data interface{}) error
- func NewNotFoundError(message string, data interface{}) error
- func NewThirdPartyError(message string, data interface{}) error
- func NewUnauthorizedError(message string, data interface{}) error
- func NewUnprocessableEntityError(message string, data interface{}) error
- func SetServicePrefix(prefix string)
- func WrapError(original, new error) error
- func WrapErrorWithPrefix(prefix string, errptr *error)
- type AuthenticationError
- type BadRequestError
- type BaseError
- type ClientError
- type ConflictError
- type DatabaseError
- type DomainError
- type ForbiddenError
- type InternalServerError
- type NotFoundError
- type ThirdPartyError
- type UnauthorizedError
- type UnprocessableEntityError
Constants ¶
const ( // Success (2yyzzz) StatusCodeSuccess = "200000" // General Success StatusCodePartialSuccess = "201000" // Partial Success (e.g., batch processing) StatusCodeAccepted = "202000" // Accepted (e.g., long-running task queued) // Client Errors (4yyzzz) StatusCodeGenericClientError = "400000" // General Client Error StatusCodeGenericBadRequestError = "401000" // Bad Request (e.g., missing or invalid parameters) StatusCodeGenericNotFoundError = "402000" // Not Found (e.g., resource not found) StatusCodeGenericConflictError = "403000" // Conflict (e.g., resource already exists) StatusCodeGenericUnprocessableEntityError = "404000" // Unprocessable Entity (e.g., validation error) // Server Errors (5yyzzz) StatusCodeGenericInternalServerError = "500000" // General Internal Server Error StatusCodeGenericDatabaseError = "501000" // Database Error StatusCodeGenericThirdPartyError = "502000" // Third-party Error // Authentication and Authorization Errors (9yyzzz) StatusCodeGenericAuthError = "900000" // General Authentication Error StatusCodeGenericForbiddenError = "902000" // Forbidden (e.g., insufficient permissions) )
Error code constants following the 'xyyzzz' ([x][yy][zzz]) convention.
- 'x' - Main category,
- 'yy' - Subcategory,
- 'zzz' - Specific error code within the subcategory.
const DefaultServicePrefix = "ERR" // DefaultServicePrefix is the default prefix used for errors.
Variables ¶
This section is empty.
Functions ¶
func GetCategoryDescription ¶
GetCategoryDescription returns the description of the 'xyy' category. If the category does not exist, it returns "Unknown Category".
func GetCategoryHTTPStatus ¶
GetCategoryHTTPStatus returns the HTTP status code of the 'xyy' category. If the category does not exist, it returns 500.
func GetFullCode ¶
GetFullCode constructs the full error code with the service prefix. If servicePrefix is "SVC" and code is "200000", it returns "SVC-200000".
func GetServicePrefix ¶
func GetServicePrefix() string
GetServicePrefix returns the current service prefix.
func IsValidCategory ¶
IsValidCategory validates the 'xyy' part of an error code. It returns true if the category exists, and false otherwise.
func NewAuthenticationError ¶
NewAuthenticationError creates a new AuthenticationError instance using the generic authentication error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewBadRequestError ¶
NewBadRequestError creates a new BadRequestError instance using the generic bad request error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewClientError ¶
NewClientError creates a new ClientError instance using the generic client error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewConflictError ¶
NewConflictError creates a new ConflictError instance using the generic conflict error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewDatabaseError ¶
NewDatabaseError creates a new DatabaseError instance using the generic database error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewForbiddenError ¶
NewForbiddenError creates a new ForbiddenError instance using the generic forbidden error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewInternalServerError ¶
NewInternalServerError creates a new InternalServerError instance using the generic internal error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewNotFoundError ¶
NewNotFoundError creates a new NotFoundError instance using the generic not found error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewThirdPartyError ¶
NewThirdPartyError creates a new ThirdPartyError instance using the generic third-party error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewUnauthorizedError ¶
NewUnauthorizedError creates a new UnauthorizedError instance using the generic unauthorized error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func NewUnprocessableEntityError ¶
NewUnprocessableEntityError creates a new UnprocessableEntityError instance using the generic unprocessable entity error code. If the `message` parameter is an empty string (""), the default message for the error code will be used.
func SetServicePrefix ¶
func SetServicePrefix(prefix string)
SetServicePrefix sets the service-specific prefix (e.g., "USER-SVC"). It converts the prefix to uppercase to maintain consistency. If an empty prefix is provided, the default prefix (ERR) is used.
func WrapError ¶
WrapError wraps two errors into one. If either error is nil, it returns the non-nil error. If both are non-nil, it wraps the new error around the original error.
func WrapErrorWithPrefix ¶
WrapErrorWithPrefix wraps the input error with a prefix. If the error is nil, it does nothing.
Types ¶
type AuthenticationError ¶
type AuthenticationError struct {
*BaseError
}
type BadRequestError ¶
type BadRequestError struct {
*BaseError
}
type BaseError ¶
type BaseError struct {
// contains filtered or unexported fields
}
BaseError provides a default implementation of the DomainError interface. It can be embedded in other error types to avoid code duplication.
func ExtractBaseError ¶
ExtractBaseError attempts to extract the BaseError from the error's concrete type. It supports both pointer and non-pointer types, checking if the error directly embeds a *BaseError (one layer deep).
func NewBaseError ¶
NewBaseError creates a new BaseError instance. If the message is empty, it uses the default message from `getDefaultMessages()` based on the error code.
The error code should follow the 'xyyzzz' convention:
- 'x' (first digit): main error category.
- 'yy' (second digit): subcategory.
- 'zzz' (last three digits): specific error detail.
**Note:** The 'xyy' prefix of the code must match a valid category defined in `validCategories`.
func (*BaseError) GetHTTPCode ¶
func (*BaseError) GetMessage ¶
type ClientError ¶
type ClientError struct {
*BaseError
}
type ConflictError ¶
type ConflictError struct {
*BaseError
}
type DatabaseError ¶
type DatabaseError struct {
*BaseError
}
type DomainError ¶
type DomainError interface {
// GetHTTPCode returns the HTTP status code associated with the error.
GetHTTPCode() int
// Code returns the full error code, including the service prefix (e.g., 'SVC-401000').
Code() string
// GetMessage returns the error message.
GetMessage() string
// GetData returns any additional data associated with the error.
GetData() interface{}
// Error implements the standard error interface.
Error() string
}
DomainError is the interface that all custom errors in the framework implement. It provides methods to retrieve the error code, message, HTTP status code and additional data.
Error Code Convention: The error code follows the format 'xyyzzz' ([x][yy][zzz])
- 'x' (first digit) represents the main error category (e.g., '4' for Client Error).
- 'yy' (second digit) represents the subcategory (e.g., '01' for Bad Request).
- 'zzz' (last three digits) provide specific details about the error.
This convention helps in categorizing errors consistently across services.
func UnwrapDomainError ¶
func UnwrapDomainError(err error) DomainError
UnwrapDomainError attempts to find a DomainError in the error chain. The error should implement the DomainError interface and have a BaseError embedded. It unwraps the error chain and checks each error to see if it is a DomainError and if it contains a BaseError. If such an error is found, it is returned.
type ForbiddenError ¶
type ForbiddenError struct {
*BaseError
}
type InternalServerError ¶
type InternalServerError struct {
*BaseError
}
type NotFoundError ¶
type NotFoundError struct {
*BaseError
}
type ThirdPartyError ¶
type ThirdPartyError struct {
*BaseError
}
type UnauthorizedError ¶
type UnauthorizedError struct {
}
type UnprocessableEntityError ¶
type UnprocessableEntityError struct {
*BaseError
}