sk-matrix-project/backend/golang/matrix-middle-service/pkg/resp/errorx/errorx.go
2023-02-26 02:50:56 +08:00

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,
},
}
}