question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Requirement to model errors as "default" makes it hard to document error cases

See original GitHub issue

Currently, AutoRest expects that all responses that aren’t “default” in the Swagger spec are success cases. However, this makes it difficult to document error cases.

For example, in order to have AutoRest generate the correct code, I need to do this:

"responses": {
  "200": {
    "description": "Description of a successful response.",
    "schema": {
      "$ref": "#/definitions/AdminKeyResult"
    }
  },
  "default": {
    "description": "HTTP 404: The subscription, resource group, or Search service could not be found. HTTP 409: The specified subscription is disabled.",
    "schema": {
      "$ref": "#/definitions/CloudError"
    }
  }
}

But what I want to do is something like this:

"responses": {
  "200": {
    "description": "Description of a successful response.",
    "schema": {
      "$ref": "#/definitions/AdminKeyResult"
    }
  },
  "404": {
    "description": "The subscription, resource group, or Search service could not be found.",
    "schema": {
      "$ref": "#/definitions/CloudError"
    }
  },
  "409": {
    "description": "The specified subscription is disabled.",
    "schema": {
      "$ref": "#/definitions/CloudError"
    }
  },
  "default": {
    "description": "An unexpected error occurred.",
    "schema": {
      "$ref": "#/definitions/CloudError"
    }
  }
}

Where perhaps AutoRest can tell a response case is an error by the presence of an error contract (#/definitions/CloudError).

This is important because as Azure reference documentation moves to docs.microsoft.com, we are moving towards auto-generated REST API reference docs that are sourced from the same Swagger specs that we use to generate SDKs. These two processes/toolchains (docs via DocFX/docs.microsoft.com and SDK code via AutoRest) need to work well together.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
csteegzcommented, Nov 29, 2016

I have a pair of proposals/branches to address this - I believe they’re pretty close to being ready for a PR if either are wanted, but I’m sure there is a lot of discussion to be have about this - I could also open PRs if that is a better way to facilitate discussion.

These proposals are both predicated on two ideas: 1. There is a canonical error response model for a specific web service. 2. This canonical error response is specified by the default tag in the swagger.

This is in line with how autorest currently treats errors, although it does limit the behavior in regards to inheritance as Thomas discuses above. That being said, it is pretty extensible as it allows different error responses for different routes, and services.

Following these ideas is easier for implementation because we don’t have to change anything in actual code generation - we just have to modify the modeler to “ignore” certain responses (This does mean that the autorest modeler wouldn’t be able to be used to generate static docs). Anything that doesn’t follow both of these would require more work - I believe that the responses would need a way to indicate that they are errors and each code generator would need to be modified to treat those errors accordingly.

x-ms-treat-as-default

My first proposal is the simple and obvious ones - we add an extension field x-ms-treat-as-default to response that tells the modeler to ignore that response, which means that it gets treated as a response. This is simple, explicit in the swagger and newer versions of Autorest will generate the same code when run against older swagger versions. The disadvantage is that this requires extra work for the person who writes the swagger and feels unintuitive - I don’t know if this should be a vendor extension, it feels like core behavior.

Treat default type as error

My second proposal is basically just an implementation of what Bruce proposed above - if a response has the same schema as the default, it is treated as an error. To me, this feels like the way that the codegen should work. This has the obvious downside of limiting the models the user can use - but, since my implementation uses the name, the user could just create an identical non-error model.

I’d love for anyone to take a look at these two proposals, if either of them seem reasonable I can open a PR. I think both of these proposals address the constraints previously mentioned in this issue.

0reactions
fearthecowboycommented, Jul 18, 2017

Added to the vNext generator support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change the default error message in the model state binder ...
I need to display my custom error message instead of default MVC error model message, which is not displaying in this case. asp.net-core-mvc ......
Read more >
Understanding schema errors | HESA
What is a schema error? The schema describes the structure of the XML document (number of elements, whether an element can be empty,...
Read more >
Part 9, add validation to an ASP.NET Core MVC app
Add validation rules to the movie model · The Required and MinimumLength attributes indicate that a property must have a value; but nothing ......
Read more >
Error codes and descriptions - Technical Knowledge Base
Another approach to resolve this error is to uninstall previous versions of the software (including any evaluation versions that may have been ...
Read more >
SML/NJ Error Messages
This document contains lists of error and warning messages produced by the ... If a syntax error is found in a program, then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found