go17/book/response/response.go

25 lines
538 B
Go
Raw Permalink Normal View History

2024-11-23 10:55:52 +08:00
package response
import (
"net/http"
"github.com/gin-gonic/gin"
2024-11-23 17:32:33 +08:00
"gitlab.com/go-course-project/go17/book/exception"
2024-11-23 10:55:52 +08:00
)
func Failed(ctx *gin.Context, err error) {
2024-11-23 17:32:33 +08:00
// 断言, 是我们定义的标准异常,之间使用异常对象本事
if v, ok := err.(*exception.ApiExceptin); ok {
if v.HttpCode == 0 {
v.HttpCode = 500
}
ctx.JSON(v.HttpCode, v)
} else {
ctx.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": err.Error()})
}
2024-11-23 10:55:52 +08:00
}
func Success(ctx *gin.Context, data any) {
ctx.JSON(http.StatusOK, data)
}