Files
open-im-server/pkg/errs/coderr.go
T

57 lines
768 B
Go
Raw Normal View History

2023-02-15 17:00:55 +08:00
package errs
import (
"fmt"
"github.com/pkg/errors"
"strings"
)
2023-03-07 11:12:39 +08:00
type Coderr interface {
2023-02-15 17:00:55 +08:00
Code() int
Msg() string
2023-03-07 12:19:30 +08:00
Wrap(msg ...string) error
2023-02-15 17:00:55 +08:00
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) Code() int {
return e.code
}
func (e *errInfo) Msg() string {
return e.msg
}
2023-03-07 12:19:30 +08:00
func (e *errInfo) Wrap(w ...string) error {
2023-02-15 17:00:55 +08:00
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 {
2023-03-07 12:19:30 +08:00
for err != nil {
unwrap, ok := err.(interface {
Unwrap() error
})
if !ok {
break
}
err = unwrap.Unwrap()
}
return err
2023-02-15 17:00:55 +08:00
}