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

@@ -2,6 +2,7 @@ package user
import (
"nixcn-cms/data"
"nixcn-cms/exception"
"nixcn-cms/utils"
"github.com/gin-gonic/gin"
@@ -12,19 +13,40 @@ func Info(c *gin.Context) {
userData := new(data.User)
userIdOrig, ok := c.Get("user_id")
if !ok {
utils.HttpResponse(c, 403, "", "userid error")
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusUser).
SetService(exception.UserService).
SetEndpoint(exception.UserInfoEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorMissingUserId).
Build()
utils.HttpResponse(c, 403, 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.UserService).
SetEndpoint(exception.UserInfoEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
Build()
utils.HttpResponse(c, 500, errorCode)
return
}
// Get user from database
user, err := userData.GetByUserId(userId)
if err != nil {
utils.HttpResponse(c, 404, "", "user not found")
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusUser).
SetService(exception.UserService).
SetEndpoint(exception.UserInfoEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorUserNotFound).
Build()
utils.HttpResponse(c, 404, errorCode)
return
}