-
Notifications
You must be signed in to change notification settings - Fork 182
/
rest_v2.go
159 lines (135 loc) · 4.1 KB
/
rest_v2.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
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
package common
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
jsoniter "github.com/json-iterator/go"
"github.com/okex/exchain/libs/tendermint/libs/log"
)
// const
const (
// common error
ErrorMissingRequiredParam errorCodeV2 = 60108
ErrorInvalidParam errorCodeV2 = 60109
ErrorServerException errorCodeV2 = 60110
ErrorDataNotExist errorCodeV2 = 60111
ErrorCodecFails errorCodeV2 = 60112
ErrorABCIQueryFails errorCodeV2 = 60113
ErrorArgsWithLimit errorCodeV2 = 60114
// account error
ErrorInvalidAddress errorCodeV2 = 60007
// order error
ErrorOrderNotExist errorCodeV2 = 60300
ErrorInvalidCurrency errorCodeV2 = 60301
ErrorEmptyInstrumentID errorCodeV2 = 60302
ErrorInstrumentIDNotExist errorCodeV2 = 60303
// staking error
ErrorInvalidValidatorAddress errorCodeV2 = 60700
ErrorInvalidDelegatorAddress errorCodeV2 = 60701
)
func defaultErrorMessageV2(code errorCodeV2) (message string) {
switch code {
case ErrorMissingRequiredParam:
message = "missing required param"
case ErrorInvalidParam:
message = "invalid request param"
case ErrorServerException:
message = "internal server error"
case ErrorDataNotExist:
message = "data not exists"
case ErrorCodecFails:
message = "inner CODEC failed"
case ErrorABCIQueryFails:
message = "abci query failed"
case ErrorArgsWithLimit:
message = "failed to parse args with limit"
case ErrorInvalidAddress:
message = "invalid address"
case ErrorOrderNotExist:
message = "order not exists"
case ErrorInvalidCurrency:
message = "invalid currency"
case ErrorEmptyInstrumentID:
message = "instrument_id is empty"
case ErrorInstrumentIDNotExist:
message = "instrument_id not exists"
// staking
case ErrorInvalidValidatorAddress:
message = "invalid validator address"
case ErrorInvalidDelegatorAddress:
message = "invalid delegator address"
// farm
//case ErrorInvalidAccountAddress:
// message = "invalid account address"
default:
message = "unknown error"
}
return
}
type errorCodeV2 int
func (code errorCodeV2) Code() string {
return strconv.Itoa(int(code))
}
func (code errorCodeV2) Message() string {
return defaultErrorMessageV2(code)
}
type responseErrorV2 struct {
Code string `json:"code"`
Message string `json:"message"`
}
// HandleErrorResponseV2 is the handler of error response with V2 standard
func HandleErrorResponseV2(w http.ResponseWriter, statusCode int, errCode errorCodeV2) {
response, err := json.Marshal(responseErrorV2{
Code: errCode.Code(),
Message: errCode.Message(),
})
if err == nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
if _, err = w.Write(response); err != nil {
log.NewTMLogger(os.Stdout).Debug(fmt.Sprintf("error: %v", err.Error()))
}
}
}
// HandleSuccessResponseV2 is the handler of successful response with V2 standard
func HandleSuccessResponseV2(w http.ResponseWriter, data []byte) {
logger := log.NewTMLogger(os.Stdout)
w.Header().Set("Content-Type", "application/json")
_, err := w.Write(data)
if err != nil {
logger.Debug(fmt.Sprintf("error: %v", err.Error()))
}
}
// HandleResponseV2 handles the response of V2 standard
func HandleResponseV2(w http.ResponseWriter, data []byte, err error) {
if err != nil {
HandleErrorResponseV2(w, http.StatusInternalServerError, ErrorServerException)
return
}
if len(data) == 0 {
HandleErrorResponseV2(w, http.StatusBadRequest, ErrorDataNotExist)
}
HandleSuccessResponseV2(w, data)
}
// JSONMarshalV2 marshals info into JSON based on V2 standard
func JSONMarshalV2(v interface{}) ([]byte, error) {
var jsonV2 = jsoniter.Config{
EscapeHTML: true,
SortMapKeys: true,
ValidateJsonRawMessage: true,
TagKey: "v2",
}.Froze()
return jsonV2.MarshalIndent(v, "", " ")
}
// JSONUnmarshalV2 unmarshals JSON bytes based on V2 standard
func JSONUnmarshalV2(data []byte, v interface{}) error {
var jsonV2 = jsoniter.Config{
EscapeHTML: true,
SortMapKeys: true,
ValidateJsonRawMessage: true,
TagKey: "v2",
}.Froze()
return jsonV2.Unmarshal(data, v)
}