openapi-generator
openapi-generator copied to clipboard
[BUG] [csharp] DefaultExceptionFactory hides exceptions
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?
👍 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.
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.
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.
And there's no way to universally construct an *Api instance because they don't have a common ancestor with a new(Configuration)
constructor
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));
};
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.
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.