refactor: standardize error handling with exception.Builder
- Replace hardcoded error messages with structured error codes using exception.Builder. - Introduce new common error constants in exception/common.go (CommonErrorInvalidInput, CommonErrorUserNotFound, etc.). - Update exception/specific.go with domain-specific errors and remove redundant ones. - Apply consistent error handling across auth, event, user services and middleware. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Noa Virellia <noa@requiem.garden>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"nixcn-cms/exception"
|
||||
"nixcn-cms/pkgs/authtoken"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
@@ -14,7 +15,14 @@ func Refresh(c *gin.Context) {
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.HttpResponse(c, 400, "", "invalid request")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.AuthService).
|
||||
SetEndpoint(exception.AuthRefreshEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -24,13 +32,27 @@ func Refresh(c *gin.Context) {
|
||||
|
||||
accessToken, err := JwtTool.RefreshAccessToken(req.RefreshToken)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 401, "", "invalid refresh token")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.AuthService).
|
||||
SetEndpoint(exception.AuthRefreshEndpoint).
|
||||
SetType(exception.ErrorTypeSpecific).
|
||||
SetOriginal(exception.AuthRefreshInvalidToken).
|
||||
Build()
|
||||
utils.HttpResponse(c, 401, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
refreshToken, err := JwtTool.RenewRefreshToken(req.RefreshToken)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 500, "", "error renew refresh token")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusServer).
|
||||
SetService(exception.AuthService).
|
||||
SetEndpoint(exception.AuthRefreshEndpoint).
|
||||
SetType(exception.ErrorTypeSpecific).
|
||||
SetOriginal(exception.AuthRefreshRenewFailed).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user