-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin.go
54 lines (41 loc) · 1.33 KB
/
gin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package winter
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
func RenderResult(ctx *gin.Context, status int, result any) {
ctx.JSON(status, result)
}
func RenderSuccessResult(ctx *gin.Context, result any) {
RenderResult(ctx, http.StatusOK, result)
}
func RenderErrorResult(ctx *gin.Context, status int, err error) {
if businessError, ok := err.(*BusinessError); ok {
statusCode := status
if statusCode != businessError.Status {
statusCode = businessError.Status
}
RenderResult(ctx, statusCode, businessError)
} else {
RenderResult(ctx, status, NewBusinessError(status, strconv.Itoa(status), err.Error()))
}
}
func RenderBadRequestResult(ctx *gin.Context, err error) {
RenderErrorResult(ctx, http.StatusBadRequest, err)
}
func RenderNotFoundResult(ctx *gin.Context, err error) {
RenderErrorResult(ctx, http.StatusNotFound, err)
}
func RenderUnauthorizedResult(ctx *gin.Context, err error) {
RenderErrorResult(ctx, http.StatusUnauthorized, err)
}
func RenderForbiddenResult(ctx *gin.Context, err error) {
RenderErrorResult(ctx, http.StatusForbidden, err)
}
func RenderInternalServerErrorResult(ctx *gin.Context, err error) {
RenderErrorResult(ctx, http.StatusInternalServerError, err)
}
func RenderServiceUnavailableResult(ctx *gin.Context, err error) {
RenderErrorResult(ctx, http.StatusServiceUnavailable, err)
}