Skip to content

Commit

Permalink
Gracefully handle errors for the DetailsAsync method.
Browse files Browse the repository at this point in the history
  • Loading branch information
PureKrome committed Jul 7, 2017
1 parent 2cc30ec commit a294999
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/SimpleGoogleWebServices/GooglePlacesApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,14 @@ public async Task<DetailsResult> DetailsAsync(string placeId)
if (!response.IsSuccessStatusCode)
{
var errorMessage =
$"Failed to retrieve a Google Places 'details' result. Status Code: {response.StatusCode}. Message: {content}";
throw new Exception(errorMessage);
$"Failed to retrieve a Google Places Details result. Status Code: {response.StatusCode}. Message: {content}";
return new DetailsResult
{
Status = response.StatusCode.ToString(),
ErrorMessage = errorMessage
};
}

// Get content from json into rich object model.
var detailsResponse = JsonConvert.DeserializeObject<DetailsResponse>(content);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Shouldly;
Expand Down Expand Up @@ -37,15 +38,18 @@ public async Task GivenAValidPlaceId_DetailsAsync_ReturnsAnOkResult(string filen
}

[Theory]
[InlineData("Invalid Request", "INVALID_REQUEST")]
[InlineData("Request Denied", "REQUEST_DENIED")]
public async Task GivenAnInvalidRequest_DetailsAsync_ReturnsAnErrorResult(string filename, string status)
[InlineData("Unknown Error", "InternalServerError", HttpStatusCode.InternalServerError)]
[InlineData("Invalid Request", "INVALID_REQUEST", HttpStatusCode.OK)]
[InlineData("Request Denied", "REQUEST_DENIED", HttpStatusCode.OK)]
public async Task GivenAnInvalidRequest_DetailsAsync_ReturnsAnErrorResult(string filename,
string status,
HttpStatusCode statusCode)
{
// Arrange.
var json = File.ReadAllText($"Sample Data\\Details\\{filename}.json");
var options = new HttpMessageOptions
{
HttpResponseMessage = FakeHttpMessageHandler.GetStringHttpResponseMessage(json)
HttpResponseMessage = FakeHttpMessageHandler.GetStringHttpResponseMessage(json, statusCode)
};
var httpClient = new HttpClient(new FakeHttpMessageHandler(options));
var service = new GooglePlacesApiService("aaa", httpClient);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"html_attributions": [],
"status": "UNKNOWN_ERROR"
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<None Update="Sample Data\Details\OK result - full address.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Sample Data\Details\Unknown Error.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Sample Data\Geocode\Error Result.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit a294999

Please sign in to comment.