60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
|
package errorx
|
||
|
|
||
|
import (
|
||
|
response "matrix-middle-service/pkg/resp"
|
||
|
)
|
||
|
|
||
|
type CodeError struct {
|
||
|
*response.Response
|
||
|
}
|
||
|
|
||
|
type CodeErrorResponse struct {
|
||
|
*response.Response
|
||
|
}
|
||
|
|
||
|
func NewCustomError(code int, data interface{}, msg string) error {
|
||
|
return &CodeError{
|
||
|
Response: response.NewCustomResponse(code, data, msg),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewErrorWithCode(code int, msg string) error {
|
||
|
return &CodeError{
|
||
|
Response: response.NewCustomResponse(code, nil, msg),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func ParseError(err error) error {
|
||
|
if err != nil {
|
||
|
return NewErrorWithCode(response.ERROR, err.Error())
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func ParseErrorWithCode(code int, err error) error {
|
||
|
if err != nil {
|
||
|
return NewErrorWithCode(code, err.Error())
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func NewDefaultError(msg string) error {
|
||
|
return NewErrorWithCode(response.ERROR, msg)
|
||
|
}
|
||
|
|
||
|
func (e *CodeError) Error() string {
|
||
|
return e.Msg
|
||
|
}
|
||
|
|
||
|
func (e *CodeError) Resp() *CodeErrorResponse {
|
||
|
return &CodeErrorResponse{
|
||
|
Response: &response.Response{
|
||
|
Code: e.Code,
|
||
|
Data: e.Data,
|
||
|
Msg: e.Msg,
|
||
|
},
|
||
|
}
|
||
|
}
|