api-client-generation-tools
api-client-generation-tools copied to clipboard
ApiException generates NullReferenceException on error response
Hello!
When exceptionClass is set to "ApiException" and an error response (non 2xx) is received the actual exception received is NullReferenceException instead of the ApiException because the constructor fails:
public ApiException(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Exception innerException)
: base(message + "\n\nStatus: " + statusCode + "\nResponse: \n" + response.Substring(0, response.Length >= 512 ? 512 : response.Length), innerException)
{
StatusCode = statusCode;
Response = response;
Headers = headers;
}
The issue is with response.Substring
when response is null.
Related generated code:
if (status_ == "401")
{
var objectResponse_ = await ReadJsonObjectResponseAsync<Error>(response_, headers_).ConfigureAwait(false);
throw new ApiException<Error>("Unauthorized", (int)response_.StatusCode, objectResponse_.Text, headers_, objectResponse_.Object, null);
}
Custom tool = NswagCodeGenerator
Thanks.