noxarion-x/enforcer/application_error.go
Rene Nochebuena 06c778db51
All checks were successful
Go CI/CD / go-ci (push) Successful in 1m47s
Add initial enforcer module with structured error handling and CI
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.
2025-04-27 00:07:04 -06:00

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
}
}