/
constants.go
72 lines (65 loc) · 1.79 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package api
type ErrorCode int
const (
NoError ErrorCode = iota
UserMessageParseError
UnsupportedDONIdError
HandlerError
RequestTimeoutError
NodeReponseEncodingError
FatalError
)
func (e ErrorCode) String() string {
switch e {
case NoError:
return "NoError"
case UserMessageParseError:
return "UserMessageParseError"
case UnsupportedDONIdError:
return "UnsupportedDONIdError"
case HandlerError:
return "HandlerError"
case RequestTimeoutError:
return "RequestTimeoutError"
case NodeReponseEncodingError:
return "NodeReponseEncodingError"
case FatalError:
return "FatalError"
default:
return "UnknownError"
}
}
// 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
HandlerError: -32600, // Invalid Request
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
HandlerError: 400, // Bad Request
RequestTimeoutError: 504, // Gateway Timeout
NodeReponseEncodingError: 500, // Internal Server Error
FatalError: 500, // Internal Server Error
}
code, ok := gatewayErrorToHttpError[errorCode]
if !ok {
return 500
}
return code
}