OData / OData/AspNetCoreOData

Thrown exceptions have incorrect error format

Open
#392 5 comments 0 reactions 1 assignee View on GitHub

@KenitoInc is already working on this.

Since Nov 30, 2021.

followup
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

When an exception is thrown inside an ODataController the response does not adhere to the OData v4 Error Response.

Example:

public class FooController : ODataController
{
  [HttpGet]
  [EnableQuery]
  public IActionResult Get()
  {
    throw new Exception("test");
    // or
    throw new ODataException("test");
  }
}

yields (content-type: text/plain):

Microsoft.OData.ODataException: test
   at ...omitted

HEADERS
=======
...omitted

I was expecting a JSON response with a format that is in spec.

{
  "error": {
    "code": "500",
    "message": "Test",
    "target": "query",
    "details": [...],
    "innererror": {
      "trace": [...],
      "context": {...}
    }
  }
}

There is some old documentation that talks about CreateErrorResponse. However that no longer works. And frankly I'd expect the ODataController to handle thrown exceptions so they have the correct/expected format.

Am I missing some configuration? Or is exception handling expected to be manually handled?


EDIT:

Turns out the text/plain response was from app.UseDeveloperExceptionPage();.
app.UseExceptionHandler("/Error"); will just return a HTML page.
If none of them are set the response is an empty (content-length: 0) 500 HTTP error.

None of these responses are to spec :)

May I suggest an app.UseDeveloperODataExceptionPage(); that if the endpoint requested is a registered OData resource then it returns html, json or xml based on the client Accept header,
This aligns with the built-in UseDeveloperExceptionPage.

As for app.UseExceptionHandler("/Error"). We still don't share detailed exception information publicly when the app runs in production. Thinking app.UseODataExceptionHandler() that again will return content based on the client Accept header.

JSON:

{
  "error": {
    "code": "500",
    "message": "Internal Server Error"
  }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.