Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion ClientProxyBase/ClientProxyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,40 @@ protected ClientProxyBase(HttpClient httpClient)

#region Methods

protected virtual async Task<String> HandleResponse(HttpResponseMessage responseMessage,
CancellationToken cancellationToken)
{
String result = String.Empty;

// Read the content from the response
String content = await responseMessage.Content.ReadAsStringAsync();

// Check the response code
if (!responseMessage.IsSuccessStatusCode)
{
// throw a specific exception to inherited class
switch (responseMessage.StatusCode)
{
case HttpStatusCode.BadRequest:
throw new InvalidOperationException(content);
case HttpStatusCode.Unauthorized:
case HttpStatusCode.Forbidden:
throw new UnauthorizedAccessException(content);
case HttpStatusCode.NotFound:
throw new KeyNotFoundException(content);
case HttpStatusCode.InternalServerError:
throw new Exception("An internal error has occurred");
default:
throw new Exception($"An internal error has occurred ({responseMessage.StatusCode})");
}
}

// Set the result
result = content;

return result;
}

/// <summary>
/// Handles the response.
/// </summary>
Expand All @@ -53,7 +87,7 @@ protected ClientProxyBase(HttpClient httpClient)
/// <exception cref="Exception">An internal error has occurred
/// or
/// An internal error has occurred</exception>
protected virtual async Task<Result<String>> HandleResponse(HttpResponseMessage responseMessage,
protected virtual async Task<Result<String>> HandleResponseX(HttpResponseMessage responseMessage,
CancellationToken cancellationToken)
{
String result = String.Empty;
Expand Down
2 changes: 1 addition & 1 deletion Shared.Tests/ClientProxyBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public TestClient(HttpClient httpClient) : base(httpClient){
}

public async Task<Result<String>> Test(HttpResponseMessage responseMessage, CancellationToken cancellationToken){
return await this.HandleResponse(responseMessage, cancellationToken);
return await this.HandleResponseX(responseMessage, cancellationToken);
}
}
}