You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.7 KiB
62 lines
1.7 KiB
package errno
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
const (
|
|
SuccessCode = 0
|
|
ServiceErrCode = 10001
|
|
ParamErrCode = 10002
|
|
UserAlreadyExistErrCode = 10003
|
|
AuthorizationFailedErrCode = 10004
|
|
BadRequestErrCode = 10005
|
|
ErrBindErrCode = 10006
|
|
InternalErrCode = 10007
|
|
RedisSetErrorCode = 10008
|
|
RedisGetErrorCode = 10009
|
|
)
|
|
|
|
type ErrNo struct {
|
|
ErrCode int
|
|
ErrMsg string
|
|
}
|
|
|
|
func (e ErrNo) Error() string {
|
|
return fmt.Sprintf("err_code=%d, err_msg=%s", e.ErrCode, e.ErrMsg)
|
|
}
|
|
|
|
func NewErrNo(code int, msg string) ErrNo {
|
|
return ErrNo{code, msg}
|
|
}
|
|
|
|
func (e ErrNo) WithMessage(msg string) ErrNo {
|
|
e.ErrMsg = msg
|
|
return e
|
|
}
|
|
|
|
var (
|
|
Success = NewErrNo(SuccessCode, "Success")
|
|
ServiceErr = NewErrNo(ServiceErrCode, "Service is unable to start successfully")
|
|
ParamErr = NewErrNo(ParamErrCode, "Wrong Parameter has been given")
|
|
UserAlreadyExistErr = NewErrNo(UserAlreadyExistErrCode, "User already exists")
|
|
AuthorizationFailedErr = NewErrNo(AuthorizationFailedErrCode, "Authorization failed")
|
|
BadRequest = NewErrNo(BadRequestErrCode, "Request Failed")
|
|
ErrBind = NewErrNo(ErrBindErrCode, "Error occurred while binding the request body to the struct")
|
|
InternalErr = NewErrNo(InternalErrCode, "Internal server error")
|
|
RedisSetErr = NewErrNo(RedisSetErrorCode, "Set data to redis error")
|
|
RedisGetErr = NewErrNo(RedisGetErrorCode, "Get data from redis error")
|
|
)
|
|
|
|
// ConvertErr convert error to Errno
|
|
func ConvertErr(err error) ErrNo {
|
|
Err := ErrNo{}
|
|
if errors.As(err, &Err) {
|
|
return Err
|
|
}
|
|
|
|
s := ServiceErr
|
|
s.ErrMsg = err.Error()
|
|
return s
|
|
}
|
|
|