2021-06-10 19:03:15 +08:00
|
|
|
|
/*
|
|
|
|
|
* @Date: 2021-06-10 16:30:10
|
|
|
|
|
* @LastEditors: viletyy
|
2021-06-18 22:40:33 +08:00
|
|
|
|
* @LastEditTime: 2021-06-18 22:22:55
|
2021-06-10 19:03:15 +08:00
|
|
|
|
* @FilePath: /potato/pkg/errcode/errcode.go
|
|
|
|
|
*/
|
|
|
|
|
package errcode
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
|
Data interface{} `json:"data,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var codes = map[int]string{}
|
|
|
|
|
|
|
|
|
|
func NewError(code int, msg string) *Error {
|
|
|
|
|
if _, ok := codes[code]; ok {
|
|
|
|
|
panic(fmt.Sprintf("错误码 %d 已存在,请更换一个", code))
|
|
|
|
|
}
|
|
|
|
|
codes[code] = msg
|
|
|
|
|
return &Error{Code: code, Msg: msg}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
|
return fmt.Sprintf("错误码:%d,错误信息:%s", e.Code, e.Msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Error) Msgf(args []interface{}) string {
|
|
|
|
|
return fmt.Sprintf(e.Msg, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Error) WithData(data interface{}) {
|
|
|
|
|
e.Data = data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Error) StatusCode() int {
|
|
|
|
|
switch e.Code {
|
|
|
|
|
case Success.Code:
|
|
|
|
|
return http.StatusOK
|
|
|
|
|
case ServerError.Code:
|
|
|
|
|
return http.StatusInternalServerError
|
|
|
|
|
case InvalidParams.Code:
|
|
|
|
|
return http.StatusBadRequest
|
|
|
|
|
case UnauthorizedAuthNotExist.Code:
|
|
|
|
|
fallthrough
|
|
|
|
|
case UnauthorizedTokenError.Code:
|
|
|
|
|
fallthrough
|
|
|
|
|
case UnauthorizedTokenGenerate.Code:
|
|
|
|
|
fallthrough
|
|
|
|
|
case UnauthorizedTokenTimeout.Code:
|
|
|
|
|
return http.StatusUnauthorized
|
|
|
|
|
case TooManyRequests.Code:
|
|
|
|
|
return http.StatusTooManyRequests
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 15:46:30 +08:00
|
|
|
|
return http.StatusBadRequest
|
2021-06-10 19:03:15 +08:00
|
|
|
|
}
|