/
response.go
80 lines (72 loc) · 2 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package common
import (
"github.com/gin-gonic/gin"
"net/http"
)
const (
SuccessCode = iota
AuthFailedErrorCode
DuplicateFieldErrorCode
DBOperateErrorCode
RedisOperateErrorCode
ParamInvalidErrorCode
DBNoRowAffectedErrorCode
TemporarilyUnavailable
ErrorIDLen
ErrorID
UnknownErrorCode
TimeOutErrorCode
RemoteAllFailedErrorCode
ProjectIDRespect
SomeFieldIsNull
ExceedLimitErrorCode
SSO_ERROR
OTPErrorCode
PasswordNeedChanged
NeedCaptchaCheck
LoginIpNotInWhiteList
UserLocked
)
var ErrorDescriptions = map[int]string{
SuccessCode: "success",
AuthFailedErrorCode: "auth failed",
DuplicateFieldErrorCode: "duplicate field",
DBOperateErrorCode: "db operate error",
RedisOperateErrorCode: "redis operate error",
ParamInvalidErrorCode: "param invalid",
DBNoRowAffectedErrorCode: "db no row affected",
TemporarilyUnavailable: "resource temporarily unavailable",
ErrorIDLen: "ID MAX LEN IS 1-15",
ErrorID: "ID ONLY SYUUPRT 'A-Z/a-z/0-9/-/_'",
UnknownErrorCode: "unknown error",
ProjectIDRespect: "PROJECT ID REPECT",
SomeFieldIsNull: "SOME FIELD IS NUL",
TimeOutErrorCode: "get result timeout",
RemoteAllFailedErrorCode: "all remote instance failed",
SSO_ERROR: "sso error",
OTPErrorCode: "otp required",
PasswordNeedChanged: "password has not been updated for a long time",
NeedCaptchaCheck: "need captcha check",
LoginIpNotInWhiteList: "login ip not in whitelist",
UserLocked: "user locked",
}
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`
Data interface{} `json:"data"`
}
func (response *Response) SetError(code int) {
response.Code = code
if msg, ok := ErrorDescriptions[code]; ok {
response.Message = msg
}
}
func CreateResponse(c *gin.Context, code int, data interface{}) {
var response Response
response.SetError(code)
response.Data = data
c.JSON(
http.StatusOK,
response,
)
}