github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/common/rest_v2.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "os" 8 "strconv" 9 10 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/log" 11 jsoniter "github.com/json-iterator/go" 12 ) 13 14 // const 15 const ( 16 // common error 17 ErrorMissingRequiredParam errorCodeV2 = 60108 18 ErrorInvalidParam errorCodeV2 = 60109 19 ErrorServerException errorCodeV2 = 60110 20 ErrorDataNotExist errorCodeV2 = 60111 21 ErrorCodecFails errorCodeV2 = 60112 22 ErrorABCIQueryFails errorCodeV2 = 60113 23 ErrorArgsWithLimit errorCodeV2 = 60114 24 25 // account error 26 ErrorInvalidAddress errorCodeV2 = 60007 27 28 // order error 29 ErrorOrderNotExist errorCodeV2 = 60300 30 ErrorInvalidCurrency errorCodeV2 = 60301 31 ErrorEmptyInstrumentID errorCodeV2 = 60302 32 ErrorInstrumentIDNotExist errorCodeV2 = 60303 33 34 // staking error 35 ErrorInvalidValidatorAddress errorCodeV2 = 60700 36 ErrorInvalidDelegatorAddress errorCodeV2 = 60701 37 ) 38 39 func defaultErrorMessageV2(code errorCodeV2) (message string) { 40 switch code { 41 case ErrorMissingRequiredParam: 42 message = "missing required param" 43 case ErrorInvalidParam: 44 message = "invalid request param" 45 case ErrorServerException: 46 message = "internal server error" 47 case ErrorDataNotExist: 48 message = "data not exists" 49 case ErrorCodecFails: 50 message = "inner CODEC failed" 51 case ErrorABCIQueryFails: 52 message = "abci query failed" 53 case ErrorArgsWithLimit: 54 message = "failed to parse args with limit" 55 56 case ErrorInvalidAddress: 57 message = "invalid address" 58 59 case ErrorOrderNotExist: 60 message = "order not exists" 61 case ErrorInvalidCurrency: 62 message = "invalid currency" 63 case ErrorEmptyInstrumentID: 64 message = "instrument_id is empty" 65 case ErrorInstrumentIDNotExist: 66 message = "instrument_id not exists" 67 68 // staking 69 case ErrorInvalidValidatorAddress: 70 message = "invalid validator address" 71 case ErrorInvalidDelegatorAddress: 72 message = "invalid delegator address" 73 74 // farm 75 //case ErrorInvalidAccountAddress: 76 // message = "invalid account address" 77 78 default: 79 message = "unknown error" 80 } 81 return 82 } 83 84 type errorCodeV2 int 85 86 func (code errorCodeV2) Code() string { 87 return strconv.Itoa(int(code)) 88 } 89 90 func (code errorCodeV2) Message() string { 91 return defaultErrorMessageV2(code) 92 } 93 94 type responseErrorV2 struct { 95 Code string `json:"code"` 96 Message string `json:"message"` 97 } 98 99 // HandleErrorResponseV2 is the handler of error response with V2 standard 100 func HandleErrorResponseV2(w http.ResponseWriter, statusCode int, errCode errorCodeV2) { 101 response, err := json.Marshal(responseErrorV2{ 102 Code: errCode.Code(), 103 Message: errCode.Message(), 104 }) 105 if err == nil { 106 w.Header().Set("Content-Type", "application/json") 107 w.WriteHeader(statusCode) 108 if _, err = w.Write(response); err != nil { 109 log.NewTMLogger(os.Stdout).Debug(fmt.Sprintf("error: %v", err.Error())) 110 } 111 } 112 } 113 114 // HandleSuccessResponseV2 is the handler of successful response with V2 standard 115 func HandleSuccessResponseV2(w http.ResponseWriter, data []byte) { 116 logger := log.NewTMLogger(os.Stdout) 117 w.Header().Set("Content-Type", "application/json") 118 _, err := w.Write(data) 119 if err != nil { 120 logger.Debug(fmt.Sprintf("error: %v", err.Error())) 121 } 122 } 123 124 // HandleResponseV2 handles the response of V2 standard 125 func HandleResponseV2(w http.ResponseWriter, data []byte, err error) { 126 if err != nil { 127 HandleErrorResponseV2(w, http.StatusInternalServerError, ErrorServerException) 128 return 129 } 130 if len(data) == 0 { 131 HandleErrorResponseV2(w, http.StatusBadRequest, ErrorDataNotExist) 132 } 133 134 HandleSuccessResponseV2(w, data) 135 } 136 137 // JSONMarshalV2 marshals info into JSON based on V2 standard 138 func JSONMarshalV2(v interface{}) ([]byte, error) { 139 var jsonV2 = jsoniter.Config{ 140 EscapeHTML: true, 141 SortMapKeys: true, 142 ValidateJsonRawMessage: true, 143 TagKey: "v2", 144 }.Froze() 145 146 return jsonV2.MarshalIndent(v, "", " ") 147 } 148 149 // JSONUnmarshalV2 unmarshals JSON bytes based on V2 standard 150 func JSONUnmarshalV2(data []byte, v interface{}) error { 151 var jsonV2 = jsoniter.Config{ 152 EscapeHTML: true, 153 SortMapKeys: true, 154 ValidateJsonRawMessage: true, 155 TagKey: "v2", 156 }.Froze() 157 158 return jsonV2.Unmarshal(data, v) 159 }