autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Suggestion: Don't throw an exception if the http status code is 2xx

Open Spencerooni opened this issue 11 years ago • 3 comments

Currently this is the generated client code for every request:

if (!(statusCode == (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), "OK")))
{
    var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", statusCode));
    ex.Request = httpRequest;
    ex.Response = httpResponse;
    if (shouldTrace)
        {
            ServiceClientTracing.Error(invocationId, ex);
        }
        throw ex;
    }

This means if I return http status code "201 created" that an exception will be thrown, when all 2xx codes indicate success.

Should this be changed to anything from >= 300?

Spencerooni avatar Nov 11 '15 15:11 Spencerooni

You can control this by modifying the responses in your Swagger spec. For example:

    "responses": {
      "200": {
        "description": "",
        "schema": {
          "$ref": "#/definitions/DataSource"
        }
      },
      "201": {
        "description": "",
        "schema": {
          "$ref": "#/definitions/DataSource"
        }
      }

bitsforthought avatar Nov 11 '15 17:11 bitsforthought

Ah great didn't see this, cheers!

Spencerooni avatar Nov 11 '15 17:11 Spencerooni

I found a good article on doing this here: https://azure.microsoft.com/en-us/documentation/articles/app-service-api-dotnet-swashbuckle-customize/

Spencerooni avatar Nov 12 '15 09:11 Spencerooni