Commit a6657fd5 authored by Ethen Pociask's avatar Ethen Pociask

[indexer.client] Updated json response logic

parent f43c24a8
...@@ -3,8 +3,6 @@ package routes ...@@ -3,8 +3,6 @@ package routes
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"github.com/ethereum/go-ethereum/log"
) )
const ( const (
...@@ -15,20 +13,20 @@ const ( ...@@ -15,20 +13,20 @@ const (
) )
// jsonResponse ... Marshals and writes a JSON response provided arbitrary data // jsonResponse ... Marshals and writes a JSON response provided arbitrary data
func jsonResponse(w http.ResponseWriter, logger log.Logger, data interface{}, statusCode int) { func jsonResponse(w http.ResponseWriter, data interface{}, statusCode int) error {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
jsonData, err := json.Marshal(data) jsonData, err := json.Marshal(data)
if err != nil { if err != nil {
http.Error(w, InternalServerError, http.StatusInternalServerError) http.Error(w, InternalServerError, http.StatusInternalServerError)
logger.Error("Failed to marshal JSON: %v", err) return err
return
} }
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
_, err = w.Write(jsonData) _, err = w.Write(jsonData)
if err != nil { if err != nil {
http.Error(w, InternalServerError, http.StatusInternalServerError) http.Error(w, InternalServerError, http.StatusInternalServerError)
logger.Error("Failed to write JSON data", err) return err
return
} }
return nil
} }
...@@ -78,5 +78,8 @@ func (h Routes) L1DepositsHandler(w http.ResponseWriter, r *http.Request) { ...@@ -78,5 +78,8 @@ func (h Routes) L1DepositsHandler(w http.ResponseWriter, r *http.Request) {
response := newDepositResponse(deposits) response := newDepositResponse(deposits)
jsonResponse(w, h.logger, response, http.StatusOK) err = jsonResponse(w, response, http.StatusOK)
if err != nil {
h.logger.Error("Error writing response", "err", err)
}
} }
...@@ -71,11 +71,13 @@ func (h Routes) L2WithdrawalsHandler(w http.ResponseWriter, r *http.Request) { ...@@ -71,11 +71,13 @@ func (h Routes) L2WithdrawalsHandler(w http.ResponseWriter, r *http.Request) {
withdrawals, err := h.view.L2BridgeWithdrawalsByAddress(address, cursor, limit) withdrawals, err := h.view.L2BridgeWithdrawalsByAddress(address, cursor, limit)
if err != nil { if err != nil {
http.Error(w, "Internal server error reading withdrawals", http.StatusInternalServerError) http.Error(w, "Internal server error reading withdrawals", http.StatusInternalServerError)
h.logger.Error("Unable to read withdrawals from DB") h.logger.Error("Unable to read withdrawals from DB", "err", err.Error())
h.logger.Error(err.Error())
return return
} }
response := newWithdrawalResponse(withdrawals) response := newWithdrawalResponse(withdrawals)
jsonResponse(w, h.logger, response, http.StatusOK) err = jsonResponse(w, response, http.StatusOK)
if err != nil {
h.logger.Error("Error writing response", "err", err.Error())
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment