diff --git a/blockchain/bl_manifest.go b/blockchain/bl_manifest.go index fdc6b05e..42a4cf3b 100644 --- a/blockchain/bl_manifest.go +++ b/blockchain/bl_manifest.go @@ -37,7 +37,13 @@ func (bl *FxBlockchain) ManifestUpload(ctx context.Context, to peer.ID, r Manife case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -130,7 +136,13 @@ func (bl *FxBlockchain) ManifestRemove(ctx context.Context, to peer.ID, r Manife case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -161,7 +173,13 @@ func (bl *FxBlockchain) ManifestRemoveStorer(ctx context.Context, to peer.ID, r case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -192,7 +210,13 @@ func (bl *FxBlockchain) ManifestRemoveStored(ctx context.Context, to peer.ID, r case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } diff --git a/blockchain/bl_pool.go b/blockchain/bl_pool.go index 27bd1bc1..a50549e5 100644 --- a/blockchain/bl_pool.go +++ b/blockchain/bl_pool.go @@ -37,7 +37,13 @@ func (bl *FxBlockchain) PoolCreate(ctx context.Context, to peer.ID, r PoolCreate case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -68,7 +74,14 @@ func (bl *FxBlockchain) PoolJoin(ctx context.Context, to peer.ID, r PoolJoinRequ case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -99,7 +112,13 @@ func (bl *FxBlockchain) PoolCancelJoin(ctx context.Context, to peer.ID, r PoolCa case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -130,7 +149,13 @@ func (bl *FxBlockchain) PoolRequests(ctx context.Context, to peer.ID, r PoolRequ case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -161,7 +186,13 @@ func (bl *FxBlockchain) PoolList(ctx context.Context, to peer.ID, r PoolListRequ case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -192,7 +223,13 @@ func (bl *FxBlockchain) PoolUserList(ctx context.Context, to peer.ID, r PoolUser case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -223,7 +260,13 @@ func (bl *FxBlockchain) PoolVote(ctx context.Context, to peer.ID, r PoolVoteRequ case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } @@ -254,7 +297,13 @@ func (bl *FxBlockchain) PoolLeave(ctx context.Context, to peer.ID, r PoolLeaveRe case err != nil: return nil, err case resp.StatusCode != http.StatusAccepted: - return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + // Attempt to parse the body as JSON. + if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil { + // If we can't parse the JSON, return the original body in the error. + return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b)) + } + // Return the parsed error message and description. + return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description) default: return b, nil } diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 39a51a36..794402ac 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -22,6 +22,11 @@ import ( "github.com/libp2p/go-libp2p/core/peer" ) +var apiError struct { + Message string `json:"message"` + Description string `json:"description"` +} + const ( FxBlockchainProtocolID = "/fx.land/blockchain/0.0.1" actionAuth = "auth"