public void GetProvider_ReturnsNull_ForCustomProblemDetails() { // Arrange var providerFactory = new ProblemDetailsWrapperProviderFactory(); var instance = new CustomProblemDetails(); var context = new WrapperProviderContext(instance.GetType(), isSerialization: true); // Act var provider = providerFactory.GetProvider(context); // Assert Assert.Null(provider); }
public void Custom_Problem_Details_With_Extensions() { var value = new CustomProblemDetails { Prop1 = "one", Prop2 = 2, Extensions = { { "Prop3", "three" }, { "Prop4", 4 } } }; const string expected = "{\"Prop1\":\"one\",\"Prop2\":2,\"Prop3\":\"three\",\"Prop4\":4}"; Serialize(value).Should().Be(expected); }
public void Cannot_Fully_Deserialize_Custom_ProblemDetails_With_Extensions() { // given var problem = New.ProblemDetails.CustomProblemDetailsWithExtensions(); var json = JsonProblemDetailsConverter.Serialize(problem); // when var actual = JsonProblemDetailsConverter.Deserialize <CustomProblemDetails>(json); // then var expected = new CustomProblemDetails { IntField = problem.IntField, StringField = problem.StringField }; problem.CopyStandardFieldsTo(expected); actual.Should().BeEquivalentTo(expected); }
public void Can_Deserialize_ProblemDetails_With_Extensions_To_CustomProblemDetails() { // given var problem = New.ProblemDetails.ProblemDetailsWithExtensions(); var json = JsonProblemDetailsConverter.Serialize(problem); // when var actual = JsonProblemDetailsConverter.Deserialize <CustomProblemDetails>(json); // then var expected = new CustomProblemDetails { IntField = (int)problem.Extensions["intField"], StringField = (string)problem.Extensions["stringField"] }; problem.CopyStandardFieldsTo(expected); actual.Should().BeEquivalentTo(expected); }
private static async Task WriteResponse(HttpContext httpContext, bool includeDetails, IEnumerable <IExceptionHandler> exceptionHandlers) { // Try and retrieve the error from the ExceptionHandler middleware var exceptionDetails = httpContext.Features.Get <IExceptionHandlerFeature>(); var ex = exceptionDetails?.Error; // Should always exist, but best to be safe! if (ex != null) { CustomProblemDetails?problemDetails = null; if (exceptionHandlers != null) { foreach (var exceptionHandler in exceptionHandlers) { if ((problemDetails = exceptionHandler.Intercept(ex)) != null) { break; } } } // ProblemDetails has it's own content type httpContext.Response.ContentType = "application/problem+json"; if (problemDetails == null) { // Get the details to display, depending on whether we want to expose the raw exception var title = includeDetails ? "An error occured: " + ex.Message : "An error occured"; problemDetails = new CustomProblemDetails { Status = 500, Title = title }; } if (problemDetails.Detail == null) { problemDetails.Detail = includeDetails ? ex.ToString() : null; } httpContext.Response.StatusCode = problemDetails.Status ?? 500; // This is often very handy information for tracing the specific request var traceId = Activity.Current?.Id ?? httpContext?.TraceIdentifier; if (traceId != null) { problemDetails.Extensions["traceId"] = traceId; } //Serialize the problem details object to the Response as JSON (using System.Text.Json) if (httpContext != null) { var stream = httpContext.Response.Body; JsonSerializerOptions options = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true, IgnoreNullValues = true }; options.Converters.Add(new JsonStringEnumConverter()); await JsonSerializer.SerializeAsync(stream, problemDetails, options : options ); } } } }