api-client-generation-tools icon indicating copy to clipboard operation
api-client-generation-tools copied to clipboard

ApiException generates NullReferenceException on error response

Open Alexei000 opened this issue 5 years ago • 0 comments

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.

Alexei000 avatar Jun 26 '19 11:06 Alexei000