Thrown exceptions have incorrect error format
@KenitoInc is already working on this.
Since Nov 30, 2021.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.