Skip to content

Commit

Permalink
Update blockchain.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Nov 5, 2023
1 parent 4d7ab47 commit 887f872
Showing 1 changed file with 2 additions and 23 deletions.
25 changes: 2 additions & 23 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,30 +332,9 @@ func (bl *FxBlockchain) handleAction(method string, action string, from peer.ID,
defer cancel()
response, err := bl.callBlockchain(ctx, method, action, req)
if err != nil {
errorMessage := err.Error()
http.Error(w, errorMessage, http.StatusBadRequest)
log.Error("failed to process action request: %v", err)
var apiError map[string]interface{}

// Assuming err is an error object that contains a JSON string with the error details.
// You would unmarshal it into the map.
if unmarshalErr := json.Unmarshal([]byte(err.Error()), &apiError); unmarshalErr != nil {
log.Error("failed to decode error details: %v", unmarshalErr)
http.Error(w, "", http.StatusInternalServerError)
return
}

apiErrorJSON, jsonErr := json.Marshal(apiError)
if jsonErr != nil {
log.Error("failed to encode error response: %v", jsonErr)
http.Error(w, "", http.StatusInternalServerError)
return
}

// Set the header and write the error JSON to the response writer.
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
if _, writeErr := w.Write(apiErrorJSON); writeErr != nil {
log.Error("failed to write error response: %v", writeErr)
}
return
} else {
w.WriteHeader(http.StatusAccepted)
Expand Down

0 comments on commit 887f872

Please sign in to comment.