Skip to content

Commit

Permalink
Error handling improvement for client package (#437)
Browse files Browse the repository at this point in the history
* feat: client error handling

Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>

* fix: unit test

Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>

* fix: check gen

Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>

* doc: update doc

Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>

Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>
  • Loading branch information
GeekArthur authored Aug 29, 2022
1 parent a48f742 commit 43f12b4
Show file tree
Hide file tree
Showing 14 changed files with 369 additions and 210 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ Helpful commands for development:
make deps
```

### Generate Types and Helpers
### Generate Types, Client and Server
```
make gen
```
If you want to modify client and server, please modify files under `templates/client` and `templates/server` then run `make gen`

### Run Tests
```
Expand Down
52 changes: 34 additions & 18 deletions client/api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,50 +78,58 @@ func (a *AccountAPIService) AccountBalance(

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to prepare request: %w", err)
}

localVarHTTPResponse, err := a.client.callAPI(ctx, r)
if err != nil || localVarHTTPResponse == nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to call API: %w", err)
}

localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
defer localVarHTTPResponse.Body.Close()
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to read response: %w", err)
}

switch localVarHTTPResponse.StatusCode {
case _nethttp.StatusOK:
var v types.AccountBalanceResponse
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 200, response body %s: %w",
string(localVarBody),
err,
)
}

return &v, nil, nil
case _nethttp.StatusInternalServerError:
var v types.Error
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 500, response body %s: %w",
string(localVarBody),
err,
)
}

return nil, &v, fmt.Errorf("%+v", v)
return nil, &v, fmt.Errorf("error %+v", v)
case _nethttp.StatusBadGateway,
_nethttp.StatusServiceUnavailable,
_nethttp.StatusGatewayTimeout,
_nethttp.StatusRequestTimeout:
return nil, nil, fmt.Errorf(
"%w: code: %d body: %s",
ErrRetriable,
"status code %d, response body %s: %w",
localVarHTTPResponse.StatusCode,
string(localVarBody),
ErrRetriable,
)
default:
return nil, nil, fmt.Errorf(
"invalid status code: %d body: %s",
"invalid status code %d, response body %s",
localVarHTTPResponse.StatusCode,
string(localVarBody),
)
Expand Down Expand Up @@ -173,50 +181,58 @@ func (a *AccountAPIService) AccountCoins(

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to prepare request: %w", err)
}

localVarHTTPResponse, err := a.client.callAPI(ctx, r)
if err != nil || localVarHTTPResponse == nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to call API: %w", err)
}

localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
defer localVarHTTPResponse.Body.Close()
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to read response: %w", err)
}

switch localVarHTTPResponse.StatusCode {
case _nethttp.StatusOK:
var v types.AccountCoinsResponse
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 200, response body %s: %w",
string(localVarBody),
err,
)
}

return &v, nil, nil
case _nethttp.StatusInternalServerError:
var v types.Error
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 500, response body %s: %w",
string(localVarBody),
err,
)
}

return nil, &v, fmt.Errorf("%+v", v)
return nil, &v, fmt.Errorf("error %+v", v)
case _nethttp.StatusBadGateway,
_nethttp.StatusServiceUnavailable,
_nethttp.StatusGatewayTimeout,
_nethttp.StatusRequestTimeout:
return nil, nil, fmt.Errorf(
"%w: code: %d body: %s",
ErrRetriable,
"status code %d, response body %s: %w",
localVarHTTPResponse.StatusCode,
string(localVarBody),
ErrRetriable,
)
default:
return nil, nil, fmt.Errorf(
"invalid status code: %d body: %s",
"invalid status code %d, response body %s",
localVarHTTPResponse.StatusCode,
string(localVarBody),
)
Expand Down
52 changes: 34 additions & 18 deletions client/api_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,50 +75,58 @@ func (a *BlockAPIService) Block(

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to prepare request: %w", err)
}

localVarHTTPResponse, err := a.client.callAPI(ctx, r)
if err != nil || localVarHTTPResponse == nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to call API: %w", err)
}

localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
defer localVarHTTPResponse.Body.Close()
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to read response: %w", err)
}

switch localVarHTTPResponse.StatusCode {
case _nethttp.StatusOK:
var v types.BlockResponse
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 200, response body %s: %w",
string(localVarBody),
err,
)
}

return &v, nil, nil
case _nethttp.StatusInternalServerError:
var v types.Error
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 500, response body %s: %w",
string(localVarBody),
err,
)
}

return nil, &v, fmt.Errorf("%+v", v)
return nil, &v, fmt.Errorf("error %+v", v)
case _nethttp.StatusBadGateway,
_nethttp.StatusServiceUnavailable,
_nethttp.StatusGatewayTimeout,
_nethttp.StatusRequestTimeout:
return nil, nil, fmt.Errorf(
"%w: code: %d body: %s",
ErrRetriable,
"status code %d, response body %s: %w",
localVarHTTPResponse.StatusCode,
string(localVarBody),
ErrRetriable,
)
default:
return nil, nil, fmt.Errorf(
"invalid status code: %d body: %s",
"invalid status code %d, response body %s",
localVarHTTPResponse.StatusCode,
string(localVarBody),
)
Expand Down Expand Up @@ -173,50 +181,58 @@ func (a *BlockAPIService) BlockTransaction(

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to prepare request: %w", err)
}

localVarHTTPResponse, err := a.client.callAPI(ctx, r)
if err != nil || localVarHTTPResponse == nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to call API: %w", err)
}

localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
defer localVarHTTPResponse.Body.Close()
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to read response: %w", err)
}

switch localVarHTTPResponse.StatusCode {
case _nethttp.StatusOK:
var v types.BlockTransactionResponse
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 200, response body %s: %w",
string(localVarBody),
err,
)
}

return &v, nil, nil
case _nethttp.StatusInternalServerError:
var v types.Error
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 500, response body %s: %w",
string(localVarBody),
err,
)
}

return nil, &v, fmt.Errorf("%+v", v)
return nil, &v, fmt.Errorf("error %+v", v)
case _nethttp.StatusBadGateway,
_nethttp.StatusServiceUnavailable,
_nethttp.StatusGatewayTimeout,
_nethttp.StatusRequestTimeout:
return nil, nil, fmt.Errorf(
"%w: code: %d body: %s",
ErrRetriable,
"status code %d, response body %s: %w",
localVarHTTPResponse.StatusCode,
string(localVarBody),
ErrRetriable,
)
default:
return nil, nil, fmt.Errorf(
"invalid status code: %d body: %s",
"invalid status code %d, response body %s",
localVarHTTPResponse.StatusCode,
string(localVarBody),
)
Expand Down
26 changes: 17 additions & 9 deletions client/api_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,50 +78,58 @@ func (a *CallAPIService) Call(

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to prepare request: %w", err)
}

localVarHTTPResponse, err := a.client.callAPI(ctx, r)
if err != nil || localVarHTTPResponse == nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to call API: %w", err)
}

localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
defer localVarHTTPResponse.Body.Close()
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to read response: %w", err)
}

switch localVarHTTPResponse.StatusCode {
case _nethttp.StatusOK:
var v types.CallResponse
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 200, response body %s: %w",
string(localVarBody),
err,
)
}

return &v, nil, nil
case _nethttp.StatusInternalServerError:
var v types.Error
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf(
"failed to decode when hit status code 500, response body %s: %w",
string(localVarBody),
err,
)
}

return nil, &v, fmt.Errorf("%+v", v)
return nil, &v, fmt.Errorf("error %+v", v)
case _nethttp.StatusBadGateway,
_nethttp.StatusServiceUnavailable,
_nethttp.StatusGatewayTimeout,
_nethttp.StatusRequestTimeout:
return nil, nil, fmt.Errorf(
"%w: code: %d body: %s",
ErrRetriable,
"status code %d, response body %s: %w",
localVarHTTPResponse.StatusCode,
string(localVarBody),
ErrRetriable,
)
default:
return nil, nil, fmt.Errorf(
"invalid status code: %d body: %s",
"invalid status code %d, response body %s",
localVarHTTPResponse.StatusCode,
string(localVarBody),
)
Expand Down
Loading

0 comments on commit 43f12b4

Please sign in to comment.