All checks were successful
Go CI/CD / go-ci (push) Successful in 1m47s
Introduce 'enforcer' package with `Sentinel` for rich error context and `ApplicationError` for custom error codes. Include tests, documentation updates, CI workflows, and foundational files like `GO.mod` for project setup.
35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
package enforcer
|
|
|
|
// ApplicationError represents a custom error type as an integer value.
|
|
type ApplicationError int
|
|
|
|
// GetHTTPStatus extracts the HTTP status code from an ApplicationError.
|
|
func (a ApplicationError) GetHTTPStatus() int {
|
|
return int(a) / 1_000_000
|
|
}
|
|
|
|
// GetCode returns the integer code associated with the ApplicationError.
|
|
func (a ApplicationError) GetCode() int {
|
|
return int(a)
|
|
}
|
|
|
|
// GetMessage returns the message associated with the ApplicationError.
|
|
func (a ApplicationError) GetMessage() string {
|
|
return applicationCodeMap[a]
|
|
}
|
|
|
|
// applicationCodeMap maps ApplicationError values to their corresponding messages.
|
|
var applicationCodeMap = make(map[ApplicationError]string)
|
|
|
|
// RegisterError maps an ApplicationError code to its corresponding message.
|
|
func RegisterError(code ApplicationError, message string) {
|
|
applicationCodeMap[code] = message
|
|
}
|
|
|
|
// RegisterErrorMap registers a mapping of ApplicationErrors to their messages.
|
|
func RegisterErrorMap(errors map[ApplicationError]string) {
|
|
for code, message := range errors {
|
|
applicationCodeMap[code] = message
|
|
}
|
|
}
|