You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.3 KiB
67 lines
1.3 KiB
2 years ago
|
package constant
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
2 years ago
|
"github.com/pkg/errors"
|
||
2 years ago
|
"gorm.io/gorm"
|
||
2 years ago
|
"strings"
|
||
2 years ago
|
)
|
||
|
|
||
|
type ErrInfo struct {
|
||
|
ErrCode int32
|
||
|
ErrMsg string
|
||
|
DetailErrMsg string
|
||
|
}
|
||
|
|
||
2 years ago
|
func NewErrInfo(code int32, msg, detail string) *ErrInfo {
|
||
|
return &ErrInfo{
|
||
|
ErrCode: code,
|
||
|
ErrMsg: msg,
|
||
|
DetailErrMsg: detail,
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func (e *ErrInfo) Error() string {
|
||
2 years ago
|
return "errMsg: " + e.ErrMsg + " detail errMsg: " + e.DetailErrMsg
|
||
2 years ago
|
}
|
||
|
|
||
|
func (e *ErrInfo) Code() int32 {
|
||
|
return e.ErrCode
|
||
|
}
|
||
|
|
||
2 years ago
|
func (e *ErrInfo) Wrap(msg ...string) error {
|
||
|
return errors.Wrap(e, strings.Join(msg, "--"))
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func NewErrNetwork(err error) error {
|
||
2 years ago
|
return toDetail(err, ErrNetwork)
|
||
2 years ago
|
}
|
||
|
|
||
|
func NewErrData(err error) error {
|
||
2 years ago
|
return toDetail(err, ErrData)
|
||
2 years ago
|
}
|
||
|
|
||
|
func toDetail(err error, info *ErrInfo) *ErrInfo {
|
||
|
errInfo := *info
|
||
|
errInfo.DetailErrMsg = err.Error()
|
||
|
return &errInfo
|
||
|
}
|
||
|
|
||
|
func ToAPIErrWithErr(err error) *ErrInfo {
|
||
|
errComm := errors.New("")
|
||
|
var marshalErr *json.MarshalerError
|
||
|
errInfo := &ErrInfo{}
|
||
|
switch {
|
||
|
case errors.As(err, &errComm):
|
||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
2 years ago
|
return toDetail(err, ErrRecordNotFound)
|
||
2 years ago
|
}
|
||
2 years ago
|
return toDetail(err, ErrData)
|
||
2 years ago
|
case errors.As(err, &marshalErr):
|
||
2 years ago
|
return toDetail(err, ErrData)
|
||
2 years ago
|
case errors.As(err, &errInfo):
|
||
|
return toDetail(err, errInfo)
|
||
|
}
|
||
2 years ago
|
return toDetail(err, ErrDefaultOther)
|
||
2 years ago
|
}
|