1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package main
- import (
- "fmt"
- "github.com/pkg/errors"
- )
- type Response struct {
- Code ErrCode `json:"code"`
- Msg string `json:"msg"`
- RequestId string `json:"request_id"`
- }
- type ErrCode int
- const (
-
- ServerError ErrCode = 10001
- ParamBindError ErrCode = 10002
-
-
- IllegalDatasetName ErrCode = 20101
- ParamNameError ErrCode = 20102
-
- IllegalPhoneNum ErrCode = 20201
- IllegalVerifyCode ErrCode = 20202
- PhoneRepeatedRegistered ErrCode = 20203
- PhoneIsNotRegistered ErrCode = 20204
- PhoneRepeatedApproved ErrCode = 20205
- PhoneIsNotApproved ErrCode = 20206
-
- IllegalModelName ErrCode = 20301
- )
- var errorMsg = map[ErrCode]string{
- ServerError: "服务内部错误",
- ParamBindError: "参数信息有误",
- IllegalDatasetName: "无效的数据集名称",
- ParamNameError: "参数name错误",
- IllegalPhoneNum: "手机号格式不正确",
- IllegalModelName: "非法模型名称",
- }
- func String(code ErrCode) string {
- if msg, ok := errorMsg[code]; ok {
- return msg
- }
- return "未知错误码"
- }
- func (r *Response) Error() string {
- return fmt.Sprintf("Code: %d, Msg: %s, RequestId: %s", r.Code, r.Msg, r.RequestId)
- }
- func NewCustomError(code ErrCode, requestId string) error {
- return errors.Wrap(&Response{
- Code: code,
- Msg: String(code),
- RequestId: requestId,
- }, "custom error")
- }
|