56 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
package errorcode
 | 
						|
 | 
						|
import "fmt"
 | 
						|
 | 
						|
var (
 | 
						|
	Success       = &BusinessErr{code: 200, message: "成功"}
 | 
						|
	ParamError    = &BusinessErr{code: 401, message: "参数错误"}
 | 
						|
	NotFoundError = &BusinessErr{code: 404, message: "请求地址未找到"}
 | 
						|
	SystemError   = &BusinessErr{code: 405, message: "系统错误"}
 | 
						|
 | 
						|
	ClientNotFound  = &BusinessErr{code: 406, message: "未找到client_id"}
 | 
						|
	SessionNotFound = &BusinessErr{code: 407, message: "未找到会话信息"}
 | 
						|
	AuthNotFound    = &BusinessErr{code: 408, message: "身份验证失败"}
 | 
						|
	KeyNotFound     = &BusinessErr{code: 409, message: "身份验证失败"}
 | 
						|
	SysNotFound     = &BusinessErr{code: 410, message: "未找到系统信息"}
 | 
						|
	InvalidParam    = &BusinessErr{code: InvalidParamCode, message: "无效参数"}
 | 
						|
)
 | 
						|
 | 
						|
const (
 | 
						|
	InvalidParamCode = 408
 | 
						|
)
 | 
						|
 | 
						|
type BusinessErr struct {
 | 
						|
	code    int
 | 
						|
	message string
 | 
						|
}
 | 
						|
 | 
						|
func (e *BusinessErr) Error() string {
 | 
						|
	return e.message
 | 
						|
}
 | 
						|
func (e *BusinessErr) Code() int {
 | 
						|
	return e.code
 | 
						|
}
 | 
						|
 | 
						|
func (e *BusinessErr) Is(target error) bool {
 | 
						|
	_, ok := target.(*BusinessErr)
 | 
						|
	return ok
 | 
						|
}
 | 
						|
 | 
						|
// CustomErr 自定义错误
 | 
						|
func NewBusinessErr(code int, message string) *BusinessErr {
 | 
						|
	return &BusinessErr{code: code, message: message}
 | 
						|
}
 | 
						|
 | 
						|
func SysErr(message string, arg ...any) *BusinessErr {
 | 
						|
	return &BusinessErr{code: SystemError.code, message: fmt.Sprintf(message, arg)}
 | 
						|
}
 | 
						|
 | 
						|
func ParamErr(message string, arg ...any) *BusinessErr {
 | 
						|
	return &BusinessErr{code: ParamError.code, message: fmt.Sprintf(message, arg)}
 | 
						|
}
 | 
						|
 | 
						|
func (e *BusinessErr) Wrap(err error) *BusinessErr {
 | 
						|
	return NewBusinessErr(e.code, err.Error())
 | 
						|
}
 |