Skip to content

Commit

Permalink
Merge pull request #139 from drhoroscope/#138
Browse files Browse the repository at this point in the history
Closes issue #138
  • Loading branch information
ThomasPe committed Sep 10, 2018
2 parents e9eb94a + ef68947 commit 1000bbe
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 10 deletions.
68 changes: 68 additions & 0 deletions WordPressPCL.Tests.Selfhosted/Exception_Unexpected_Tests.cs
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WordPressPCL.Models;

namespace WordPressPCL.Tests.Selfhosted
{
[TestClass]
public class Exception_Unexpected_Tests
{
private WordPressClient _badConnectionClient;

[TestInitialize]
public void Initialize()
{
_badConnectionClient = new WordPressClient("https://microsoft.com");
}

[TestMethod]
public async Task Tags_Get_UnexpectedException()
{
await CheckForUnexpectedException(async () => await _badConnectionClient.Tags.Get());
}

[TestMethod]
public async Task Tags_Post_UnexpectedException()
{
await CheckForUnexpectedException(async () => await _badConnectionClient.Tags.Update(new Tag()));
}

[TestMethod]
public async Task Tags_Delete_UnexpectedException()
{
await CheckForUnexpectedException(async () => await _badConnectionClient.Tags.Delete(1), HttpStatusCode.MethodNotAllowed);
}

private async Task CheckForUnexpectedException(Func<Task> task, HttpStatusCode expectedStatus = HttpStatusCode.NotFound)
{
bool exceptionCaught = false;

try
{
await task();
}
catch (Exception ex)
{
exceptionCaught = true;

if (!(ex is WPUnexpectedException typedException))
{
Assert.Fail($"Expected an exception of type WPUnexpectedException, but saw exception of type {ex.GetType()}");
}
else
{
Assert.AreEqual($"Server returned HTTP status {expectedStatus}", typedException.Message);
Assert.IsNotNull(typedException.Response, "No HTTP response has been returned");
Assert.AreEqual(expectedStatus, typedException.Response.StatusCode);
Assert.IsFalse(string.IsNullOrEmpty(typedException.ResponseBody), "Expected a response body but didn't see one");
}
}

Assert.IsTrue(exceptionCaught, "Exception was expected but none was seen");
}
}
}
37 changes: 37 additions & 0 deletions WordPressPCL/Models/Exceptions.cs
@@ -1,4 +1,5 @@
using System;
using System.Net.Http;

namespace WordPressPCL.Models
{
Expand Down Expand Up @@ -33,4 +34,40 @@ public class WPException : Exception
/// </summary>
public BadRequest RequestData { get; set; }
}

/// <summary>
/// An exception resulting from a malformed WP response from a
/// WP REST call, where the response is still a valid (but not
/// successful) HTTP response. For example, the response returned
/// from an endpoint that is not a WP REST endpoint
/// </summary>
public class WPUnexpectedException : Exception
{
/// <summary>
/// Construct a WPUnexpectedException from a response
/// </summary>
/// <param name="response">The raw response</param>
/// <param name="resonseBody">The response body, if any</param>
public WPUnexpectedException(HttpResponseMessage response, string resonseBody)
: base(FormatExceptionMessage(response))
{
Response = response;
ResponseBody = resonseBody;
}

/// <summary>
/// The response that triggered the error
/// </summary>
public HttpResponseMessage Response { get; set; }

/// <summary>
/// The response body (if any) that was returned with the error status
/// </summary>
public string ResponseBody { get; set; }

private static string FormatExceptionMessage(HttpResponseMessage response)
{
return $"Server returned HTTP status {response.StatusCode}";
}
}
}
31 changes: 21 additions & 10 deletions WordPressPCL/Utility/HttpHelper.cs
Expand Up @@ -15,7 +15,7 @@ namespace WordPressPCL.Utility
public class HttpHelper
{
private static HttpClient _httpClient = new HttpClient();
private string _WordpressURI;
private readonly string _WordpressURI;

/// <summary>
/// JSON Web Token
Expand Down Expand Up @@ -90,9 +90,7 @@ internal async Task<TClass> GetRequest<TClass>(string route, bool embed, bool is
}
else
{
Debug.WriteLine(responseString);
var badrequest = JsonConvert.DeserializeObject<BadRequest>(responseString);
throw new WPException(badrequest.Message, badrequest);
throw CreateUnexpectedResponseException(response, responseString);
}
}
catch (WPException) { throw; }
Expand Down Expand Up @@ -134,9 +132,7 @@ internal async Task<(TClass, HttpResponseMessage)> PostRequest<TClass>(string ro
}
else
{
Debug.WriteLine(responseString);
var badrequest = JsonConvert.DeserializeObject<BadRequest>(responseString);
throw new WPException(badrequest.Message, badrequest);
throw CreateUnexpectedResponseException(response, responseString);
}
}
catch (WPException) { throw; }
Expand Down Expand Up @@ -173,9 +169,7 @@ internal async Task<bool> DeleteRequest(string route, bool isAuthRequired = true
}
else
{
Debug.WriteLine(responseString);
var badrequest = JsonConvert.DeserializeObject<BadRequest>(responseString);
throw new WPException(badrequest.Message, badrequest);
throw CreateUnexpectedResponseException(response, responseString);
}
}
catch (WPException) { throw; }
Expand All @@ -185,5 +179,22 @@ internal async Task<bool> DeleteRequest(string route, bool isAuthRequired = true
throw;
}
}

private static Exception CreateUnexpectedResponseException(HttpResponseMessage response, string responseString)
{
Debug.WriteLine(responseString);

BadRequest badrequest = null;
try
{
badrequest = JsonConvert.DeserializeObject<BadRequest>(responseString);
}
catch (JsonReaderException)
{
// the response is not a well formed bad request
return new WPUnexpectedException(response, responseString);
}
return new WPException(badrequest.Message, badrequest);
}
}
}

0 comments on commit 1000bbe

Please sign in to comment.