Skip to content

Commit 3e6f55b

Browse files
committed
fix: AbortWithJsonError获取Code错误BUG
1 parent 0d793f8 commit 3e6f55b

File tree

2 files changed

+70
-24
lines changed

2 files changed

+70
-24
lines changed

server/ginx/error.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ginx
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
)
7+
8+
var internalServerError = NewError(http.StatusInternalServerError, 500, "Internal Server Error")
9+
10+
type statusError interface {
11+
error
12+
Status() int
13+
}
14+
15+
type codeError interface {
16+
error
17+
Code() int
18+
}
19+
20+
type Error struct {
21+
status int
22+
code int
23+
message string
24+
}
25+
26+
func NewError(status, code int, message string) error {
27+
return &Error{
28+
status: status,
29+
code: code,
30+
message: message,
31+
}
32+
}
33+
34+
func (e *Error) Error() string {
35+
return e.message
36+
}
37+
38+
func (e *Error) Status() int {
39+
return e.status
40+
}
41+
42+
func (e *Error) Code() int {
43+
return e.code
44+
}
45+
46+
func parseError(err error) (code int, status int, message string) {
47+
var se statusError
48+
if errors.As(err, &se) {
49+
status = se.Status()
50+
} else {
51+
status = http.StatusInternalServerError
52+
}
53+
var ce codeError
54+
if errors.As(err, &ce) {
55+
code = ce.Code()
56+
} else {
57+
code = 500
58+
}
59+
message = err.Error()
60+
return
61+
}

server/ginx/with.go

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
package ginx
22

33
import (
4-
"errors"
54
"github.com/TBXark/sphere/log"
65
"github.com/TBXark/sphere/log/logfields"
76
"github.com/gin-gonic/gin"
87
"net/http"
98
)
109

11-
var internalServerError = errors.New("internal server error")
12-
13-
type statusError interface {
14-
error
15-
Status() int
16-
}
17-
1810
type Context = gin.Context
1911

2012
func Value[T any](key string, ctx *gin.Context) (*T, bool) {
@@ -28,19 +20,12 @@ func Value[T any](key string, ctx *gin.Context) (*T, bool) {
2820
return nil, false
2921
}
3022

31-
func AbortWithJsonError(ctx *gin.Context, code int, err error) {
32-
var hErr statusError
33-
if errors.As(err, &hErr) {
34-
ctx.AbortWithStatusJSON(hErr.Status(), ErrorResponse{
35-
Code: hErr.Status(),
36-
Message: hErr.Error(),
37-
})
38-
} else {
39-
ctx.AbortWithStatusJSON(code, ErrorResponse{
40-
Code: hErr.Status(),
41-
Message: err.Error(),
42-
})
43-
}
23+
func AbortWithJsonError(ctx *gin.Context, err error) {
24+
code, status, message := parseError(err)
25+
ctx.AbortWithStatusJSON(status, ErrorResponse{
26+
Code: code,
27+
Message: message,
28+
})
4429
}
4530

4631
func WithRecover(message string, handler func(ctx *gin.Context)) func(ctx *gin.Context) {
@@ -51,7 +36,7 @@ func WithRecover(message string, handler func(ctx *gin.Context)) func(ctx *gin.C
5136
message,
5237
logfields.Any("error", err),
5338
)
54-
AbortWithJsonError(ctx, http.StatusInternalServerError, internalServerError)
39+
AbortWithJsonError(ctx, internalServerError)
5540
}
5641
}()
5742
handler(ctx)
@@ -62,7 +47,7 @@ func WithJson[T any](handler func(ctx *gin.Context) (T, error)) func(ctx *gin.Co
6247
return WithRecover("WithJson panic", func(ctx *gin.Context) {
6348
data, err := handler(ctx)
6449
if err != nil {
65-
AbortWithJsonError(ctx, http.StatusBadRequest, err)
50+
AbortWithJsonError(ctx, err)
6651
} else {
6752
ctx.JSON(200, DataResponse[T]{
6853
Success: true,
@@ -76,7 +61,7 @@ func WithText(handler func(ctx *gin.Context) (string, error)) func(ctx *gin.Cont
7661
return WithRecover("WithText panic", func(ctx *gin.Context) {
7762
data, err := handler(ctx)
7863
if err != nil {
79-
AbortWithJsonError(ctx, http.StatusBadRequest, err)
64+
AbortWithJsonError(ctx, err)
8065
} else {
8166
ctx.String(200, data)
8267
}

0 commit comments

Comments
 (0)