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:
2026-01-21 12:47:49 +08:00
parent 5dbbdc62e6
commit e4329dfc2b
14 changed files with 611 additions and 69 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"nixcn-cms/data"
"nixcn-cms/exception"
"nixcn-cms/pkgs/authcode"
"nixcn-cms/utils"
@@ -23,38 +24,80 @@ func Exchange(c *gin.Context) {
err := c.ShouldBindJSON(&exchangeReq)
if err != nil {
fmt.Println(err)
utils.HttpResponse(c, 400, "", "invalid request")
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.AuthService).
SetEndpoint(exception.AuthExchangeEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
userIdOrig, ok := c.Get("user_id")
if !ok {
utils.HttpResponse(c, 401, "", "unauthorized")
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.AuthService).
SetEndpoint(exception.AuthExchangeEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorUnauthorized).
Build()
utils.HttpResponse(c, 401, errorCode)
return
}
userId, err := uuid.Parse(userIdOrig.(string))
if err != nil {
utils.HttpResponse(c, 500, "", "failed to parse uuid")
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusServer).
SetService(exception.AuthService).
SetEndpoint(exception.AuthExchangeEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
Build()
utils.HttpResponse(c, 500, errorCode)
return
}
userData := new(data.User)
user, err := userData.GetByUserId(userId)
if err != nil {
utils.HttpResponse(c, 500, "", "failed to get user id")
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusServer).
SetService(exception.AuthService).
SetEndpoint(exception.AuthExchangeEndpoint).
SetType(exception.ErrorTypeSpecific).
SetOriginal(exception.AuthExchangeGetUserIdFailed).
Build()
utils.HttpResponse(c, 500, errorCode)
return
}
code, err := authcode.NewAuthCode(exchangeReq.ClientId, user.Email)
if err != nil {
utils.HttpResponse(c, 500, "", "code gen failed")
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusServer).
SetService(exception.AuthService).
SetEndpoint(exception.AuthExchangeEndpoint).
SetType(exception.ErrorTypeSpecific).
SetOriginal(exception.AuthExchangeCodeGenFailed).
Build()
utils.HttpResponse(c, 500, errorCode)
return
}
url, err := url.Parse(exchangeReq.RedirectUri)
if err != nil {
utils.HttpResponse(c, 400, "", "invalid redirect uri")
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.AuthService).
SetEndpoint(exception.AuthExchangeEndpoint).
SetType(exception.ErrorTypeSpecific).
SetOriginal(exception.AuthExchangeInvalidRedirectUri).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
query := url.Query()