openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [csharp] DefaultExceptionFactory hides exceptions

Open chrbkr opened this issue 4 years ago • 5 comments

The openapi-generator creates this ExceptionFactory:

https://github.com/OpenAPITools/openapi-generator/blob/150e24dc553a8ea5230ffb938ed3e6020e972faa/samples/client/petstore/csharp/OpenAPIClientNetStandard/src/Org.OpenAPITools/Client/Configuration.cs#L48-L62

First of all, I'm not sure handling response codes as exceptions is good practice, but that could be debated.

However, what's much worse is that real exceptions are hidden - in case a timeout occurs, or the connection is closed etc., all I get is a null response instead of the real exception.

Am I missing something? How are other people handling this?

chrbkr avatar Feb 18 '21 01:02 chrbkr

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

auto-labeler[bot] avatar Feb 18 '21 01:02 auto-labeler[bot]

Found a similar issue with this, where an API returned a 401 auth error but the generated client hides the exception. Only found the real result by stepping through with the debugger.

mbrookson avatar Jun 09 '21 08:06 mbrookson

Exception handling in the csharp generator is simply broken. The only error type that is bubbled up are 400+ HTTP codes. Moreover, there's no way to override it except for setting a new exception handler manually on every *Api instance.

Eugeny avatar Jul 01 '21 15:07 Eugeny

And there's no way to universally construct an *Api instance because they don't have a common ancestor with a new(Configuration) constructor

Eugeny avatar Jul 01 '21 15:07 Eugeny

We received a null result when contacting our APIs due to this RestSharp issue. It was a real pain to debug.

To avoid receiving a Null result we decided to declare a custom ExceptionFactory to overcome this issue. I think it would be better to include something similar to the template to be able to manage errors unrelated to the API.

apiInstance.ExceptionFactory = (methodName, response) =>
            {
                var status = (int)response.StatusCode;
                if (status >= 400)
                {
                    return new ApiException(status,
                        string.Format("Error calling {0}: {1}", methodName, response.RawContent),
                        response.RawContent, response.Headers);
                }
                return new Exception(string.Format("Generic error: {0}", response.ErrorText));
            };

valmoz avatar Sep 19 '22 08:09 valmoz

Well I had a similar issue, where I was expecting a 400 Bad Request and that's what I wanted to check in Specflow, but the response was eaten by the ApiException. So now I override the ExceptionFactory, but this could be greatly improved I think.

kipusoep avatar Jul 10 '23 13:07 kipusoep

There seems to be a bugfix for netStandard as described in #15471, but not in general use. This fix creates an ApiException for status code 0. To me that seems a better solution for error handling, than to generate a general exception to all responses. This still hides non error related responses, but I don't think exceptions are the right place for those any way.

MichaelMay81 avatar Mar 01 '24 08:03 MichaelMay81