-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
constants.go
51 lines (45 loc) · 1.36 KB
/
constants.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
package api
type ErrorCode int
const (
NoError ErrorCode = iota
UserMessageParseError
UnsupportedDONIdError
InternalHandlerError
RequestTimeoutError
NodeReponseEncodingError
FatalError
)
// See https://www.jsonrpc.org/specification#error_object
func ToJsonRPCErrorCode(errorCode ErrorCode) int {
gatewayErrorToJsonRPCError := map[ErrorCode]int{
NoError: 0,
UserMessageParseError: -32700, // Parse Error
UnsupportedDONIdError: -32602, // Invalid Params
InternalHandlerError: -32000, // Server Error
RequestTimeoutError: -32000, // Server Error
NodeReponseEncodingError: -32603, // Internal Error
FatalError: -32000, // Server Error
}
code, ok := gatewayErrorToJsonRPCError[errorCode]
if !ok {
return -32000
}
return code
}
// See https://go.dev/src/net/http/status.go
func ToHttpErrorCode(errorCode ErrorCode) int {
gatewayErrorToHttpError := map[ErrorCode]int{
NoError: 200, // OK
UserMessageParseError: 400, // Bad Request
UnsupportedDONIdError: 400, // Bad Request
InternalHandlerError: 500, // Internal Server Error
RequestTimeoutError: 504, // Gateway Timeout
NodeReponseEncodingError: 500, // Internal Server Error
FatalError: 500, // Internal Server Error
}
code, ok := gatewayErrorToHttpError[errorCode]
if !ok {
return 500
}
return code
}