forked from songzhaoliang/echotool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.go
More file actions
160 lines (141 loc) · 4.81 KB
/
Copy pathcode.go
File metadata and controls
160 lines (141 loc) · 4.81 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package echotool
import (
"net/http"
)
const (
CodeOKZero = 0
CodeOK = 20000
CodeCreated = 20100
CodePartialContent = 20600
CodeMultipleChoices = 30000
CodeMovedPermanently = 30100
CodeFound = 30200
CodeSeeOther = 30300
CodeNotModified = 30400
CodeUseProxy = 30500
CodeTemporaryRedirect = 30700
CodePermanentRedirect = 30800
CodeBadRequest = 40000
CodeUnauthorized = 40100
CodeForbidden = 40300
CodeNotFound = 40400
CodeTooManyRequests = 42900
CodeValidateErr = 45000
CodeInternalErr = 50000
CodeServiceUnavailable = 50300
CodeParseDataErr = 50013
CodeMySQLErr = 50014
CodePostgreSQLErr = 50015
CodeRedisErr = 50016
CodeClickHouseErr = 50017
CodeMongoDBErr = 50018
CodeElasticsearchErr = 50019
CodeNSQErr = 50030
CodeKafkaErr = 50031
CodeRocketMQErr = 50032
CodeBindErr = 50040
CodeEncodeErr = 50041
CodeDownstreamErr = 50099
)
var codeMsg = map[int]string{
CodeOKZero: "success",
CodeOK: "success",
CodeCreated: "created",
CodePartialContent: "partial content",
CodeMultipleChoices: "multiple choices",
CodeMovedPermanently: "moved permanently",
CodeFound: "found",
CodeSeeOther: "see other",
CodeNotModified: "not modified",
CodeUseProxy: "use proxy",
CodeTemporaryRedirect: "temporary redirect",
CodePermanentRedirect: "permanent redirect",
CodeBadRequest: "bad request",
CodeUnauthorized: "unauthorized",
CodeForbidden: "forbidden",
CodeNotFound: "not found",
CodeTooManyRequests: "too many requests",
CodeValidateErr: "validate error",
CodeInternalErr: "internal error",
CodeServiceUnavailable: "service unavailable",
CodeParseDataErr: "parse request data error",
CodeMySQLErr: "mysql error",
CodePostgreSQLErr: "postgresql error",
CodeRedisErr: "redis error",
CodeClickHouseErr: "clickhouse error",
CodeMongoDBErr: "mongodb error",
CodeElasticsearchErr: "elasticsearch error",
CodeNSQErr: "nsq error",
CodeKafkaErr: "kafka error",
CodeRocketMQErr: "rocketmq error",
CodeBindErr: "bind error",
CodeEncodeErr: "encode error",
CodeDownstreamErr: "downstream error",
}
func CodeMsg(code int) string {
if msg, exists := codeMsg[code]; exists {
return msg
}
return "unknown code"
}
var httpStatus = map[int]int{
CodeOKZero: http.StatusOK,
CodeOK: http.StatusOK,
CodeCreated: http.StatusCreated,
CodePartialContent: http.StatusPartialContent,
CodeMultipleChoices: http.StatusMultipleChoices,
CodeMovedPermanently: http.StatusMovedPermanently,
CodeFound: http.StatusFound,
CodeSeeOther: http.StatusSeeOther,
CodeNotModified: http.StatusNotModified,
CodeUseProxy: http.StatusUseProxy,
CodeTemporaryRedirect: http.StatusTemporaryRedirect,
CodePermanentRedirect: http.StatusPermanentRedirect,
CodeBadRequest: http.StatusBadRequest,
CodeUnauthorized: http.StatusUnauthorized,
CodeForbidden: http.StatusForbidden,
CodeNotFound: http.StatusNotFound,
CodeTooManyRequests: http.StatusTooManyRequests,
CodeValidateErr: http.StatusBadRequest,
CodeInternalErr: http.StatusInternalServerError,
CodeServiceUnavailable: http.StatusInternalServerError,
CodeParseDataErr: http.StatusInternalServerError,
CodeMySQLErr: http.StatusInternalServerError,
CodePostgreSQLErr: http.StatusInternalServerError,
CodeRedisErr: http.StatusInternalServerError,
CodeClickHouseErr: http.StatusInternalServerError,
CodeMongoDBErr: http.StatusInternalServerError,
CodeElasticsearchErr: http.StatusInternalServerError,
CodeNSQErr: http.StatusInternalServerError,
CodeKafkaErr: http.StatusInternalServerError,
CodeRocketMQErr: http.StatusInternalServerError,
CodeBindErr: http.StatusInternalServerError,
CodeEncodeErr: http.StatusInternalServerError,
CodeDownstreamErr: http.StatusInternalServerError,
}
const (
UnknownStatus = 999
)
func HTTPStatus(code int) int {
if status, exists := httpStatus[code]; exists {
return status
}
return UnknownStatus
}
// RegisterCode will not cover code and status which exists.
func RegisterCode(code int, msg string, status int) bool {
if _, exists := codeMsg[code]; exists {
return false
}
if _, exists := httpStatus[code]; exists {
return false
}
codeMsg[code] = msg
httpStatus[code] = status
return true
}
// ForceRegisterCode will force to cover codeMsg map and httpStatus map.
func ForceRegisterCode(code int, msg string, status int) {
codeMsg[code] = msg
httpStatus[code] = status
}