From 43f12b453655967509beea7b15405a8517c0de5a Mon Sep 17 00:00:00 2001 From: Jingfu Wang Date: Mon, 29 Aug 2022 17:10:57 -0400 Subject: [PATCH] Error handling improvement for client package (#437) * feat: client error handling Signed-off-by: Jingfu Wang * fix: unit test Signed-off-by: Jingfu Wang * fix: check gen Signed-off-by: Jingfu Wang * doc: update doc Signed-off-by: Jingfu Wang Signed-off-by: Jingfu Wang --- README.md | 3 +- client/api_account.go | 52 ++++-- client/api_block.go | 52 ++++-- client/api_call.go | 26 ++- client/api_construction.go | 208 ++++++++++++++++-------- client/api_events.go | 26 ++- client/api_mempool.go | 52 ++++-- client/api_network.go | 78 ++++++--- client/api_search.go | 26 ++- client/client.go | 13 +- client/configuration.go | 6 +- templates/client/api.mustache | 18 +- templates/client/client.mustache | 13 +- templates/client/configuration.mustache | 6 +- 14 files changed, 369 insertions(+), 210 deletions(-) diff --git a/README.md b/README.md index 9940b2ee8..a287283e9 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/client/api_account.go b/client/api_account.go index 567206dc5..d6e49a9a9 100644 --- a/client/api_account.go +++ b/client/api_account.go @@ -78,18 +78,18 @@ 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 { @@ -97,7 +97,11 @@ func (a *AccountAPIService) AccountBalance( 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 @@ -105,23 +109,27 @@ func (a *AccountAPIService) AccountBalance( 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), ) @@ -173,18 +181,18 @@ 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 { @@ -192,7 +200,11 @@ func (a *AccountAPIService) AccountCoins( 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 @@ -200,23 +212,27 @@ func (a *AccountAPIService) AccountCoins( 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), ) diff --git a/client/api_block.go b/client/api_block.go index 04db91539..d42f7a02a 100644 --- a/client/api_block.go +++ b/client/api_block.go @@ -75,18 +75,18 @@ 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 { @@ -94,7 +94,11 @@ func (a *BlockAPIService) Block( 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 @@ -102,23 +106,27 @@ func (a *BlockAPIService) Block( 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), ) @@ -173,18 +181,18 @@ 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 { @@ -192,7 +200,11 @@ func (a *BlockAPIService) BlockTransaction( 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 @@ -200,23 +212,27 @@ func (a *BlockAPIService) BlockTransaction( 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), ) diff --git a/client/api_call.go b/client/api_call.go index bbece7f3e..f8ae99684 100644 --- a/client/api_call.go +++ b/client/api_call.go @@ -78,18 +78,18 @@ 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 { @@ -97,7 +97,11 @@ func (a *CallAPIService) Call( 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 @@ -105,23 +109,27 @@ func (a *CallAPIService) Call( 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), ) diff --git a/client/api_construction.go b/client/api_construction.go index cc5ef08ae..05b1c0ffc 100644 --- a/client/api_construction.go +++ b/client/api_construction.go @@ -70,18 +70,18 @@ func (a *ConstructionAPIService) ConstructionCombine( 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 { @@ -89,7 +89,11 @@ func (a *ConstructionAPIService) ConstructionCombine( var v types.ConstructionCombineResponse 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 @@ -97,23 +101,27 @@ func (a *ConstructionAPIService) ConstructionCombine( 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), ) @@ -156,18 +164,18 @@ func (a *ConstructionAPIService) ConstructionDerive( 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 { @@ -175,7 +183,11 @@ func (a *ConstructionAPIService) ConstructionDerive( var v types.ConstructionDeriveResponse 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 @@ -183,23 +195,27 @@ func (a *ConstructionAPIService) ConstructionDerive( 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), ) @@ -242,18 +258,18 @@ func (a *ConstructionAPIService) ConstructionHash( 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 { @@ -261,7 +277,11 @@ func (a *ConstructionAPIService) ConstructionHash( var v types.TransactionIdentifierResponse 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 @@ -269,23 +289,27 @@ func (a *ConstructionAPIService) ConstructionHash( 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), ) @@ -335,18 +359,18 @@ func (a *ConstructionAPIService) ConstructionMetadata( 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 { @@ -354,7 +378,11 @@ func (a *ConstructionAPIService) ConstructionMetadata( var v types.ConstructionMetadataResponse 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 @@ -362,23 +390,27 @@ func (a *ConstructionAPIService) ConstructionMetadata( 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), ) @@ -422,18 +454,18 @@ func (a *ConstructionAPIService) ConstructionParse( 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 { @@ -441,7 +473,11 @@ func (a *ConstructionAPIService) ConstructionParse( var v types.ConstructionParseResponse 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 @@ -449,23 +485,27 @@ func (a *ConstructionAPIService) ConstructionParse( 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), ) @@ -514,18 +554,18 @@ func (a *ConstructionAPIService) ConstructionPayloads( 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 { @@ -533,7 +573,11 @@ func (a *ConstructionAPIService) ConstructionPayloads( var v types.ConstructionPayloadsResponse 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 @@ -541,23 +585,27 @@ func (a *ConstructionAPIService) ConstructionPayloads( 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), ) @@ -604,18 +652,18 @@ func (a *ConstructionAPIService) ConstructionPreprocess( 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 { @@ -623,7 +671,11 @@ func (a *ConstructionAPIService) ConstructionPreprocess( var v types.ConstructionPreprocessResponse 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 @@ -631,23 +683,27 @@ func (a *ConstructionAPIService) ConstructionPreprocess( 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), ) @@ -693,18 +749,18 @@ func (a *ConstructionAPIService) ConstructionSubmit( 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 { @@ -712,7 +768,11 @@ func (a *ConstructionAPIService) ConstructionSubmit( var v types.TransactionIdentifierResponse 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 @@ -720,23 +780,27 @@ func (a *ConstructionAPIService) ConstructionSubmit( 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), ) diff --git a/client/api_events.go b/client/api_events.go index 1d3e38e31..086b7dbbd 100644 --- a/client/api_events.go +++ b/client/api_events.go @@ -73,18 +73,18 @@ func (a *EventsAPIService) EventsBlocks( 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 { @@ -92,7 +92,11 @@ func (a *EventsAPIService) EventsBlocks( var v types.EventsBlocksResponse 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 @@ -100,23 +104,27 @@ func (a *EventsAPIService) EventsBlocks( 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), ) diff --git a/client/api_mempool.go b/client/api_mempool.go index 727f708bc..b694c6823 100644 --- a/client/api_mempool.go +++ b/client/api_mempool.go @@ -68,18 +68,18 @@ func (a *MempoolAPIService) Mempool( 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 { @@ -87,7 +87,11 @@ func (a *MempoolAPIService) Mempool( var v types.MempoolResponse 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 @@ -95,23 +99,27 @@ func (a *MempoolAPIService) Mempool( 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), ) @@ -159,18 +167,18 @@ func (a *MempoolAPIService) MempoolTransaction( 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 { @@ -178,7 +186,11 @@ func (a *MempoolAPIService) MempoolTransaction( var v types.MempoolTransactionResponse 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 @@ -186,23 +198,27 @@ func (a *MempoolAPIService) MempoolTransaction( 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), ) diff --git a/client/api_network.go b/client/api_network.go index 8e644b573..a471b4aab 100644 --- a/client/api_network.go +++ b/client/api_network.go @@ -68,18 +68,18 @@ func (a *NetworkAPIService) NetworkList( 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 { @@ -87,7 +87,11 @@ func (a *NetworkAPIService) NetworkList( var v types.NetworkListResponse 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 @@ -95,23 +99,27 @@ func (a *NetworkAPIService) NetworkList( 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), ) @@ -156,18 +164,18 @@ func (a *NetworkAPIService) NetworkOptions( 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 { @@ -175,7 +183,11 @@ func (a *NetworkAPIService) NetworkOptions( var v types.NetworkOptionsResponse 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 @@ -183,23 +195,27 @@ func (a *NetworkAPIService) NetworkOptions( 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), ) @@ -242,18 +258,18 @@ func (a *NetworkAPIService) NetworkStatus( 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 { @@ -261,7 +277,11 @@ func (a *NetworkAPIService) NetworkStatus( var v types.NetworkStatusResponse 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 @@ -269,23 +289,27 @@ func (a *NetworkAPIService) NetworkStatus( 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), ) diff --git a/client/api_search.go b/client/api_search.go index cb07192e7..a84d0068e 100644 --- a/client/api_search.go +++ b/client/api_search.go @@ -73,18 +73,18 @@ func (a *SearchAPIService) SearchTransactions( 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 { @@ -92,7 +92,11 @@ func (a *SearchAPIService) SearchTransactions( var v types.SearchTransactionsResponse 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 @@ -100,23 +104,27 @@ func (a *SearchAPIService) SearchTransactions( 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), ) diff --git a/client/client.go b/client/client.go index 9ce83b264..b79797231 100644 --- a/client/client.go +++ b/client/client.go @@ -151,7 +151,7 @@ func (c *APIClient) callAPI(ctx context.Context, request *http.Request) (*http.R log.Printf("\n%s\n", string(dump)) } - return resp, err + return resp, nil } // ChangeBasePath changes base path to allow switching to mocks @@ -185,14 +185,14 @@ func (c *APIClient) prepareRequest( body, err = setBody(postBody, contentType) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to set body: %w", err) } } // Setup path and query parameters url, err := url.Parse(path) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to parse path %s: %w", path, err) } // Override request host, if applicable @@ -208,7 +208,7 @@ func (c *APIClient) prepareRequest( // Generate a new request localVarRequest, err = http.NewRequest(http.MethodPost, url.String(), body) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to generate new request: %w", err) } // add header parameters, if any @@ -247,7 +247,7 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err if jsonCheck.MatchString(contentType) { if err = json.Unmarshal(b, v); err != nil { - return err + return fmt.Errorf("failed to unmarshal: %w", err) } return nil } @@ -278,8 +278,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s", contentType) - return nil, err + return nil, fmt.Errorf("invalid body type %s", contentType) } return bodyBuf, nil } diff --git a/client/configuration.go b/client/configuration.go index 9c4086eb0..0d4d218d6 100644 --- a/client/configuration.go +++ b/client/configuration.go @@ -114,8 +114,8 @@ func (c *Configuration) AddDefaultHeader(key string, value string) { // ServerURL returns URL based on server settings func (c *Configuration) ServerURL(index int, variables map[string]string) (string, error) { - if index < 0 || len(c.Servers) <= index { - return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers)-1) + if index < 0 || index >= len(c.Servers) { + return "", fmt.Errorf("index %d out of range [0-%d]", index, len(c.Servers)-1) } server := c.Servers[index] url := server.URL @@ -131,7 +131,7 @@ func (c *Configuration) ServerURL(index int, variables map[string]string) (strin } if !found { return "", fmt.Errorf( - "The variable %s in the server URL has invalid value %v. Must be %v", + "the variable %s in the server URL has invalid value %s. Must be %v", name, value, variable.EnumValues, diff --git a/templates/client/api.mustache b/templates/client/api.mustache index 7002bc1d0..1532edf2e 100644 --- a/templates/client/api.mustache +++ b/templates/client/api.mustache @@ -74,18 +74,18 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams 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 { @@ -95,7 +95,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams {{/returnType}} 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 @@ -103,23 +103,23 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams 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), ) diff --git a/templates/client/client.mustache b/templates/client/client.mustache index 7caab62b4..548a93bbc 100644 --- a/templates/client/client.mustache +++ b/templates/client/client.mustache @@ -127,7 +127,7 @@ func (c *APIClient) callAPI(ctx context.Context, request *http.Request) (*http.R log.Printf("\n%s\n", string(dump)) } - return resp, err + return resp, nil } // ChangeBasePath changes base path to allow switching to mocks @@ -160,14 +160,14 @@ func (c *APIClient) prepareRequest( body, err = setBody(postBody, contentType) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to set body: %w", err) } } // Setup path and query parameters url, err := url.Parse(path) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to parse path %s: %w", path, err) } // Override request host, if applicable @@ -183,7 +183,7 @@ func (c *APIClient) prepareRequest( // Generate a new request localVarRequest, err = http.NewRequest(http.MethodPost, url.String(), body) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to generate new request: %w", err) } // add header parameters, if any @@ -222,7 +222,7 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err if jsonCheck.MatchString(contentType) { if err = json.Unmarshal(b, v); err != nil { - return err + return fmt.Errorf("failed to unmarshal: %w", err) } return nil } @@ -253,8 +253,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s", contentType) - return nil, err + return nil, fmt.Errorf("invalid body type %s", contentType) } return bodyBuf, nil } diff --git a/templates/client/configuration.mustache b/templates/client/configuration.mustache index a985dd15e..5773083fd 100644 --- a/templates/client/configuration.mustache +++ b/templates/client/configuration.mustache @@ -139,8 +139,8 @@ func (c *Configuration) AddDefaultHeader(key string, value string) { // ServerUrl returns URL based on server settings func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { - if index < 0 || len(c.Servers) <= index { - return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1) + if index < 0 || index >= len(c.Servers) { + return "", fmt.Errorf("index %d out of range [0-%d]", index, len(c.Servers)-1) } server := c.Servers[index] url := server.Url @@ -155,7 +155,7 @@ func (c *Configuration) ServerUrl(index int, variables map[string]string) (strin } } if !found { - return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) + return "", fmt.Errorf("the variable %s in the server URL has invalid value %s. Must be %v", name, value, variable.EnumValues) } url = strings.ReplaceAll(url, "{"+name+"}", value) } else {