Skip to content

Commit

Permalink
Descriptive bl response (#178)
Browse files Browse the repository at this point in the history
* add description to blockchain errors

* Update blockchain.go
  • Loading branch information
ehsan6sha authored Nov 5, 2023
1 parent f0f0c98 commit 556fbd1
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,29 @@ func (bl *FxBlockchain) handleAction(method string, action string, from peer.ID,
defer cancel()
response, err := bl.callBlockchain(ctx, method, action, req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Error("failed to process action request: %v", err)
apiError := struct {
Message string `json:"message"`
Description string `json:"description"`
}{
Message: "Pallet: Pool, Variant: UserBusy",
Description: "Fula Pool error",
}

// Marshal the error to JSON.
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 _, err := w.Write(apiErrorJSON); err != nil {
log.Error("failed to write error response: %v", err)
}
return
} else {
w.WriteHeader(http.StatusAccepted)
Expand Down

0 comments on commit 556fbd1

Please sign in to comment.