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 event
|
||||
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/exception"
|
||||
"nixcn-cms/utils"
|
||||
"time"
|
||||
|
||||
@@ -13,31 +14,66 @@ func Checkin(c *gin.Context) {
|
||||
data := new(data.Attendance)
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
utils.HttpResponse(c, 403, "", "userid error")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusUser).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinEndpoint).
|
||||
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.EventService).
|
||||
SetEndpoint(exception.EventCheckinEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
}
|
||||
|
||||
// Get event id from query
|
||||
eventIdOrig, ok := c.GetQuery("event_id")
|
||||
if !ok {
|
||||
utils.HttpResponse(c, 400, "", "undefinded event id")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse event id to uuid
|
||||
eventId, err := uuid.Parse(eventIdOrig)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 500, "", "error parsing string to uuid")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusServer).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
data.UserId = userId
|
||||
code, err := data.GenCheckinCode(eventId)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 500, "", "error generating code")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusServer).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinEndpoint).
|
||||
SetType(exception.ErrorTypeSpecific).
|
||||
SetOriginal(exception.EventCheckinGenCodeFailed).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -56,7 +92,14 @@ func CheckinSubmit(c *gin.Context) {
|
||||
attendanceData := new(data.Attendance)
|
||||
err := attendanceData.VerifyCheckinCode(req.ChekinCode)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 400, "", "error verify checkin code")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinSubmitEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -66,23 +109,51 @@ func CheckinSubmit(c *gin.Context) {
|
||||
func CheckinQuery(c *gin.Context) {
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
utils.HttpResponse(c, 400, "", "userid error")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusUser).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorMissingUserId).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, 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.EventService).
|
||||
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
eventIdOrig, ok := c.GetQuery("event_id")
|
||||
if !ok {
|
||||
utils.HttpResponse(c, 400, "", "could not found event_id")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
eventId, err := uuid.Parse(eventIdOrig)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 400, "", "event_id is not valid")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -90,10 +161,24 @@ func CheckinQuery(c *gin.Context) {
|
||||
attendance, err := attendanceData.GetAttendance(userId, eventId)
|
||||
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 500, "", "database error")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusServer).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorDatabase).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
} else if attendance == nil {
|
||||
utils.HttpResponse(c, 404, "", "event checkin record not found")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||
SetType(exception.ErrorTypeSpecific).
|
||||
SetOriginal(exception.EventCheckinQueryRecordNotFound).
|
||||
Build()
|
||||
utils.HttpResponse(c, 404, errorCode)
|
||||
return
|
||||
} else if attendance.CheckinAt.IsZero() {
|
||||
utils.HttpResponse(c, 200, "", "success", gin.H{"checkin_at": nil})
|
||||
|
||||
@@ -2,6 +2,7 @@ package event
|
||||
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/exception"
|
||||
"nixcn-cms/utils"
|
||||
"time"
|
||||
|
||||
@@ -13,20 +14,41 @@ func Info(c *gin.Context) {
|
||||
eventData := new(data.Event)
|
||||
eventIdOrig, ok := c.GetQuery("event_id")
|
||||
if !ok {
|
||||
utils.HttpResponse(c, 400, "", "undefinded event id")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventInfoEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse event id
|
||||
eventId, err := uuid.Parse(eventIdOrig)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 500, "", "error parsing string to uuid")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusServer).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventInfoEndpoint).
|
||||
SetType(exception.ErrorTypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
Build()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
event, err := eventData.GetEventById(eventId)
|
||||
if err != nil {
|
||||
utils.HttpResponse(c, 404, "", "event id not found")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusClient).
|
||||
SetService(exception.EventService).
|
||||
SetEndpoint(exception.EventInfoEndpoint).
|
||||
SetType(exception.ErrorTypeSpecific).
|
||||
SetOriginal(exception.EventInfoNotFound).
|
||||
Build()
|
||||
utils.HttpResponse(c, 404, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user