Azure / autorest.powershell

AutoRest PowerShell Generator
MIT License
112 stars 80 forks source link

Supporting custom default errors #460

Open joshspicer opened 5 years ago

joshspicer commented 5 years ago

We are looking to support custom errors as supplied by the swagger file. Right now AutoREST is simply expecting a code and message at the top level of any error response, else it will ignore any other response and throw a generic error.

I'm setting the swagger default field, and would like to define a response shape that autorest can then parse and convert into a powershell error.

For example, here is the structure of an ODATA error response. I'm setting this as the default response schema in my swagger.

{
  "odata.error": {
    "code": "string",   
    "message": {
      "lang": "string",
      "value": "string"
    },
    "innererror": {
      "message": "string",
      "type": "string",
      "stacktrace": "string"   
    }
  }
}

At its current state, AutoREST will not be able to parse this error properly and will ignore it entirely.

Our idea to implement proper error handling is to allow specifying the location of the error code and error message in the Swagger itself (perhaps with the format option, as shown below).

definitions:
  default_error:
    type: object
    properties:
      odata.error:
        properties:
          code:
            type: string
             format: ERROR_CODE
          message:
            type: object
            properties:
              lang:
                type: string
              value:
                type: string
          innererror:
            type: object
            properties:
              message:
                type: string
                format: ERROR_MESSAGE
              type:
                type: string
              stacktrace:
                type: string

AutoREST can then parse whatever is specified as the "default" response, and can pick out the correct error data to present to the user.

We found that the code emitting methods onDefault in file extensions\powershell\cmdlet-class.ts (around line 500) can be adapted to behave this way.

cc: @kenal-microsoft @amareshbAtMicrosoft

markcowl commented 4 years ago

In general, we need to do a bit better with errors. We should have a structure similar to the .Net generator, where all errors have a code and message property, and the message becomes the exception message, when present. We should also have a way of representing custom error information, either as a property of our exception, or by creating a separate exception type.