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:
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/exception"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -11,24 +12,52 @@ import (
|
||||
func Full(c *gin.Context) {
|
||||
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.UserFullEndpoint).
|
||||
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.UserFullEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
userData, err := new(data.User).GetByUserId(userId)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 404, "", "user not found")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusUser).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserFullEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorUserNotFound).
|
||||
Build()
|
||||
utils.HttpResponse(c, 404, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
users, err := userData.GetFullTable()
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 500, "", "database error")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusServer).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserFullEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorDatabase).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/exception"
|
||||
"nixcn-cms/utils"
|
||||
"strconv"
|
||||
|
||||
@@ -16,26 +17,54 @@ func List(c *gin.Context) {
|
||||
}
|
||||
offset, ok := c.GetQuery("offset")
|
||||
if !ok {
|
||||
utils.HttpResponse(c, 400, "", "offset not found")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserListEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse string to int64
|
||||
limitNum, err := strconv.ParseInt(limit, 10, 64)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 400, "", "parse string to int error")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserListEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
offsetNum, err := strconv.ParseInt(offset, 10, 64)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 400, "", "parse string to int error")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserListEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Get user list from search engine
|
||||
list, err := new(data.User).FastListUsers(limitNum, offsetNum)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 500, "", "failed list users from meilisearch")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusServer).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserListEndpoint).
|
||||
SetType(exception.ErrorTypeSpecific).
|
||||
SetOriginal(exception.UserListMeilisearchFailed).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
}
|
||||
|
||||
userListResp := struct {
|
||||
|
||||
@@ -3,6 +3,7 @@ package user
|
||||
import (
|
||||
"net/url"
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/exception"
|
||||
"nixcn-cms/internal/cryptography"
|
||||
"nixcn-cms/utils"
|
||||
"unicode/utf8"
|
||||
@@ -15,26 +16,54 @@ func Update(c *gin.Context) {
|
||||
// New user model
|
||||
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.UserUpdateEndpoint).
|
||||
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.UserUpdateEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
var ReqInfo data.User
|
||||
err = c.ShouldBindJSON(&ReqInfo)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 400, "", "invilad request")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserUpdateEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Get user info
|
||||
userData, err := new(data.User).GetByUserId(userId)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 500, "", "failed to find user")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusUser).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserUpdateEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorUserNotFound).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -49,7 +78,14 @@ func Update(c *gin.Context) {
|
||||
|
||||
if ReqInfo.Username != "" {
|
||||
if len(ReqInfo.Username) < 5 || len(ReqInfo.Username) >= 255 {
|
||||
utils.HttpResponse(c, 400, "", "invalid request")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserUpdateEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
userData.Username = ReqInfo.Username
|
||||
@@ -57,7 +93,14 @@ func Update(c *gin.Context) {
|
||||
|
||||
if ReqInfo.Nickname != "" {
|
||||
if utf8.RuneCountInString(ReqInfo.Nickname) > 24 {
|
||||
utils.HttpResponse(c, 400, "", "invalid request")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserUpdateEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
userData.Nickname = ReqInfo.Nickname
|
||||
@@ -65,7 +108,14 @@ func Update(c *gin.Context) {
|
||||
|
||||
if ReqInfo.Subtitle != "" {
|
||||
if utf8.RuneCountInString(ReqInfo.Subtitle) > 32 {
|
||||
utils.HttpResponse(c, 400, "", "invalid request")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserUpdateEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
userData.Subtitle = ReqInfo.Subtitle
|
||||
@@ -74,7 +124,14 @@ func Update(c *gin.Context) {
|
||||
if ReqInfo.Avatar != "" {
|
||||
_, err := url.ParseRequestURI(ReqInfo.Avatar)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 400, "", "invalid request")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserUpdateEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
userData.Avatar = ReqInfo.Avatar
|
||||
@@ -82,7 +139,14 @@ func Update(c *gin.Context) {
|
||||
|
||||
if ReqInfo.Bio != "" {
|
||||
if !cryptography.IsBase64Std(ReqInfo.Bio) {
|
||||
utils.HttpResponse(c, 400, "", "invalid request")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.UserService).
|
||||
SetEndpoint(exception.UserUpdateEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
userData.Bio = ReqInfo.Bio
|
||||
|
||||
Reference in New Issue
Block a user