Developer Exception Page Middleware
Detailed information about unhandled exceptions for both sync and async. By default enabled when in DEV. Never do in PROD. Contains stack trace query parameters of the request cookies headers endpoint meta data. For complete information we need to use logger. It is formatted based on accept header of request- content Negotiation
HTML page returned as plain text instead for example
Exception Handler Middleware. How to configure with route
Configure it if in PROD. Pass a route in it. Make a controller action for that route to return problem() problemdetails. It sends RFC compliant payload to client.
Why isn’t my request reaching the error endpoint
Don’t make the action with any http verb. Explicit verb will not let some request go to it.
Put [AllowAnonymous] to allow unauthenticated users
I don’t want the error route to come in docs
Use [ApiExploreSettings(IgnoreApi=true)] to not make open api to doc it.
Difference between developer exceptions and exception handler
One meant for development to provide full stack trace and additional data for developers to debug the issue. But this data is sensitive and shouldn’t be shown in production of security risk and bad ux. In prod use exception handler to only provide necessary client friendly erro information and hide sentive data like stack trace. Both can follow content Negotiation and catch unhandled exceptions only
What is error result
Any result with code 400 and above
Do we have to configure problem details
Error status codes that is above 400 is wrapped in problem details by default
What is problem details
Report errors in RFC compliant format. By default it is a json
What is statuscodepages
Any 4xx error is converted into problem details as Response
Does exception handler give out problem details in every unhandled exception
No. If the body has not been written, only then.
How can we customise problemdetails
In services.AddProblemDetails options customise problem details
Use httpcontext features and get the type of feature and edit the context.problemdetails
How to handle validation failure erro response
The response type is validation problemdetails when model validation fails which is automatically done
How to give more context to the exceptions of action
Create a custom exception classes called httpResponseException inheriting exception with status code and object to hold exception object which is Nullable
Then create a action filter called httpresponseexceptionfilter and leave the action executing but implement action executed. There check if the exception in action context is of type of custom class then create result as objectresult with custom class and give status code. Set the exception handled as true. We can also make it ordered filter with max value to make it run at end of pipeline.
Then register it globally in services.addcontrollers options.filter.add