diff --git a/src/generator/operations.ts b/src/generator/operations.ts index 3e4f3778f..f0fe456ef 100644 --- a/src/generator/operations.ts +++ b/src/generator/operations.ts @@ -753,18 +753,35 @@ function createProtocolErrHandler(client: string, op: Operation, imports: Import const name = info.protocolNaming.errorMethod; let text = `${comment(name, '// ')} handles the ${info.name} error response.\n`; text += `func (client *${client}) ${name}(resp *azcore.Response) error {\n`; + + // define a generic error for when there are no exceptions or no error schema + const generateGenericError = function () { + imports.add('errors'); + imports.add('io/ioutil'); + imports.add('fmt'); + return `body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) + `; + + } + // if the response doesn't define any error types return a generic error if (!op.exceptions) { - imports.add('errors'); - text += `\treturn errors.New(resp.Status)\n`; + text += generateGenericError(); text += '}\n\n'; return text; } + const generateUnmarshaller = function (exception: Response, prefix: string) { let unmarshaller = ''; if (exception.language.go!.genericError) { - imports.add('errors'); - unmarshaller += `${prefix}return errors.New(resp.Status)\n`; + unmarshaller += `${prefix}${generateGenericError()}`; return unmarshaller; } const schemaError = (exception).schema; diff --git a/test/autorest/errorsgroup/errorsgroup_test.go b/test/autorest/errorsgroup/errorsgroup_test.go index 5f5e77778..d54585488 100644 --- a/test/autorest/errorsgroup/errorsgroup_test.go +++ b/test/autorest/errorsgroup/errorsgroup_test.go @@ -185,7 +185,7 @@ func TestGetPetByIDError5(t *testing.T) { client := getClient(t) result, err := client.GetPetByID(context.Background(), "unknown") // default generic error (no schema) - helpers.DeepEqualOrFatal(t, err.Error(), "402 Payment Required") + helpers.DeepEqualOrFatal(t, err.Error(), "That's all folks!!") if result != nil { t.Fatal("expected nil result") } diff --git a/test/autorest/generated/azurespecialsgroup/xmsclientrequestid.go b/test/autorest/generated/azurespecialsgroup/xmsclientrequestid.go index 9b37cef83..1bb3e6a6d 100644 --- a/test/autorest/generated/azurespecialsgroup/xmsclientrequestid.go +++ b/test/autorest/generated/azurespecialsgroup/xmsclientrequestid.go @@ -8,7 +8,9 @@ package azurespecialsgroup import ( "context" "errors" + "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "io/ioutil" "net/http" ) @@ -63,7 +65,14 @@ func (client *xmsClientRequestIdoperations) getHandleResponse(resp *azcore.Respo // getHandleError handles the Get error response. func (client *xmsClientRequestIdoperations) getHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // ParamGet - Get method that overwrites x-ms-client-request header with value 9C4D50EE-2D56-4CD3-8152-34347DC9F2B0. diff --git a/test/autorest/generated/complexgroup/flattencomplex.go b/test/autorest/generated/complexgroup/flattencomplex.go index e4fb6e89e..8e3464688 100644 --- a/test/autorest/generated/complexgroup/flattencomplex.go +++ b/test/autorest/generated/complexgroup/flattencomplex.go @@ -8,7 +8,9 @@ package complexgroup import ( "context" "errors" + "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "io/ioutil" "net/http" ) @@ -60,5 +62,12 @@ func (client *flattencomplexOperations) getValidHandleResponse(resp *azcore.Resp // getValidHandleError handles the GetValid error response. func (client *flattencomplexOperations) getValidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } diff --git a/test/autorest/generated/errorsgroup/pet.go b/test/autorest/generated/errorsgroup/pet.go index d5e997961..a8eeff6ac 100644 --- a/test/autorest/generated/errorsgroup/pet.go +++ b/test/autorest/generated/errorsgroup/pet.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "io/ioutil" "net/http" "net/url" "strings" @@ -144,6 +145,13 @@ func (client *petOperations) getPetByIdHandleError(resp *azcore.Response) error } return fmt.Errorf("%v", err) default: - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } } diff --git a/test/autorest/generated/httpinfrastructuregroup/httpfailure.go b/test/autorest/generated/httpinfrastructuregroup/httpfailure.go index 61c72fc50..85ec2b927 100644 --- a/test/autorest/generated/httpinfrastructuregroup/httpfailure.go +++ b/test/autorest/generated/httpinfrastructuregroup/httpfailure.go @@ -8,7 +8,9 @@ package httpinfrastructuregroup import ( "context" "errors" + "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "io/ioutil" "net/http" ) @@ -112,7 +114,14 @@ func (client *httpFailureOperations) getNoModelEmptyHandleResponse(resp *azcore. // getNoModelEmptyHandleError handles the GetNoModelEmpty error response. func (client *httpFailureOperations) getNoModelEmptyHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetNoModelError - Get empty error form server @@ -154,5 +163,12 @@ func (client *httpFailureOperations) getNoModelErrorHandleResponse(resp *azcore. // getNoModelErrorHandleError handles the GetNoModelError error response. func (client *httpFailureOperations) getNoModelErrorHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } diff --git a/test/autorest/generated/httpinfrastructuregroup/multipleresponses.go b/test/autorest/generated/httpinfrastructuregroup/multipleresponses.go index 67dd54ef0..9f064d329 100644 --- a/test/autorest/generated/httpinfrastructuregroup/multipleresponses.go +++ b/test/autorest/generated/httpinfrastructuregroup/multipleresponses.go @@ -8,7 +8,9 @@ package httpinfrastructuregroup import ( "context" "errors" + "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "io/ioutil" "net/http" ) @@ -521,7 +523,14 @@ func (client *multipleResponsesOperations) get200ModelA200InvalidHandleResponse( // get200ModelA200InvalidHandleError handles the Get200ModelA200Invalid error response. func (client *multipleResponsesOperations) get200ModelA200InvalidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get200ModelA200None - Send a 200 response with no payload, when a payload is expected - client should return a null object of thde type for model A @@ -563,7 +572,14 @@ func (client *multipleResponsesOperations) get200ModelA200NoneHandleResponse(res // get200ModelA200NoneHandleError handles the Get200ModelA200None error response. func (client *multipleResponsesOperations) get200ModelA200NoneHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get200ModelA200Valid - Send a 200 response with payload {'statusCode': '200'} @@ -605,7 +621,14 @@ func (client *multipleResponsesOperations) get200ModelA200ValidHandleResponse(re // get200ModelA200ValidHandleError handles the Get200ModelA200Valid error response. func (client *multipleResponsesOperations) get200ModelA200ValidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get200ModelA201ModelC404ModelDDefaultError200Valid - Send a 200 response with valid payload: {'statusCode': '200'} @@ -867,7 +890,14 @@ func (client *multipleResponsesOperations) get200ModelA202ValidHandleResponse(re // get200ModelA202ValidHandleError handles the Get200ModelA202Valid error response. func (client *multipleResponsesOperations) get200ModelA202ValidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get200ModelA400Invalid - Send a 200 response with invalid payload {'statusCodeInvalid': '400'} @@ -909,7 +939,14 @@ func (client *multipleResponsesOperations) get200ModelA400InvalidHandleResponse( // get200ModelA400InvalidHandleError handles the Get200ModelA400Invalid error response. func (client *multipleResponsesOperations) get200ModelA400InvalidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get200ModelA400None - Send a 400 response with no payload client should treat as an http error with no error model @@ -951,7 +988,14 @@ func (client *multipleResponsesOperations) get200ModelA400NoneHandleResponse(res // get200ModelA400NoneHandleError handles the Get200ModelA400None error response. func (client *multipleResponsesOperations) get200ModelA400NoneHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get200ModelA400Valid - Send a 200 response with payload {'statusCode': '400'} @@ -993,7 +1037,14 @@ func (client *multipleResponsesOperations) get200ModelA400ValidHandleResponse(re // get200ModelA400ValidHandleError handles the Get200ModelA400Valid error response. func (client *multipleResponsesOperations) get200ModelA400ValidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get202None204NoneDefaultError202None - Send a 202 response with no payload @@ -1169,7 +1220,14 @@ func (client *multipleResponsesOperations) get202None204NoneDefaultNone202Invali // get202None204NoneDefaultNone202InvalidHandleError handles the Get202None204NoneDefaultNone202Invalid error response. func (client *multipleResponsesOperations) get202None204NoneDefaultNone202InvalidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get202None204NoneDefaultNone204None - Send a 204 response with no payload @@ -1210,7 +1268,14 @@ func (client *multipleResponsesOperations) get202None204NoneDefaultNone204NoneHa // get202None204NoneDefaultNone204NoneHandleError handles the Get202None204NoneDefaultNone204None error response. func (client *multipleResponsesOperations) get202None204NoneDefaultNone204NoneHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get202None204NoneDefaultNone400Invalid - Send a 400 response with an unexpected payload {'property': 'value'} @@ -1251,7 +1316,14 @@ func (client *multipleResponsesOperations) get202None204NoneDefaultNone400Invali // get202None204NoneDefaultNone400InvalidHandleError handles the Get202None204NoneDefaultNone400Invalid error response. func (client *multipleResponsesOperations) get202None204NoneDefaultNone400InvalidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // Get202None204NoneDefaultNone400None - Send a 400 response with no payload @@ -1292,7 +1364,14 @@ func (client *multipleResponsesOperations) get202None204NoneDefaultNone400NoneHa // get202None204NoneDefaultNone400NoneHandleError handles the Get202None204NoneDefaultNone400None error response. func (client *multipleResponsesOperations) get202None204NoneDefaultNone400NoneHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetDefaultModelA200None - Send a 200 response with no payload @@ -1334,7 +1413,14 @@ func (client *multipleResponsesOperations) getDefaultModelA200NoneHandleResponse // getDefaultModelA200NoneHandleError handles the GetDefaultModelA200None error response. func (client *multipleResponsesOperations) getDefaultModelA200NoneHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetDefaultModelA200Valid - Send a 200 response with valid payload: {'statusCode': '200'} @@ -1376,7 +1462,14 @@ func (client *multipleResponsesOperations) getDefaultModelA200ValidHandleRespons // getDefaultModelA200ValidHandleError handles the GetDefaultModelA200Valid error response. func (client *multipleResponsesOperations) getDefaultModelA200ValidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetDefaultModelA400None - Send a 400 response with no payload @@ -1507,7 +1600,14 @@ func (client *multipleResponsesOperations) getDefaultNone200InvalidHandleRespons // getDefaultNone200InvalidHandleError handles the GetDefaultNone200Invalid error response. func (client *multipleResponsesOperations) getDefaultNone200InvalidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetDefaultNone200None - Send a 200 response with no payload @@ -1548,7 +1648,14 @@ func (client *multipleResponsesOperations) getDefaultNone200NoneHandleResponse(r // getDefaultNone200NoneHandleError handles the GetDefaultNone200None error response. func (client *multipleResponsesOperations) getDefaultNone200NoneHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetDefaultNone400Invalid - Send a 400 response with valid payload: {'statusCode': '400'} @@ -1589,7 +1696,14 @@ func (client *multipleResponsesOperations) getDefaultNone400InvalidHandleRespons // getDefaultNone400InvalidHandleError handles the GetDefaultNone400Invalid error response. func (client *multipleResponsesOperations) getDefaultNone400InvalidHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetDefaultNone400None - Send a 400 response with no payload @@ -1630,5 +1744,12 @@ func (client *multipleResponsesOperations) getDefaultNone400NoneHandleResponse(r // getDefaultNone400NoneHandleError handles the GetDefaultNone400None error response. func (client *multipleResponsesOperations) getDefaultNone400NoneHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } diff --git a/test/autorest/generated/migroup/multipleinheritanceserviceclient.go b/test/autorest/generated/migroup/multipleinheritanceserviceclient.go index 19b4a5c66..9620a0909 100644 --- a/test/autorest/generated/migroup/multipleinheritanceserviceclient.go +++ b/test/autorest/generated/migroup/multipleinheritanceserviceclient.go @@ -8,7 +8,9 @@ package migroup import ( "context" "errors" + "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "io/ioutil" "net/http" ) @@ -310,7 +312,14 @@ func (client *multipleInheritanceServiceClientOperations) putCatHandleResponse(r // putCatHandleError handles the PutCat error response. func (client *multipleInheritanceServiceClientOperations) putCatHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutFeline - Put a feline who hisses and doesn't meow @@ -352,7 +361,14 @@ func (client *multipleInheritanceServiceClientOperations) putFelineHandleRespons // putFelineHandleError handles the PutFeline error response. func (client *multipleInheritanceServiceClientOperations) putFelineHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutHorse - Put a horse with name 'General' and isAShowHorse false @@ -394,7 +410,14 @@ func (client *multipleInheritanceServiceClientOperations) putHorseHandleResponse // putHorseHandleError handles the PutHorse error response. func (client *multipleInheritanceServiceClientOperations) putHorseHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutKitten - Put a kitten with name 'Kitty' where likesMilk and hisses is false, meows and eatsMiceYet is true @@ -436,7 +459,14 @@ func (client *multipleInheritanceServiceClientOperations) putKittenHandleRespons // putKittenHandleError handles the PutKitten error response. func (client *multipleInheritanceServiceClientOperations) putKittenHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutPet - Put a pet with name 'Butter' @@ -478,5 +508,12 @@ func (client *multipleInheritanceServiceClientOperations) putPetHandleResponse(r // putPetHandleError handles the PutPet error response. func (client *multipleInheritanceServiceClientOperations) putPetHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } diff --git a/test/autorest/generated/paginggroup/paging.go b/test/autorest/generated/paginggroup/paging.go index ee59f4f33..07b3b8765 100644 --- a/test/autorest/generated/paginggroup/paging.go +++ b/test/autorest/generated/paginggroup/paging.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "io/ioutil" "net/http" "net/url" "strconv" @@ -111,7 +112,14 @@ func (client *pagingOperations) getMultiplePagesHandleResponse(resp *azcore.Resp // getMultiplePagesHandleError handles the GetMultiplePages error response. func (client *pagingOperations) getMultiplePagesHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetMultiplePagesFailure - A paging operation that receives a 400 on the second call @@ -159,7 +167,14 @@ func (client *pagingOperations) getMultiplePagesFailureHandleResponse(resp *azco // getMultiplePagesFailureHandleError handles the GetMultiplePagesFailure error response. func (client *pagingOperations) getMultiplePagesFailureHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetMultiplePagesFailureURI - A paging operation that receives an invalid nextLink @@ -207,7 +222,14 @@ func (client *pagingOperations) getMultiplePagesFailureUriHandleResponse(resp *a // getMultiplePagesFailureUriHandleError handles the GetMultiplePagesFailureURI error response. func (client *pagingOperations) getMultiplePagesFailureUriHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetMultiplePagesFragmentNextLink - A paging operation that doesn't return a full URL, just a fragment @@ -252,7 +274,14 @@ func (client *pagingOperations) getMultiplePagesFragmentNextLinkHandleResponse(r // getMultiplePagesFragmentNextLinkHandleError handles the GetMultiplePagesFragmentNextLink error response. func (client *pagingOperations) getMultiplePagesFragmentNextLinkHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetMultiplePagesFragmentWithGroupingNextLink - A paging operation that doesn't return a full URL, just a fragment with parameters grouped @@ -297,7 +326,14 @@ func (client *pagingOperations) getMultiplePagesFragmentWithGroupingNextLinkHand // getMultiplePagesFragmentWithGroupingNextLinkHandleError handles the GetMultiplePagesFragmentWithGroupingNextLink error response. func (client *pagingOperations) getMultiplePagesFragmentWithGroupingNextLinkHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetMultiplePagesLro - A long-running paging operation that includes a nextLink that has 10 pages @@ -336,7 +372,14 @@ func (client *pagingOperations) getMultiplePagesLroHandleResponse(resp *azcore.R // getMultiplePagesLroHandleError handles the GetMultiplePagesLro error response. func (client *pagingOperations) getMultiplePagesLroHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetMultiplePagesRetryFirst - A paging operation that fails on the first call with 500 and then retries and then get a response including a nextLink that has 10 pages @@ -384,7 +427,14 @@ func (client *pagingOperations) getMultiplePagesRetryFirstHandleResponse(resp *a // getMultiplePagesRetryFirstHandleError handles the GetMultiplePagesRetryFirst error response. func (client *pagingOperations) getMultiplePagesRetryFirstHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetMultiplePagesRetrySecond - A paging operation that includes a nextLink that has 10 pages, of which the 2nd call fails first with 500. The client should retry and finish all 10 pages eventually. @@ -432,7 +482,14 @@ func (client *pagingOperations) getMultiplePagesRetrySecondHandleResponse(resp * // getMultiplePagesRetrySecondHandleError handles the GetMultiplePagesRetrySecond error response. func (client *pagingOperations) getMultiplePagesRetrySecondHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetMultiplePagesWithOffset - A paging operation that includes a nextLink that has 10 pages @@ -490,7 +547,14 @@ func (client *pagingOperations) getMultiplePagesWithOffsetHandleResponse(resp *a // getMultiplePagesWithOffsetHandleError handles the GetMultiplePagesWithOffset error response. func (client *pagingOperations) getMultiplePagesWithOffsetHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetNoItemNamePages - A paging operation that must return result of the default 'value' node. @@ -538,7 +602,14 @@ func (client *pagingOperations) getNoItemNamePagesHandleResponse(resp *azcore.Re // getNoItemNamePagesHandleError handles the GetNoItemNamePages error response. func (client *pagingOperations) getNoItemNamePagesHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetNullNextLinkNamePages - A paging operation that must ignore any kind of nextLink, and stop after page 1. @@ -580,7 +651,14 @@ func (client *pagingOperations) getNullNextLinkNamePagesHandleResponse(resp *azc // getNullNextLinkNamePagesHandleError handles the GetNullNextLinkNamePages error response. func (client *pagingOperations) getNullNextLinkNamePagesHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetOdataMultiplePages - A paging operation that includes a nextLink in odata format that has 10 pages @@ -637,7 +715,14 @@ func (client *pagingOperations) getOdataMultiplePagesHandleResponse(resp *azcore // getOdataMultiplePagesHandleError handles the GetOdataMultiplePages error response. func (client *pagingOperations) getOdataMultiplePagesHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetPagingModelWithItemNameWithXmsClientName - A paging operation that returns a paging model whose item name is is overriden by x-ms-client-name 'indexes'. @@ -685,7 +770,14 @@ func (client *pagingOperations) getPagingModelWithItemNameWithXmsClientNameHandl // getPagingModelWithItemNameWithXmsClientNameHandleError handles the GetPagingModelWithItemNameWithXmsClientName error response. func (client *pagingOperations) getPagingModelWithItemNameWithXmsClientNameHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetSinglePages - A paging operation that finishes on the first call without a nextlink @@ -733,7 +825,14 @@ func (client *pagingOperations) getSinglePagesHandleResponse(resp *azcore.Respon // getSinglePagesHandleError handles the GetSinglePages error response. func (client *pagingOperations) getSinglePagesHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetSinglePagesFailure - A paging operation that receives a 400 on the first call @@ -781,7 +880,14 @@ func (client *pagingOperations) getSinglePagesFailureHandleResponse(resp *azcore // getSinglePagesFailureHandleError handles the GetSinglePagesFailure error response. func (client *pagingOperations) getSinglePagesFailureHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetWithQueryParams - A paging operation that includes a next operation. It has a different query parameter from it's next operation nextOperationWithQueryParams. Returns a ProductResult @@ -826,7 +932,14 @@ func (client *pagingOperations) getWithQueryParamsHandleResponse(resp *azcore.Re // getWithQueryParamsHandleError handles the GetWithQueryParams error response. func (client *pagingOperations) getWithQueryParamsHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // nextFragmentCreateRequest creates the NextFragment request. @@ -856,7 +969,14 @@ func (client *pagingOperations) nextFragmentHandleResponse(resp *azcore.Response // nextFragmentHandleError handles the NextFragment error response. func (client *pagingOperations) nextFragmentHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // nextFragmentWithGroupingCreateRequest creates the NextFragmentWithGrouping request. @@ -886,7 +1006,14 @@ func (client *pagingOperations) nextFragmentWithGroupingHandleResponse(resp *azc // nextFragmentWithGroupingHandleError handles the NextFragmentWithGrouping error response. func (client *pagingOperations) nextFragmentWithGroupingHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // nextOperationWithQueryParamsCreateRequest creates the NextOperationWithQueryParams request. @@ -914,5 +1041,12 @@ func (client *pagingOperations) nextOperationWithQueryParamsHandleResponse(resp // nextOperationWithQueryParamsHandleError handles the NextOperationWithQueryParams error response. func (client *pagingOperations) nextOperationWithQueryParamsHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } diff --git a/test/autorest/generated/validationgroup/autorestvalidationtest.go b/test/autorest/generated/validationgroup/autorestvalidationtest.go index 56f278727..5fbd1f5ce 100644 --- a/test/autorest/generated/validationgroup/autorestvalidationtest.go +++ b/test/autorest/generated/validationgroup/autorestvalidationtest.go @@ -8,7 +8,9 @@ package validationgroup import ( "context" "errors" + "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "io/ioutil" "net/http" "net/url" "strconv" @@ -69,7 +71,14 @@ func (client *autoRestValidationTestOperations) getWithConstantInPathHandleRespo // getWithConstantInPathHandleError handles the GetWithConstantInPath error response. func (client *autoRestValidationTestOperations) getWithConstantInPathHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } func (client *autoRestValidationTestOperations) PostWithConstantInBody(ctx context.Context, autoRestValidationTestPostWithConstantInBodyOptions *AutoRestValidationTestPostWithConstantInBodyOptions) (*ProductResponse, error) { @@ -114,7 +123,14 @@ func (client *autoRestValidationTestOperations) postWithConstantInBodyHandleResp // postWithConstantInBodyHandleError handles the PostWithConstantInBody error response. func (client *autoRestValidationTestOperations) postWithConstantInBodyHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // ValidationOfBody - Validates body parameters on the method. See swagger for details. diff --git a/test/autorest/generated/xmlgroup/xml.go b/test/autorest/generated/xmlgroup/xml.go index ba0783f66..f092fd27f 100644 --- a/test/autorest/generated/xmlgroup/xml.go +++ b/test/autorest/generated/xmlgroup/xml.go @@ -9,7 +9,9 @@ import ( "context" "encoding/xml" "errors" + "fmt" "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "io/ioutil" "net/http" ) @@ -123,7 +125,14 @@ func (client *xmlOperations) getAcLsHandleResponse(resp *azcore.Response) (*Sign // getAcLsHandleError handles the GetACLs error response. func (client *xmlOperations) getAcLsHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetComplexTypeRefNoMeta - Get a complex type that has a ref to a complex type with no XML node @@ -165,7 +174,14 @@ func (client *xmlOperations) getComplexTypeRefNoMetaHandleResponse(resp *azcore. // getComplexTypeRefNoMetaHandleError handles the GetComplexTypeRefNoMeta error response. func (client *xmlOperations) getComplexTypeRefNoMetaHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetComplexTypeRefWithMeta - Get a complex type that has a ref to a complex type with XML node @@ -207,7 +223,14 @@ func (client *xmlOperations) getComplexTypeRefWithMetaHandleResponse(resp *azcor // getComplexTypeRefWithMetaHandleError handles the GetComplexTypeRefWithMeta error response. func (client *xmlOperations) getComplexTypeRefWithMetaHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetEmptyChildElement - Gets an XML document with an empty child element. @@ -249,7 +272,14 @@ func (client *xmlOperations) getEmptyChildElementHandleResponse(resp *azcore.Res // getEmptyChildElementHandleError handles the GetEmptyChildElement error response. func (client *xmlOperations) getEmptyChildElementHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetEmptyList - Get an empty list. @@ -291,7 +321,14 @@ func (client *xmlOperations) getEmptyListHandleResponse(resp *azcore.Response) ( // getEmptyListHandleError handles the GetEmptyList error response. func (client *xmlOperations) getEmptyListHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetEmptyRootList - Gets an empty list as the root element. @@ -333,7 +370,14 @@ func (client *xmlOperations) getEmptyRootListHandleResponse(resp *azcore.Respons // getEmptyRootListHandleError handles the GetEmptyRootList error response. func (client *xmlOperations) getEmptyRootListHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetEmptyWrappedLists - Gets some empty wrapped lists. @@ -375,7 +419,14 @@ func (client *xmlOperations) getEmptyWrappedListsHandleResponse(resp *azcore.Res // getEmptyWrappedListsHandleError handles the GetEmptyWrappedLists error response. func (client *xmlOperations) getEmptyWrappedListsHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetHeaders - Get strongly-typed response headers. @@ -420,7 +471,14 @@ func (client *xmlOperations) getHeadersHandleResponse(resp *azcore.Response) (*X // getHeadersHandleError handles the GetHeaders error response. func (client *xmlOperations) getHeadersHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetRootList - Gets a list as the root element. @@ -462,7 +520,14 @@ func (client *xmlOperations) getRootListHandleResponse(resp *azcore.Response) (* // getRootListHandleError handles the GetRootList error response. func (client *xmlOperations) getRootListHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetRootListSingleItem - Gets a list with a single item. @@ -504,7 +569,14 @@ func (client *xmlOperations) getRootListSingleItemHandleResponse(resp *azcore.Re // getRootListSingleItemHandleError handles the GetRootListSingleItem error response. func (client *xmlOperations) getRootListSingleItemHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetServiceProperties - Gets storage service properties. @@ -550,7 +622,14 @@ func (client *xmlOperations) getServicePropertiesHandleResponse(resp *azcore.Res // getServicePropertiesHandleError handles the GetServiceProperties error response. func (client *xmlOperations) getServicePropertiesHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // GetSimple - Get a simple XML document @@ -638,7 +717,14 @@ func (client *xmlOperations) getWrappedListsHandleResponse(resp *azcore.Response // getWrappedListsHandleError handles the GetWrappedLists error response. func (client *xmlOperations) getWrappedListsHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // JSONInput - A Swagger with XML that has one operation that takes JSON as input. You need to send the ID number 42 @@ -679,7 +765,14 @@ func (client *xmlOperations) jsonInputHandleResponse(resp *azcore.Response) (*ht // jsonInputHandleError handles the JSONInput error response. func (client *xmlOperations) jsonInputHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // JSONOutput - A Swagger with XML that has one operation that returns JSON. ID number 42 @@ -721,7 +814,14 @@ func (client *xmlOperations) jsonOutputHandleResponse(resp *azcore.Response) (*J // jsonOutputHandleError handles the JSONOutput error response. func (client *xmlOperations) jsonOutputHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // ListBlobs - Lists blobs in a storage container. @@ -767,7 +867,14 @@ func (client *xmlOperations) listBlobsHandleResponse(resp *azcore.Response) (*Li // listBlobsHandleError handles the ListBlobs error response. func (client *xmlOperations) listBlobsHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // ListContainers - Lists containers in a storage account. @@ -812,7 +919,14 @@ func (client *xmlOperations) listContainersHandleResponse(resp *azcore.Response) // listContainersHandleError handles the ListContainers error response. func (client *xmlOperations) listContainersHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutACLs - Puts storage ACLs for a container. @@ -861,7 +975,14 @@ func (client *xmlOperations) putAcLsHandleResponse(resp *azcore.Response) (*http // putAcLsHandleError handles the PutACLs error response. func (client *xmlOperations) putAcLsHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutComplexTypeRefNoMeta - Puts a complex type that has a ref to a complex type with no XML node @@ -902,7 +1023,14 @@ func (client *xmlOperations) putComplexTypeRefNoMetaHandleResponse(resp *azcore. // putComplexTypeRefNoMetaHandleError handles the PutComplexTypeRefNoMeta error response. func (client *xmlOperations) putComplexTypeRefNoMetaHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutComplexTypeRefWithMeta - Puts a complex type that has a ref to a complex type with XML node @@ -943,7 +1071,14 @@ func (client *xmlOperations) putComplexTypeRefWithMetaHandleResponse(resp *azcor // putComplexTypeRefWithMetaHandleError handles the PutComplexTypeRefWithMeta error response. func (client *xmlOperations) putComplexTypeRefWithMetaHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutEmptyChildElement - Puts a value with an empty child element. @@ -984,7 +1119,14 @@ func (client *xmlOperations) putEmptyChildElementHandleResponse(resp *azcore.Res // putEmptyChildElementHandleError handles the PutEmptyChildElement error response. func (client *xmlOperations) putEmptyChildElementHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutEmptyList - Puts an empty list. @@ -1025,7 +1167,14 @@ func (client *xmlOperations) putEmptyListHandleResponse(resp *azcore.Response) ( // putEmptyListHandleError handles the PutEmptyList error response. func (client *xmlOperations) putEmptyListHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutEmptyRootList - Puts an empty list as the root element. @@ -1070,7 +1219,14 @@ func (client *xmlOperations) putEmptyRootListHandleResponse(resp *azcore.Respons // putEmptyRootListHandleError handles the PutEmptyRootList error response. func (client *xmlOperations) putEmptyRootListHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutEmptyWrappedLists - Puts some empty wrapped lists. @@ -1111,7 +1267,14 @@ func (client *xmlOperations) putEmptyWrappedListsHandleResponse(resp *azcore.Res // putEmptyWrappedListsHandleError handles the PutEmptyWrappedLists error response. func (client *xmlOperations) putEmptyWrappedListsHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutRootList - Puts a list as the root element. @@ -1156,7 +1319,14 @@ func (client *xmlOperations) putRootListHandleResponse(resp *azcore.Response) (* // putRootListHandleError handles the PutRootList error response. func (client *xmlOperations) putRootListHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutRootListSingleItem - Puts a list with a single item. @@ -1201,7 +1371,14 @@ func (client *xmlOperations) putRootListSingleItemHandleResponse(resp *azcore.Re // putRootListSingleItemHandleError handles the PutRootListSingleItem error response. func (client *xmlOperations) putRootListSingleItemHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutServiceProperties - Puts storage service properties. @@ -1246,7 +1423,14 @@ func (client *xmlOperations) putServicePropertiesHandleResponse(resp *azcore.Res // putServicePropertiesHandleError handles the PutServiceProperties error response. func (client *xmlOperations) putServicePropertiesHandleError(resp *azcore.Response) error { - return errors.New(resp.Status) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("%s; failed to read response body: %w", resp.Status, err) + } + if len(body) == 0 { + return errors.New(resp.Status) + } + return errors.New(string(body)) } // PutSimple - Put a simple XML document