5 Star 14 Fork 12

go-course / go12

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
exception.go 721 Bytes
一键复制 编辑 原始数据 按行查看 历史
Mr.Yu 提交于 2023-10-28 12:11 . 对接登陆页面和状态管理
package exception
import (
"fmt"
"net/http"
)
// New(5000, "令牌过期...")
func New(code int, format string, a ...any) *ApiException {
HttpCode := http.StatusInternalServerError
// 0 ~ 5xx
if code/100 < 6 && code/100 > 0 {
HttpCode = code
}
return &ApiException{
BizCode: code,
Message: fmt.Sprintf(format, a...),
// 如果是异常,直接使用http code作为异常, 不做使用200
HttpCode: HttpCode,
}
}
// 业务自定义异常
type ApiException struct {
// 业务异常
BizCode int `json:"code"`
Message string `json:"message"`
Data any `json:"data"`
HttpCode int `json:"http_code"`
}
// 实现Error接口
func (e *ApiException) Error() string {
return e.Message
}
Go
1
https://gitee.com/go-course/go12.git
git@gitee.com:go-course/go12.git
go-course
go12
go12
8a8b3aa3c2e4

搜索帮助