Skip to content

Add a JSON Exception Handler for improved deserialization error responses inspired by ASP.NET Core Minimal API discussion #298

Description

@marcominerva

Summary

Implement a JSON Exception Handler similar to the functionality discussed in dotnet/aspnetcore#62202. The goal is to provide improved, concise, and clear error responses for JSON deserialization errors, inspired by the differences between Minimal API and controller-based error handling discussed in the referenced issue.

Motivation

Currently, deserialization errors can result in verbose or unclear feedback. Having a dedicated handler would simplify error responses and improve usability, making error details more actionable and developer-friendly.

Desired Solution

  • Develop a helper or middleware that provides clear and concise error handling for JSON deserialization issues.
  • Format and structure error responses to be as clear as the standard used by ASP.NET Core controllers, reducing verbosity, and focusing on actionable feedback.
  • Take inspiration from dotnet/aspnetcore#62202 for concrete examples and guidelines for error response formatting.

Example Usage (from TinyHelpers samples)

You can see in the samples how exceptions are currently handled (e.g., for a thrown exception):

app.MapPost("/api/exception", () =>
{
    throw new Exception("This is an exception", innerException: new HttpRequestException("This is an inner exception"));
});

This produces a structured problem+json response. The desired handler should provide a similar concise experience for JSON deserialization errors, for example when receiving invalid JSON on inputs.

Example structured response (adapted from the issue):

{
  "type": "https://tools.ietf.org/html/rfc9110#section-15.6.1",
  "title": "System.Text.Json.JsonException",
  "status": 400,
  "detail": "The JSON value could not be converted to System.String. Path: $.summary | LineNumber: 3 | BytePositionInLine: 17.",
  "instance": "/api/target-endpoint",
  "traceId": "..."
}

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions