Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling improvement for client package #437

Merged
merged 4 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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