补充业务异常处理机制

This commit is contained in:
yumaojun03 2025-05-11 17:34:48 +08:00
parent fad998ea15
commit 43f675d736
2 changed files with 54 additions and 17 deletions

View File

@ -1,12 +1,12 @@
package handlers
import (
"net/http"
"strconv"
"122.51.31.227/go-course/go18/book/v3/config"
"122.51.31.227/go-course/go18/book/v3/controllers"
"122.51.31.227/go-course/go18/book/v3/models"
"122.51.31.227/go-course/go18/book/v3/response"
"github.com/gin-gonic/gin"
)
@ -45,7 +45,7 @@ func (h *BookApiHandler) listBook(ctx *gin.Context) {
if pageNumber != "" {
pnInt, err := strconv.ParseInt(pageNumber, 10, 64)
if err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
pn = int(pnInt)
@ -55,7 +55,7 @@ func (h *BookApiHandler) listBook(ctx *gin.Context) {
if pageSize != "" {
psInt, err := strconv.ParseInt(pageSize, 10, 64)
if err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
ps = int(psInt)
@ -79,12 +79,12 @@ func (h *BookApiHandler) listBook(ctx *gin.Context) {
// 4 offset 3 * 20, 20
offset := (pn - 1) * ps
if err := query.Count(&set.Total).Offset(int(offset)).Limit(int(ps)).Find(&set.Items).Error; err != nil {
ctx.JSON(500, gin.H{"code": 500, "message": err.Error()})
response.Failed(ctx, err)
return
}
// 获取总数, 总共多少个, 总共有多少页
ctx.JSON(200, set)
response.OK(ctx, set)
}
func (h *BookApiHandler) createBook(ctx *gin.Context) {
@ -110,41 +110,41 @@ func (h *BookApiHandler) createBook(ctx *gin.Context) {
// 获取到bookInstance
// 参数是不是为空
if err := ctx.BindJSON(bookSpecInstance); err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
book, err := controllers.Book.CreateBook(ctx.Request.Context(), bookSpecInstance)
if err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
// 返回响应
ctx.JSON(http.StatusCreated, book)
response.OK(ctx, book)
}
func (h *BookApiHandler) getBook(ctx *gin.Context) {
bnInt, err := strconv.ParseInt(ctx.Param("bn"), 10, 64)
if err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
book, err := controllers.Book.GetBook(ctx, controllers.NewGetBookRequest(int(bnInt)))
if err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
ctx.JSON(200, book)
response.OK(ctx, book)
}
func (h *BookApiHandler) updateBook(ctx *gin.Context) {
bnStr := ctx.Param("bn")
bn, err := strconv.ParseInt(bnStr, 10, 64)
if err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
@ -154,22 +154,23 @@ func (h *BookApiHandler) updateBook(ctx *gin.Context) {
}
// 获取到bookInstance
if err := ctx.BindJSON(&bookInstance.BookSpec); err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
if err := config.DB().Where("id = ?", bookInstance.Id).Updates(bookInstance).Error; err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
ctx.JSON(200, bookInstance)
response.OK(ctx, bookInstance)
}
func (h *BookApiHandler) deleteBook(ctx *gin.Context) {
if err := config.DB().Where("id = ?", ctx.Param("bn")).Delete(&models.Book{}).Error; err != nil {
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
response.Failed(ctx, err)
return
}
ctx.JSON(http.StatusNoContent, "ok")
response.OK(ctx, "ok")
}

View File

@ -0,0 +1,36 @@
package response
import (
"122.51.31.227/go-course/go18/book/v3/exception"
"github.com/gin-gonic/gin"
)
// 当前请求成功的时候,我们应用返回的数据
// 1. {code: 0, data: {}}
// 2. 正常直接返回数据, Restful接口 怎么知道这些请求是成功还是失败喃? 通过HTTP判断 2xx
// 如果后面 所有的返回数据 要进过特殊处理,都在这个函数内进行扩展,方便维护,比如 数据脱敏
func OK(ctx *gin.Context, data any) {
ctx.JSON(200, data)
ctx.Abort()
}
// 当前请求失败的时候, 我们返回的数据格式
// 1. {code: xxxx, data: null, message: "错误信息"}
// 请求HTTP Code 非 2xx 就返回我们自定义的异常
//
// {
// "code": 404,
// "message": "book 1 not found"
// }
func Failed(ctx *gin.Context, err error) {
// 一种是我们自己的业务异常
if e, ok := err.(*exception.ApiException); ok {
ctx.JSON(e.HttpCode, e)
ctx.Abort()
return
}
// 非业务异常
ctx.JSON(500, exception.NewApiException(500, err.Error()))
ctx.Abort()
}