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

57 lines
786 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 16:57:49 +08:00
type CodeError 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
}
2023-03-07 16:57:49 +08:00
func NewCodeError(code int, msg string) CodeError {
return &codeError{
2023-02-15 17:00:55 +08:00
code: code,
msg: msg,
}
}
2023-03-07 16:57:49 +08:00
type codeError struct {
2023-02-15 17:00:55 +08:00
code int
msg string
detail string
}
2023-03-07 16:57:49 +08:00
func (e *codeError) Code() int {
2023-02-15 17:00:55 +08:00
return e.code
}
2023-03-07 16:57:49 +08:00
func (e *codeError) Msg() string {
2023-02-15 17:00:55 +08:00
return e.msg
}
2023-03-07 16:57:49 +08:00
func (e *codeError) Wrap(w ...string) error {
2023-02-15 17:00:55 +08:00
return errors.Wrap(e, strings.Join(w, ", "))
}
2023-03-07 16:57:49 +08:00
func (e *codeError) Error() string {
2023-02-15 17:00:55 +08:00
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
}