0

Problem

I am developing web API Application in asp.net core with ef core and using repository pattern. all the API is consumes by mobile developer as well as web developer.

I need to make a generic Exception handling in a way that all error I catch on server side is send my custom error code like application codes, server codes, and database codes for error and send it to the consumer in such a way that it is easy to incorporate these code and error to display user friendly code to users.

and also in a case where undetermined error occur is code and it is also handle through that mechanism and suggestion or implementation or documentation available kindly share. I already go through the internet for this problem but can't find the best generic way to handle it.

Community
  • 1
  • 1
Malik Kashmiri
  • 5,741
  • 11
  • 44
  • 80
  • Take a look at the [`DeveloperExceptionPageMiddleware`](https://github.com/aspnet/Diagnostics/blob/dev/src/Microsoft.AspNetCore.Diagnostics/DeveloperExceptionPage/DeveloperExceptionPageMiddleware.cs) which is the middleware that’s used when you run `UseDeveloperExceptionPage()`. Just build a middleware yourself that does what you want, e.g. redirects to a generic error message while providing just a little bit of information. – poke Aug 17 '17 at 07:31
  • See also [the docs](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling#configuring-a-custom-exception-handling-page) for `UseExceptionHandler()` – actually, use that instead of writing your own middleware. You can [pass a handler to the function](https://learn.microsoft.com/en-us/aspnet/core/api/microsoft.aspnetcore.builder.exceptionhandlerextensions#Microsoft_AspNetCore_Builder_ExceptionHandlerExtensions_UseExceptionHandler_IApplicationBuilder_System_Action_IApplicationBuilder__). – poke Aug 17 '17 at 07:33
  • 2
    Possible duplicate of [Error handling in ASP.NET Core 1.0 Web API (Sending ex.Message to the client)](https://stackoverflow.com/questions/38014379/error-handling-in-asp-net-core-1-0-web-api-sending-ex-message-to-the-client) – Set Aug 17 '17 at 07:39

0 Answers0