mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-27 20:30:40 +08:00
code error
This commit is contained in:
parent
3cc8e6a0a0
commit
bedb9e117b
80
pkg/errs/coderr.go
Normal file
80
pkg/errs/coderr.go
Normal file
@ -0,0 +1,80 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Code interface {
|
||||
Code() int
|
||||
Msg() string
|
||||
}
|
||||
|
||||
type Coderr interface {
|
||||
Code
|
||||
Detail() string
|
||||
WithDetail(string) Coderr
|
||||
Warp(...string) error
|
||||
error
|
||||
}
|
||||
|
||||
func NewCodeError(code int, msg string) Coderr {
|
||||
return &errInfo{
|
||||
code: code,
|
||||
msg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
type errInfo struct {
|
||||
code int
|
||||
msg string
|
||||
detail string
|
||||
}
|
||||
|
||||
func (e *errInfo) WithDetail(s string) Coderr {
|
||||
if e.detail == "" {
|
||||
e.detail = s
|
||||
} else {
|
||||
e.detail = s + ", " + e.detail
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *errInfo) Code() int {
|
||||
return e.code
|
||||
}
|
||||
|
||||
func (e *errInfo) Msg() string {
|
||||
return e.msg
|
||||
}
|
||||
|
||||
func (e *errInfo) Detail() string {
|
||||
return e.detail
|
||||
}
|
||||
|
||||
func (e *errInfo) Warp(w ...string) error {
|
||||
return errors.Wrap(e, strings.Join(w, ", "))
|
||||
}
|
||||
|
||||
func (e *errInfo) Error() string {
|
||||
return fmt.Sprintf("[%d]%s", e.code, e.msg)
|
||||
}
|
||||
|
||||
func Unwrap(err error) error {
|
||||
return utils.Unwrap(err)
|
||||
}
|
||||
|
||||
func GetCode(err error) Code {
|
||||
if err == nil {
|
||||
return NewCodeError(UnknownCode, "nil")
|
||||
}
|
||||
if code, ok := Unwrap(err).(Code); ok {
|
||||
if code.Code() == 0 {
|
||||
return NewCodeError(UnknownCode, "code == 0")
|
||||
}
|
||||
return code
|
||||
}
|
||||
return NewCodeError(UnknownCode, "unknown code")
|
||||
}
|
4
pkg/errs/define.go
Normal file
4
pkg/errs/define.go
Normal file
@ -0,0 +1,4 @@
|
||||
package errs
|
||||
|
||||
// UnknownCode 没有解析到code或解析的code=0
|
||||
const UnknownCode = 1000
|
Loading…
x
Reference in New Issue
Block a user