Unify error handling

This commit is contained in:
Maciej Witowski 2023-05-05 13:16:48 +02:00
parent dbb892d89f
commit 08e6e04145

View File

@ -195,9 +195,11 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
var to address.Address
addressInput := r.FormValue("address") addressInput := r.FormValue("address")
var filecoinAddress address.Address
var decodeError error
if strings.HasPrefix(addressInput, "0x") { if strings.HasPrefix(addressInput, "0x") {
ethAddress, err := ethtypes.ParseEthAddress(addressInput) ethAddress, err := ethtypes.ParseEthAddress(addressInput)
if err != nil { if err != nil {
@ -205,15 +207,12 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
filecoinAddress, err := ethAddress.ToFilecoinAddress() filecoinAddress, decodeError = ethAddress.ToFilecoinAddress()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
to = filecoinAddress
} else { } else {
filecoinAddress, err := address.NewFromString(addressInput) filecoinAddress, decodeError = address.NewFromString(addressInput)
if err != nil { }
if decodeError != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
@ -222,11 +221,8 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
to = filecoinAddress
}
// Limit based on wallet address // Limit based on wallet address
limiter := h.limiter.GetWalletLimiter(to.String()) limiter := h.limiter.GetWalletLimiter(filecoinAddress.String())
if !limiter.Allow() { if !limiter.Allow() {
http.Error(w, http.StatusText(http.StatusTooManyRequests)+": wallet limit", http.StatusTooManyRequests) http.Error(w, http.StatusText(http.StatusTooManyRequests)+": wallet limit", http.StatusTooManyRequests)
return return
@ -252,7 +248,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
smsg, err := h.api.MpoolPushMessage(h.ctx, &types.Message{ smsg, err := h.api.MpoolPushMessage(h.ctx, &types.Message{
Value: types.BigInt(h.sendPerRequest), Value: types.BigInt(h.sendPerRequest),
From: h.from, From: h.from,
To: to, To: filecoinAddress,
}, nil) }, nil)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)