Skip to content

Commit

Permalink
generalized error format
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Nov 5, 2023
1 parent f420e09 commit 9b037ee
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,16 @@ func (bl *FxBlockchain) handleAction(method string, action string, from peer.ID,
response, err := bl.callBlockchain(ctx, method, action, req)
if err != nil {
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",
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
}

// Marshal the error to JSON.
apiErrorJSON, jsonErr := json.Marshal(apiError)
if jsonErr != nil {
log.Error("failed to encode error response: %v", jsonErr)
Expand All @@ -352,8 +353,8 @@ func (bl *FxBlockchain) handleAction(method string, action string, from peer.ID,
// 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)
if _, writeErr := w.Write(apiErrorJSON); writeErr != nil {
log.Error("failed to write error response: %v", writeErr)
}
return
} else {
Expand Down

0 comments on commit 9b037ee

Please sign in to comment.