feat: impl requests handllers for contacts and healthcheck
This commit is contained in:
88
internal/handler/errors.go
Normal file
88
internal/handler/errors.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
/*
|
||||
Domain (First Digit)
|
||||
|
||||
1xxx - Request/Input Errors
|
||||
|
||||
2xxx - Authentication/Authorization
|
||||
|
||||
3xxx - Business Logic/Validation
|
||||
|
||||
4xxx - Data/Resource Errors
|
||||
|
||||
5xxx - System/External Service Errors
|
||||
|
||||
Category (Second Digit)
|
||||
|
||||
x0xx - General/Generic errors in that domain
|
||||
|
||||
x1xx - Format/Structure errors
|
||||
|
||||
x2xx - Validation/Constraint errors
|
||||
|
||||
x3xx - State/Workflow errors
|
||||
|
||||
x4xx - Permission/Access errors
|
||||
|
||||
Specific Error (Last Two Digits)
|
||||
|
||||
xx00-xx99 - Specific error instances
|
||||
*/
|
||||
|
||||
// Domains
|
||||
const (
|
||||
DomainRequest = 1000
|
||||
DomainAuth = 2000
|
||||
DomainData = 4000
|
||||
DomainSystem = 5000
|
||||
)
|
||||
|
||||
const (
|
||||
CategoryGeneral = 0
|
||||
CategoryFormat = 100
|
||||
CategoryValidation = 200
|
||||
CategoryState = 300
|
||||
CategoryPermission = 400
|
||||
)
|
||||
|
||||
// Err code Domain + Category + Specific
|
||||
const (
|
||||
CodeInvalidJSON = DomainRequest + CategoryFormat + 1
|
||||
InvalidParamFormat = DomainRequest + CategoryFormat + 0
|
||||
CodeValidationFailed = DomainRequest + CategoryValidation + 0
|
||||
CodeMissingRequired = DomainRequest + CategoryValidation + 1
|
||||
CodeNotFound = DomainData + CategoryPermission + 0
|
||||
CodeInternalError = DomainSystem + CategoryGeneral + 0
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidJSON = APIError{Code: CodeInvalidJSON, Message: "Invalid JSON in request body"}
|
||||
ErrValidation = APIError{Code: CodeValidationFailed, Message: "Validation failed"}
|
||||
ErrMissingRequired = APIError{Code: CodeMissingRequired, Message: "Required field is missing"}
|
||||
ErrNotFound = APIError{Code: CodeNotFound, Message: "Resource not found"}
|
||||
ErrInternalServer = APIError{Code: CodeInternalError, Message: "Internal server error"}
|
||||
ErrInvalidFormat = APIError{Code: InvalidParamFormat, Message: "The given param doesn't match the format expectations"}
|
||||
)
|
||||
|
||||
// Helper functions for common errors
|
||||
func BadRequest(w http.ResponseWriter, err APIError, details string) {
|
||||
JSONError(w, err.Message, details, err.Code, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func NotFound(w http.ResponseWriter, err APIError, details string) {
|
||||
JSONError(w, err.Message, details, err.Code, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func InternalError(w http.ResponseWriter, err error) {
|
||||
JSONError(w, ErrInternalServer.Message, err.Error(), ErrInternalServer.Code, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func RequiredFieldErr(field string) string {
|
||||
return (fmt.Sprintf("%s %s", field, required_field))
|
||||
}
|
||||
Reference in New Issue
Block a user