3 Star 0 Fork 0

neuro-netw0rk/server-servicegolib

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
code.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
wangxianyu 提交于 2023-07-11 11:49 +08:00 . fix
package code
import (
"net/http"
"gitee.com/neuro-netw0rk/server-golib/errors"
"github.com/novalagung/gubrak"
)
type ErrCode struct {
// C refers to the code of the ErrCode.
C int
// HTTP status that should be used for the associated error code.
HTTP int
// External (user) facing error text.
Ext string
// Ref specify the reference document.
Ref string
}
var _ errors.Coder = &ErrCode{}
// Code returns the integer code of ErrCode.
func (coder ErrCode) Code() int {
return coder.C
}
// String implements stringer. String returns the external error message,
// if any.
func (coder ErrCode) String() string {
return coder.Ext
}
// Reference returns the reference document.
func (coder ErrCode) Reference() string {
return coder.Ref
}
// HTTPStatus returns the associated HTTP status code, if any. Otherwise,
// returns 200.
func (coder ErrCode) HTTPStatus() int {
if coder.HTTP == 0 {
return http.StatusInternalServerError
}
return coder.HTTP
}
// nolint: unparam
func register(code int, httpStatus int, message string, refs ...string) {
found, _ := gubrak.Includes([]int{200, 400, 401, 403, 404, 500}, httpStatus)
if !found {
panic("http code not in `200, 400, 401, 403, 404, 500`")
}
var reference string
if len(refs) > 0 {
reference = refs[0]
}
coder := &ErrCode{
C: code,
HTTP: httpStatus,
Ext: message,
Ref: reference,
}
errors.MustRegister(coder)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/neuro-netw0rk/server-servicegolib.git
git@gitee.com:neuro-netw0rk/server-servicegolib.git
neuro-netw0rk
server-servicegolib
server-servicegolib
615e99e2ef31

搜索帮助