From d8b4e15554663cf2e0beaa42854632033ebc14e2 Mon Sep 17 00:00:00 2001 From: Jingfu Wang Date: Fri, 26 Aug 2022 19:35:16 -0400 Subject: [PATCH 1/4] feat: client error handling Signed-off-by: Jingfu Wang --- client/api_account.go | 36 +++++----- client/api_block.go | 36 +++++----- client/api_call.go | 18 ++--- client/api_construction.go | 144 ++++++++++++++++++------------------- client/api_events.go | 18 ++--- client/api_mempool.go | 36 +++++----- client/api_network.go | 54 +++++++------- client/api_search.go | 18 ++--- client/client.go | 21 +++--- client/configuration.go | 6 +- 10 files changed, 194 insertions(+), 193 deletions(-) diff --git a/client/api_account.go b/client/api_account.go index 567206dc..9a8ac479 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,7 @@ 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 +105,23 @@ 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 +173,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 +192,7 @@ 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 +200,23 @@ 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 04db9153..2df326a4 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,7 @@ 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 +102,23 @@ 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 +173,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 +192,7 @@ 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 +200,23 @@ 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 bbece7f3..5805d426 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,7 @@ 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 +105,23 @@ 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 cc5ef08a..c40067d0 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,7 @@ 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 +97,23 @@ 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 +156,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 +175,7 @@ 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 +183,23 @@ 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 +242,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 +261,7 @@ 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 +269,23 @@ 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 +335,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 +354,7 @@ 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 +362,23 @@ 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 +422,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 +441,7 @@ 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 +449,23 @@ 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 +514,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 +533,7 @@ 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 +541,23 @@ 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 +604,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 +623,7 @@ 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 +631,23 @@ 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 +693,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 +712,7 @@ 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 +720,23 @@ 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, repsonse 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 1d3e38e3..90545524 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,7 @@ 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 +100,23 @@ 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 727f708b..c4c7cc5e 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,7 @@ 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 +95,23 @@ 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 +159,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 +178,7 @@ 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 +186,23 @@ 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 8e644b57..b977f7a5 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,7 @@ 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 +95,23 @@ 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 +156,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 +175,7 @@ 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 +183,23 @@ 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 +242,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 +261,7 @@ 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 +269,23 @@ 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 cb07192e..52375fa5 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,7 @@ 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 +100,23 @@ 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 9ce83b26..7f840a59 100644 --- a/client/client.go +++ b/client/client.go @@ -30,6 +30,8 @@ import ( "reflect" "regexp" "strings" + + "github.com/coinbase/rosetta-sdk-go/types" ) var ( @@ -133,25 +135,25 @@ func (c *APIClient) callAPI(ctx context.Context, request *http.Request) (*http.R if c.cfg.Debug { dump, err := httputil.DumpRequestOut(request, true) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to dump request %s: %w", types.PrintStruct(request), err) } log.Printf("\n%s\n", string(dump)) } resp, err := c.cfg.HTTPClient.Do(request.WithContext(ctx)) if err != nil { - return resp, err + return resp, fmt.Errorf("failed to send request %s: %w", types.PrintStruct(request), err) } if c.cfg.Debug { dump, err := httputil.DumpResponse(resp, true) if err != nil { - return resp, err + return resp, fmt.Errorf("failed to dump response %s: %w", types.PrintStruct(resp), err) } log.Printf("\n%s\n", string(dump)) } - return resp, err + return resp, nil } // ChangeBasePath changes base path to allow switching to mocks @@ -185,14 +187,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 +210,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 +249,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 +280,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 9c4086eb..0d4d218d 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, From b3b4a1e8dc792beb407d1e2f038be5f792db4474 Mon Sep 17 00:00:00 2001 From: Jingfu Wang Date: Mon, 29 Aug 2022 11:49:08 -0400 Subject: [PATCH 2/4] fix: unit test Signed-off-by: Jingfu Wang --- client/client.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/client/client.go b/client/client.go index 7f840a59..b7979723 100644 --- a/client/client.go +++ b/client/client.go @@ -30,8 +30,6 @@ import ( "reflect" "regexp" "strings" - - "github.com/coinbase/rosetta-sdk-go/types" ) var ( @@ -135,20 +133,20 @@ func (c *APIClient) callAPI(ctx context.Context, request *http.Request) (*http.R if c.cfg.Debug { dump, err := httputil.DumpRequestOut(request, true) if err != nil { - return nil, fmt.Errorf("failed to dump request %s: %w", types.PrintStruct(request), err) + return nil, err } log.Printf("\n%s\n", string(dump)) } resp, err := c.cfg.HTTPClient.Do(request.WithContext(ctx)) if err != nil { - return resp, fmt.Errorf("failed to send request %s: %w", types.PrintStruct(request), err) + return resp, err } if c.cfg.Debug { dump, err := httputil.DumpResponse(resp, true) if err != nil { - return resp, fmt.Errorf("failed to dump response %s: %w", types.PrintStruct(resp), err) + return resp, err } log.Printf("\n%s\n", string(dump)) } From b3f963b05141d89d877b76a3694d14d41b2c3806 Mon Sep 17 00:00:00 2001 From: Jingfu Wang Date: Mon, 29 Aug 2022 16:20:58 -0400 Subject: [PATCH 3/4] fix: check gen Signed-off-by: Jingfu Wang --- client/api_account.go | 26 ++++-- client/api_block.go | 24 +++++- client/api_call.go | 12 ++- client/api_construction.go | 104 +++++++++++++++++++----- client/api_events.go | 12 ++- client/api_mempool.go | 24 +++++- client/api_network.go | 36 ++++++-- client/api_search.go | 12 ++- templates/client/api.mustache | 18 ++-- templates/client/client.mustache | 13 ++- templates/client/configuration.mustache | 6 +- 11 files changed, 223 insertions(+), 64 deletions(-) diff --git a/client/api_account.go b/client/api_account.go index 9a8ac479..d6e49a9a 100644 --- a/client/api_account.go +++ b/client/api_account.go @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +109,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +212,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -216,7 +232,7 @@ func (a *AccountAPIService) AccountCoins( ) default: return nil, nil, fmt.Errorf( - "invalid status code %d, response 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 2df326a4..d42f7a02 100644 --- a/client/api_block.go +++ b/client/api_block.go @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +106,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +212,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) diff --git a/client/api_call.go b/client/api_call.go index 5805d426..f8ae9968 100644 --- a/client/api_call.go +++ b/client/api_call.go @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +109,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) diff --git a/client/api_construction.go b/client/api_construction.go index c40067d0..05b1c0ff 100644 --- a/client/api_construction.go +++ b/client/api_construction.go @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +101,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -113,7 +121,7 @@ func (a *ConstructionAPIService) ConstructionCombine( ) default: return nil, nil, fmt.Errorf( - "invalid status code %d, response body: %s", + "invalid status code %d, response body %s", localVarHTTPResponse.StatusCode, string(localVarBody), ) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +195,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -199,7 +215,7 @@ func (a *ConstructionAPIService) ConstructionDerive( ) default: return nil, nil, fmt.Errorf( - "invalid status code %d, response body: %s", + "invalid status code %d, response body %s", localVarHTTPResponse.StatusCode, string(localVarBody), ) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +289,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -285,7 +309,7 @@ func (a *ConstructionAPIService) ConstructionHash( ) default: return nil, nil, fmt.Errorf( - "invalid status code %d, response body: %s", + "invalid status code %d, response body %s", localVarHTTPResponse.StatusCode, string(localVarBody), ) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +390,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +485,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +585,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +683,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +780,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -729,7 +793,7 @@ func (a *ConstructionAPIService) ConstructionSubmit( _nethttp.StatusGatewayTimeout, _nethttp.StatusRequestTimeout: return nil, nil, fmt.Errorf( - "status code %d, repsonse body %s: %w", + "status code %d, response body %s: %w", localVarHTTPResponse.StatusCode, string(localVarBody), ErrRetriable, diff --git a/client/api_events.go b/client/api_events.go index 90545524..086b7dbb 100644 --- a/client/api_events.go +++ b/client/api_events.go @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +104,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) diff --git a/client/api_mempool.go b/client/api_mempool.go index c4c7cc5e..b694c682 100644 --- a/client/api_mempool.go +++ b/client/api_mempool.go @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +99,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +198,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) diff --git a/client/api_network.go b/client/api_network.go index b977f7a5..a471b4aa 100644 --- a/client/api_network.go +++ b/client/api_network.go @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +99,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +195,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +289,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) diff --git a/client/api_search.go b/client/api_search.go index 52375fa5..a84d0068 100644 --- a/client/api_search.go +++ b/client/api_search.go @@ -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, fmt.Errorf("failed to decode when hit status code 200, response body %s: %w", string(localVarBody), 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,7 +104,11 @@ 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, fmt.Errorf("failed to decode when hit status code 500, response body %s: %w", string(localVarBody), 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("error %+v", v) diff --git a/templates/client/api.mustache b/templates/client/api.mustache index 7002bc1d..1532edf2 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 7caab62b..548a93bb 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 a985dd15..5773083f 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 { From 57a0031221fcd4ada56ccc69f871ee6f3595d66d Mon Sep 17 00:00:00 2001 From: Jingfu Wang Date: Mon, 29 Aug 2022 16:29:08 -0400 Subject: [PATCH 4/4] doc: update doc Signed-off-by: Jingfu Wang --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9940b2ee..a287283e 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 ```