Compare commits

...

2 Commits

Author SHA1 Message Date
5dbbdc62e6 Add exception error manager
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-01-21 12:04:17 +08:00
200614a5c9 Add error retern for database
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-01-21 10:03:56 +08:00
12 changed files with 158 additions and 13 deletions

View File

@@ -34,7 +34,7 @@ func Init() {
// Conect to db // Conect to db
db, err := drivers.Postgres(exDSN) db, err := drivers.Postgres(exDSN)
if err != nil { if err != nil {
slog.Error("[Database] Error connecting to db!") slog.Error("[Database] Error connecting to db!", "err", err)
os.Exit(1) os.Exit(1)
} }

55
exception/builder.go Normal file
View File

@@ -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,
)
}

6
exception/common.go Normal file
View File

@@ -0,0 +1,6 @@
package exception
const (
CommonErrorInvalidInput = "00001"
CommonErrorUnauthorized = "00002"
)

32
exception/endpoints.go Normal file
View File

@@ -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"
)

16
exception/services.go Normal file
View File

@@ -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"
)

5
exception/specific.go Normal file
View File

@@ -0,0 +1,5 @@
package exception
const (
ApiVersionNotFound = "00001"
)

8
exception/status.go Normal file
View File

@@ -0,0 +1,8 @@
package exception
const (
ErrorStatusSuccess = "2"
ErrorStatusUser = "4"
ErrorStatusServer = "5"
ErrorStatusClient = "6"
)

6
exception/types.go Normal file
View File

@@ -0,0 +1,6 @@
package exception
const (
ErrorTypeCommon = "0"
ErrorTypeSpecific = "1"
)

View File

@@ -1,6 +1,7 @@
package middleware package middleware
import ( import (
"nixcn-cms/exception"
"nixcn-cms/utils" "nixcn-cms/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -10,7 +11,14 @@ func ApiVersionCheck() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
apiVersion := c.GetHeader("X-Api-Version") apiVersion := c.GetHeader("X-Api-Version")
if apiVersion == "" { 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 return
} }
c.Next() c.Next()

View File

@@ -1,7 +1,7 @@
package middleware package middleware
import ( import (
"fmt" "nixcn-cms/exception"
"nixcn-cms/pkgs/authtoken" "nixcn-cms/pkgs/authtoken"
"nixcn-cms/utils" "nixcn-cms/utils"
@@ -16,8 +16,15 @@ func JWTAuth() gin.HandlerFunc {
authtoken := new(authtoken.Token) authtoken := new(authtoken.Token)
uid, err := authtoken.HeaderVerify(auth) uid, err := authtoken.HeaderVerify(auth)
if err != nil { if err != nil {
fmt.Println(err) errorCode := new(exception.Builder).
utils.HttpAbort(c, 401, "", "unauthorized") SetStatus(exception.ErrorStatusServer).
SetService(exception.MiddlewareJwtService).
SetEndpoint(exception.MiddlewareEndpoint).
SetType(exception.ErrorTypeSpecific).
SetOriginal(exception.CommonErrorUnauthorized).
Build()
utils.HttpAbort(c, 401, errorCode)
return return
} }

View File

@@ -11,6 +11,8 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
const ()
func Exchange(c *gin.Context) { func Exchange(c *gin.Context) {
var exchangeReq struct { var exchangeReq struct {
ClientId string `json:"client_id"` ClientId string `json:"client_id"`

View File

@@ -9,16 +9,16 @@ import (
type RespStatus struct { type RespStatus struct {
Code int `json:"code"` Code int `json:"code"`
ErrorId string `json:"error_id"`
Status string `json:"status"` Status string `json:"status"`
ErrorId string `json:"error_id"`
Data any `json:"data"` 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{ resp := RespStatus{
Code: code, Code: code,
ErrorId: id, Status: http.StatusText(code),
Status: status, ErrorId: errId,
} }
switch len(data) { 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) _, _ = c.Writer.Write(jsonBytes)
} }
func HttpResponse(c *gin.Context, code int, id string, status string, data ...any) { func HttpResponse(c *gin.Context, code int, errId string, data ...any) {
render(c, code, id, status, data, false) render(c, code, errId, data, false)
} }
func HttpAbort(c *gin.Context, code int, id string, status string, data ...any) { func HttpAbort(c *gin.Context, code int, errId string, data ...any) {
render(c, code, id, status, data, true) render(c, code, errId, data, true)
} }