Skip to content

Commit

Permalink
Adding response payload to error messages when exceptions are not def…
Browse files Browse the repository at this point in the history
…ined (#425)

* Adding response payload to error messages when exceptions are not defined in the swagger

* bug fix

* Updating error handling

* fixing happy line

* checking for body length not equal to zero in the response

* reading bytes update

* updating errorsgroup test

* Refactoring to have the generic error definition in one place

Co-authored-by: Catalina Peralta <[email protected]>
  • Loading branch information
catalinaperalta and cperaltah authored Jun 17, 2020
1 parent 91123b3 commit 72e3daa
Show file tree
Hide file tree
Showing 11 changed files with 630 additions and 79 deletions.
25 changes: 21 additions & 4 deletions src/generator/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (<SchemaResponse>exception).schema;
Expand Down
2 changes: 1 addition & 1 deletion test/autorest/errorsgroup/errorsgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
11 changes: 10 additions & 1 deletion test/autorest/generated/azurespecialsgroup/xmsclientrequestid.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion test/autorest/generated/complexgroup/flattencomplex.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion test/autorest/generated/errorsgroup/pet.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions test/autorest/generated/httpinfrastructuregroup/httpfailure.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 72e3daa

Please sign in to comment.