118 lines
3.1 KiB
Go
118 lines
3.1 KiB
Go
package coco
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
const UnknownError = "unknown"
|
|
|
|
const (
|
|
// ErrorNil 正常
|
|
ErrorNil = 0
|
|
// ErrorInternalServer 系统错误
|
|
ErrorInternalServer = 500
|
|
// ErrorNotImplemented 方法没有实现
|
|
ErrorNotImplemented = 501
|
|
// ErrorBadGateway 网关错误
|
|
ErrorBadGateway = 502
|
|
// ErrorServiceUnavailable 服务不可用
|
|
ErrorServiceUnavailable = 503
|
|
// ErrorGatewayTimeout 网关超时
|
|
ErrorGatewayTimeout = 504
|
|
// ErrorBadRequest 请求参数无效
|
|
ErrorBadRequest = 400
|
|
// ErrorUnauthorized 未授权访问
|
|
ErrorUnauthorized = 401
|
|
// ErrorPaymentRequired 缺少参数
|
|
ErrorPaymentRequired = 402
|
|
// ErrorForbidden 禁止访问
|
|
ErrorForbidden = 403
|
|
// ErrorNotFound 找不到记录
|
|
ErrorNotFound = 404
|
|
// ErrorMethodNotAllowed 请求不被允许
|
|
ErrorMethodNotAllowed = 405
|
|
// ErrorNotAcceptable 不被接受的请求
|
|
ErrorNotAcceptable = 406
|
|
// ErrorProxyAuthenticationRequired 代理需要授权
|
|
ErrorProxyAuthenticationRequired = 407
|
|
// ErrorRequestTimeout 请求超时
|
|
ErrorRequestTimeout = 408
|
|
// ErrorConflict 资源冲突
|
|
ErrorConflict = 409
|
|
|
|
// ErrorFreqLimit 路由频率限制 [业务级]
|
|
ErrorFreqLimit = 1004
|
|
// ErrorRequestBroken 请求熔断
|
|
ErrorRequestBroken = 1005
|
|
// ErrorRequestRateLimit 请求流控 [服务级]
|
|
ErrorRequestRateLimit = 1006
|
|
// ErrorParamEmpty 请求参数为空
|
|
ErrorParamEmpty = 1007
|
|
)
|
|
|
|
var (
|
|
errCodeMap = map[int32]string{
|
|
ErrorNil: "ok",
|
|
ErrorInternalServer: "internal server error",
|
|
ErrorNotImplemented: "method not implemented",
|
|
ErrorBadGateway: "bad gateway",
|
|
ErrorServiceUnavailable: "service unavailable",
|
|
ErrorGatewayTimeout: "gateway timeout",
|
|
ErrorBadRequest: "bad request",
|
|
ErrorUnauthorized: "unauthorized",
|
|
ErrorPaymentRequired: "payment required",
|
|
ErrorForbidden: "forbidden",
|
|
ErrorNotFound: "not found",
|
|
ErrorMethodNotAllowed: "method not allowed",
|
|
ErrorNotAcceptable: "not acceptable",
|
|
ErrorProxyAuthenticationRequired: "proxy authentication is required",
|
|
ErrorRequestTimeout: "request timeout",
|
|
ErrorConflict: "conflict",
|
|
}
|
|
)
|
|
|
|
func (m *ErrMsg) Error() string {
|
|
return fmt.Sprintf("err_code: %d, err_msg: %s", m.ErrCode, m.ErrMsg)
|
|
}
|
|
|
|
func RegisterErrorCode(m map[int32]string) {
|
|
for k, v := range m {
|
|
errCodeMap[k] = v
|
|
}
|
|
}
|
|
|
|
// GetErrMsgMsg 基于错误码返回错误信息
|
|
func GetErrMsgMsg(errCode int32) string {
|
|
msg, ok := errCodeMap[errCode]
|
|
if ok {
|
|
return msg
|
|
}
|
|
|
|
return UnknownError
|
|
}
|
|
|
|
// GetErrMsg 基于错误码返回错误信息
|
|
func GetErrMsg(errCode int32) *ErrMsg {
|
|
return &ErrMsg{ErrCode: errCode, ErrMsg: GetErrMsgMsg(errCode)}
|
|
}
|
|
|
|
// GetErrMsgCode 获取错误码 默认1 业务错误
|
|
func GetErrMsgCode(err error) int {
|
|
if err == nil {
|
|
return 0
|
|
}
|
|
|
|
var p *ErrMsg
|
|
if errors.As(err, &p) {
|
|
return int(p.ErrCode)
|
|
}
|
|
|
|
return 1
|
|
}
|
|
|
|
// CreateErrorWithMsg 自定义创建错误
|
|
func CreateErrorWithMsg(errCode int32, errMsg string) *ErrMsg {
|
|
return &ErrMsg{ErrCode: errCode, ErrMsg: errMsg, Autonomy: true}
|
|
}
|