diff --git a/Directory.Build.props b/Directory.Build.props index e15dc06..382acc3 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 0.10.0 + 0.11.0 9.0 enable true diff --git a/ReleaseNotes.md b/ReleaseNotes.md index a514717..b9bc789 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,5 +1,9 @@ # Release Notes +## 0.11.0 + +* Populate `AutoWebServiceResponse.ResponseContentPreview` when creating a `WebServiceException` from the response. + ## 0.10.0 * Support additional json content types (e.g., application/vnd.api+json). diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs index 80e12d5..9ca0a12 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs @@ -1,10 +1,8 @@ using System; using System.Globalization; using System.IO; -using System.Linq; using System.Net; using System.Net.Http; -using System.Reflection; using System.Threading; using System.Threading.Tasks; using Faithlife.Json; @@ -57,14 +55,13 @@ public AutoWebServiceRequest(Uri uri) protected override async Task HandleResponseCoreAsync(WebServiceResponseHandlerInfo info) { var response = CreateResponse()!; - Type responseType = response.GetType(); var webResponse = info.WebResponse!; // find specific status code property bool isStatusCodeHandled = false; HttpStatusCode statusCode = webResponse.StatusCode; string statusCodeText = statusCode.ToString(); - var resultProperty = GetProperty(responseType, statusCodeText); + var resultProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, statusCodeText); object? content = null; if (resultProperty?.CanWrite ?? false) @@ -75,7 +72,7 @@ protected override async Task HandleResponseCoreAsync(WebServiceResponseHa isStatusCodeHandled = true; } - var statusCodeProperty = GetProperty(responseType, "StatusCode"); + var statusCodeProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, "StatusCode"); if (statusCodeProperty?.CanWrite ?? false) { Type statusCodePropertyType = statusCodeProperty.PropertyType; @@ -101,7 +98,7 @@ protected override async Task HandleResponseCoreAsync(WebServiceResponseHa #pragma warning disable CA1307 // Specify StringComparison for clarity string propertyName = headerName.Replace("-", ""); #pragma warning restore CA1307 // Specify StringComparison for clarity - var headerProperty = GetProperty(responseType, propertyName); + var headerProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, propertyName); if (headerProperty?.CanWrite ?? false) { // get header text @@ -139,16 +136,6 @@ protected override async Task HandleResponseCoreAsync(WebServiceResponseHa return true; } - private static PropertyInfo? GetProperty(Type type, string propertyName) - { - var property = type.GetRuntimeProperties().FirstOrDefault(x => x.GetMethod is object && !x.GetMethod.IsStatic && string.Equals(x.Name, propertyName, StringComparison.OrdinalIgnoreCase)); - - if (property is object && property.SetMethod is null) - property = property.DeclaringType.GetRuntimeProperty(property.Name); - - return property; - } - private async Task ReadContentAsAsync(WebServiceResponseHandlerInfo info, Type propertyType) { var webResponse = info.WebResponse!; diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs index 15134d4..173c02b 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs @@ -2,6 +2,7 @@ using System.Net; using System.Net.Http.Headers; using System.Threading.Tasks; +using Faithlife.Json; namespace Faithlife.WebRequests.Json { @@ -16,6 +17,20 @@ public abstract class AutoWebServiceResponse /// A new exception for the response. public WebServiceException CreateException(string? message = null, Exception? innerException = null) { + if (m_responseContentPreview == null) + { + var statusCodeProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(this, m_responseStatusCode?.ToString()); + + if (statusCodeProperty?.CanRead == true) + { + const int maxPreviewContentLength = 1000; + m_responseContentPreview = JsonUtility.ToJson(statusCodeProperty.GetValue(this)); + + if (m_responseContentPreview.Length > maxPreviewContentLength) + m_responseContentPreview = m_responseContentPreview.Substring(0, maxPreviewContentLength) + "..."; + } + } + return new WebServiceException( message: message, requestMethod: m_requestMethod, diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs index 64c8333..1e48122 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Reflection; namespace Faithlife.WebRequests.Json { @@ -39,5 +41,27 @@ public static void VerifyResultIsExpected(this TResponse r { GetExpectedResult(response, getProperty); } + + /// + /// Returns PropertyInfo of the desired property. + /// + /// The response. + /// The name of the property. + /// The PropertyInfo of the property. + internal static PropertyInfo? GetStatusCodeProperty(object response, string? propertyName) + { + if (propertyName == null) + return null; + + var property = response.GetType().GetRuntimeProperties().FirstOrDefault(x => + x.GetMethod is object && + !x.GetMethod.IsStatic && + string.Equals(x.Name, propertyName, StringComparison.OrdinalIgnoreCase)); + + if (property is object && property.SetMethod is null) + property = property.DeclaringType.GetRuntimeProperty(property.Name); + + return property; + } } }