Skip to content

Commit

Permalink
add description to blockchain errors (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha authored Nov 5, 2023
1 parent 2badfca commit f0f0c98
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
32 changes: 28 additions & 4 deletions blockchain/bl_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
65 changes: 57 additions & 8 deletions blockchain/bl_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f0f0c98

Please sign in to comment.