Skip to content

Provide in-the-box handling for page vs. API errors #2573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aspnet-hello opened this issue Jan 2, 2018 · 5 comments
Closed

Provide in-the-box handling for page vs. API errors #2573

aspnet-hello opened this issue Jan 2, 2018 · 5 comments
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-diagnostics Diagnostic middleware and pages (except EF diagnostics) Needs: Design This issue requires design work before implementating.
Milestone

Comments

@aspnet-hello
Copy link

From @breyed on Saturday, December 23, 2017 8:47:37 AM

Currently, a fair amount of configuration is required to report errors in a native format for pages vs. web API. It would be nice to have the following features:

  • A way to tell MVC what routes go to pages vs. APIs.
  • Automatic response of HTML error pages for page routes, and empty-or-JSON bodies for API routes.
  • Inclusion of status codes from exceptions in API responses (avoids the need to always use IActionResult, which minimizes friction in unit testing web API methods).
  • Automatic suppression of authentication redirects for API routes.

Currently, I provide these features like this (and like this for redirects, but it seems like something that would be quite useful to have baked in from the start.

Copied from original issue: aspnet/Diagnostics#427

@aspnet-hello aspnet-hello added this to the 2.1.0-preview2 milestone Jan 2, 2018
@aspnet-hello aspnet-hello added Needs: Design This issue requires design work before implementating. repo:Diagnostics labels Jan 2, 2018
@aspnet-hello
Copy link
Author

From @Tratcher on Saturday, December 23, 2017 10:03:05 AM

Content-negotiation could be used rather than routing to determine the acceptable output formatting for errors.

The HTML vs JSON request is related to aspnet/Diagnostics#346, but it sounds like you want something for production rather than development.

Mapping exceptions to status codes is precarious. The error handling should only be used for un-expected failures which makes any built-in mapping futile. Expected errors with specific response should be handled in the APIs themselves. You may be able to at least share the logic for handling specific types of errors across APIs using MVC filters. Did you have something more specific in mind?

Auth is a very different feature set from diagnostics. We're working on the redirect problem over in the Security repo with the new virtual handlers. Let's not discuss auth on this thread.

@aspnet-hello
Copy link
Author

From @breyed on Saturday, December 23, 2017 2:07:58 PM

Great points. Content-negotiation is better than path for determining the format. If the MVC formatting provided a way to not only auto-format as JSON and XML, but also as HTML, it would become trivial to write an Error controller (or Razor Page) that handled all formats needed for production error reporting.

Regarding exceptions, they can be just as deterministic as NotFound(), Forbid(), etc. Those methods in ControllerBase rely on clearly documented semantics for the status code. An exception that takes a status code in its constructor can do likewise. It would provide an alternative control flow with identical result.

The nice thing about an exception-based flow is that tests for success scenarios don't need the Assert.IsType checks of the recommended pattern, because the controller method returns the data object directly. For cases where success tests highly outnumber failure tests, this is a nice friction reducer in writing the tests.

@aspnet-hello
Copy link
Author

From @Tratcher on Wednesday, December 27, 2017 4:15:44 AM

@rynowak about auto formatting HTML vs XML/JSON.

Exception based flow control is strongly discouraged and we do not design for it here. These diagnostic components are built to provide a minimal fallback experience in the event of an unexpected failure.

@aspnet-hello
Copy link
Author

From @rynowak on Wednesday, December 27, 2017 3:02:39 PM

These diagnostic components are built to provide a minimal fallback experience in the event of an unexpected failure.

We've had repeated requests for an experience exactly like this using JSON as the output. We haven't really dug into exactly what we'd do, but this is coming from a real place.

@mkArtakMSFT
Copy link
Member

Hi. Thanks for contacting us.
We're closing this issue as there was not much community interest in this ask for quite a while now.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
@amcasey amcasey added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-diagnostics Diagnostic middleware and pages (except EF diagnostics) Needs: Design This issue requires design work before implementating.
Projects
None yet
Development

No branches or pull requests

5 participants