potato/pkg/errcode/errcode.go

64 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Date: 2021-06-10 16:30:10
* @LastEditors: viletyy
* @LastEditTime: 2021-06-18 22:22:55
* @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
}
return http.StatusBadRequest
}