Skip to content

Commit

Permalink
Reduce Cognitive Complexity of GetResponseData method
Browse files Browse the repository at this point in the history
Trying to make Sonar happier :)
  • Loading branch information
twogood committed Oct 28, 2019
1 parent 02ad8db commit d678795
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions Activout.RestClient/Implementation/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,7 @@ private async Task<object> GetResponseData(HttpRequestMessage request, HttpRespo
var deserializer = _context.SerializationManager.GetDeserializer(contentTypeMediaType);
if (deserializer == null)
{
var restClientException = new RestClientException(request.RequestUri, response.StatusCode,
"No deserializer found for " + contentTypeMediaType);

if (response.IsSuccessStatusCode || !_context.UseDomainException)
{
throw restClientException;
}

throw await _domainExceptionMapper.CreateExceptionAsync(response, null, restClientException);
throw await CreateNoDeserializerFoundException(request, response, contentTypeMediaType);
}

try
Expand All @@ -348,15 +340,36 @@ private async Task<object> GetResponseData(HttpRequestMessage request, HttpRespo
throw;
}

var errorResponse = response.Content == null ? null : await response.Content.ReadAsStringAsync();
throw await CreateDeserializationException(request, response, e);
}
}

if (response.IsSuccessStatusCode || !_context.UseDomainException)
{
throw new RestClientException(request.RequestUri, response.StatusCode, errorResponse, e);
}
private async Task<Exception> CreateDeserializationException(HttpRequestMessage request,
HttpResponseMessage response, Exception e)
{
var errorResponse = response.Content == null ? null : await response.Content.ReadAsStringAsync();

throw await _domainExceptionMapper.CreateExceptionAsync(response, null, e);
if (response.IsSuccessStatusCode || !_context.UseDomainException)
{
return new RestClientException(request.RequestUri, response.StatusCode, errorResponse, e);
}

return await _domainExceptionMapper.CreateExceptionAsync(response, errorResponse, e);
}

private async Task<Exception> CreateNoDeserializerFoundException(HttpRequestMessage request,
HttpResponseMessage response,
string contentTypeMediaType)
{
var exception = (Exception) new RestClientException(request.RequestUri, response.StatusCode,
"No deserializer found for " + contentTypeMediaType);

if (response.IsSuccessStatusCode || !_context.UseDomainException)
{
return exception;
}

return await _domainExceptionMapper.CreateExceptionAsync(response, null, exception);
}
}
}

0 comments on commit d678795

Please sign in to comment.