Commit a6657fd5 authored by Ethen Pociask's avatar Ethen Pociask

[indexer.client] Updated json response logic

parent f43c24a8
......@@ -3,8 +3,6 @@ package routes
import (
"encoding/json"
"net/http"
"github.com/ethereum/go-ethereum/log"
)
const (
......@@ -15,20 +13,20 @@ const (
)
// 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")
jsonData, err := json.Marshal(data)
if err != nil {
http.Error(w, InternalServerError, http.StatusInternalServerError)
logger.Error("Failed to marshal JSON: %v", err)
return
return err
}
w.WriteHeader(statusCode)
_, err = w.Write(jsonData)
if err != nil {
http.Error(w, InternalServerError, http.StatusInternalServerError)
logger.Error("Failed to write JSON data", err)
return
return err
}
return nil
}
......@@ -78,5 +78,8 @@ func (h Routes) L1DepositsHandler(w http.ResponseWriter, r *http.Request) {
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) {
withdrawals, err := h.view.L2BridgeWithdrawalsByAddress(address, cursor, limit)
if err != nil {
http.Error(w, "Internal server error reading withdrawals", http.StatusInternalServerError)
h.logger.Error("Unable to read withdrawals from DB")
h.logger.Error(err.Error())
h.logger.Error("Unable to read withdrawals from DB", "err", err.Error())
return
}
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