diff --git a/exception/builder.go b/exception/builder.go new file mode 100644 index 0000000..a434344 --- /dev/null +++ b/exception/builder.go @@ -0,0 +1,55 @@ +package exception + +import ( + "fmt" +) + +// 12 chars len +// :1=status +// :3=service +// :2=endpoint +// :1=common/specific +// :5=original + +type Builder struct { + Status string + Service string + Endpoint string + Type string + Original string +} + +func (self *Builder) SetStatus(s string) *Builder { + self.Status = s + return self +} + +func (self *Builder) SetService(s string) *Builder { + self.Service = s + return self +} + +func (self *Builder) SetEndpoint(s string) *Builder { + self.Endpoint = s + return self +} + +func (self *Builder) SetType(s string) *Builder { + self.Type = s + return self +} + +func (self *Builder) SetOriginal(s string) *Builder { + self.Original = s + return self +} + +func (self *Builder) Build() string { + return fmt.Sprintf("%s%s%s%s%s", + self.Status, + self.Service, + self.Endpoint, + self.Type, + self.Original, + ) +} diff --git a/exception/common.go b/exception/common.go new file mode 100644 index 0000000..7c318ae --- /dev/null +++ b/exception/common.go @@ -0,0 +1,6 @@ +package exception + +const ( + CommonErrorInvalidInput = "00001" + CommonErrorUnauthorized = "00002" +) diff --git a/exception/endpoints.go b/exception/endpoints.go new file mode 100644 index 0000000..2e98d45 --- /dev/null +++ b/exception/endpoints.go @@ -0,0 +1,32 @@ +package exception + +// Middleware Service Endpoints +const ( + MiddlewareEndpoint = "01" +) + +// Auth Service Endpoints +const ( + AuthRedirectEndpoint = "01" + AuthMagicEndpoint = "02" + AuthTokenEndpoint = "03" + AuthRefreshEndpoint = "04" + AuthExchangeEndpoint = "05" +) + +// Event Service Endpoints +const ( + EventInfoEndpoint = "01" + EventCheckinEndpoint = "02" + EventCheckinQueryEndpoint = "03" + EventCheckinSubmitEndpoint = "04" +) + +// User Service Endpoints +const ( + UserInfoEndpoint = "01" + UserUpdateEndpoint = "02" + UserListEndpoint = "03" + UserFullEndpoint = "04" + UserCreateEndpoint = "05" +) diff --git a/exception/services.go b/exception/services.go new file mode 100644 index 0000000..3e2137e --- /dev/null +++ b/exception/services.go @@ -0,0 +1,16 @@ +package exception + +// Middleware Services +const ( + MiddlewareGinLoggerService = "901" + MiddlewareJwtService = "902" + MiddlewarePermissionService = "903" + MiddlewareApiVersionService = "904" +) + +// Application Services +const ( + AuthService = "001" + UserService = "002" + EventService = "003" +) diff --git a/exception/specific.go b/exception/specific.go new file mode 100644 index 0000000..58e743b --- /dev/null +++ b/exception/specific.go @@ -0,0 +1,5 @@ +package exception + +const ( + ApiVersionNotFound = "00001" +) diff --git a/exception/status.go b/exception/status.go new file mode 100644 index 0000000..79776a0 --- /dev/null +++ b/exception/status.go @@ -0,0 +1,8 @@ +package exception + +const ( + ErrorStatusSuccess = "2" + ErrorStatusUser = "4" + ErrorStatusServer = "5" + ErrorStatusClient = "6" +) diff --git a/exception/types.go b/exception/types.go new file mode 100644 index 0000000..fa8a80f --- /dev/null +++ b/exception/types.go @@ -0,0 +1,6 @@ +package exception + +const ( + ErrorTypeCommon = "0" + ErrorTypeSpecific = "1" +) diff --git a/middleware/api_version.go b/middleware/api_version.go index a08f8ef..f9b7421 100644 --- a/middleware/api_version.go +++ b/middleware/api_version.go @@ -1,6 +1,7 @@ package middleware import ( + "nixcn-cms/exception" "nixcn-cms/utils" "github.com/gin-gonic/gin" @@ -10,7 +11,14 @@ func ApiVersionCheck() gin.HandlerFunc { return func(c *gin.Context) { apiVersion := c.GetHeader("X-Api-Version") if apiVersion == "" { - utils.HttpAbort(c, 400, "", "api version not found") + errorCode := new(exception.Builder). + SetStatus(exception.ErrorStatusServer). + SetService(exception.MiddlewareApiVersionService). + SetEndpoint(exception.MiddlewareEndpoint). + SetType(exception.ErrorTypeSpecific). + SetOriginal(exception.ApiVersionNotFound). + Build() + utils.HttpAbort(c, 400, errorCode) return } c.Next() diff --git a/middleware/jwt.go b/middleware/jwt.go index f97249b..8d1d3b3 100644 --- a/middleware/jwt.go +++ b/middleware/jwt.go @@ -1,7 +1,7 @@ package middleware import ( - "fmt" + "nixcn-cms/exception" "nixcn-cms/pkgs/authtoken" "nixcn-cms/utils" @@ -16,8 +16,15 @@ func JWTAuth() gin.HandlerFunc { authtoken := new(authtoken.Token) uid, err := authtoken.HeaderVerify(auth) if err != nil { - fmt.Println(err) - utils.HttpAbort(c, 401, "", "unauthorized") + errorCode := new(exception.Builder). + SetStatus(exception.ErrorStatusServer). + SetService(exception.MiddlewareJwtService). + SetEndpoint(exception.MiddlewareEndpoint). + SetType(exception.ErrorTypeSpecific). + SetOriginal(exception.CommonErrorUnauthorized). + Build() + + utils.HttpAbort(c, 401, errorCode) return } diff --git a/service/auth/exchange.go b/service/auth/exchange.go index 7742c66..fa5c62c 100644 --- a/service/auth/exchange.go +++ b/service/auth/exchange.go @@ -11,6 +11,8 @@ import ( "github.com/google/uuid" ) +const () + func Exchange(c *gin.Context) { var exchangeReq struct { ClientId string `json:"client_id"` diff --git a/utils/response.go b/utils/response.go index 90ceca0..381b777 100644 --- a/utils/response.go +++ b/utils/response.go @@ -9,16 +9,16 @@ import ( type RespStatus struct { Code int `json:"code"` - ErrorId string `json:"error_id"` Status string `json:"status"` + ErrorId string `json:"error_id"` Data any `json:"data"` } -func render(c *gin.Context, code int, id string, status string, data []any, abort bool) { +func render(c *gin.Context, code int, errId string, data []any, abort bool) { resp := RespStatus{ Code: code, - ErrorId: id, - Status: status, + Status: http.StatusText(code), + ErrorId: errId, } switch len(data) { @@ -45,10 +45,10 @@ func render(c *gin.Context, code int, id string, status string, data []any, abor _, _ = c.Writer.Write(jsonBytes) } -func HttpResponse(c *gin.Context, code int, id string, status string, data ...any) { - render(c, code, id, status, data, false) +func HttpResponse(c *gin.Context, code int, errId string, data ...any) { + render(c, code, errId, data, false) } -func HttpAbort(c *gin.Context, code int, id string, status string, data ...any) { - render(c, code, id, status, data, true) +func HttpAbort(c *gin.Context, code int, errId string, data ...any) { + render(c, code, errId, data, true) }