Skip to content
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

Feature: Catch all database exceptions #1247

Closed
hocinehacherouf opened this issue Sep 16, 2022 · 3 comments · Fixed by #1520
Closed

Feature: Catch all database exceptions #1247

hocinehacherouf opened this issue Sep 16, 2022 · 3 comments · Fixed by #1520
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@hocinehacherouf
Copy link
Contributor

Describe the solution you'd like
All database exceptions must be translated to InternalServerErrorException

@kbeaugrand
Copy link
Member

kbeaugrand commented Nov 1, 2022

@hocinehacherouf , what about catching all unhandled exceptions in the ASP.NET core and translate it to InternalErrorException ?
This will help to reduce exception handling in our business code and ensure that exception are well catched at the end..

-- EDIT
This might be done by using dedicated middleware in ASP.NET Core . See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-6.0

@kbeaugrand
Copy link
Member

Discussed today:

  • Use a middleware to catch data base exceptions (
  • Treat database exceptions and manage it to provide a more user friendly message to the user.

See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-6.0 for documentation about how writing a custom middleware.

In our case we will add custom maps to the ProblemDetailsMiddleware:
ex:

 // Add problem details support
            _ = services.AddProblemDetails(setup =>
            {
                setup.IncludeExceptionDetails = (_, _) => HostEnvironment.IsDevelopment();

                // Custom mapping function for FluentValidation's ValidationException.
                setup.MapFluentValidationException();

                setup.Map<InternalServerErrorException>(exception => new ProblemDetails
                {
                    Title = exception.Title,
                    Detail = exception.Detail,
                    Status = StatusCodes.Status500InternalServerError
                });

                setup.Map<BaseException>(exception => new ProblemDetails
                {
                    Title = exception.Title,
                    Detail = exception.Detail,
                    Status = StatusCodes.Status400BadRequest
                });

                setup.Map<ArgumentNullException>(exception => new ProblemDetails
                {
                    Title = "Null Argument",
                    Detail = exception.Message,
                    Status = StatusCodes.Status400BadRequest,
                    Extensions =
                    {
                        ["params"] = exception.ParamName
                    }
                });
            });

@kbeaugrand kbeaugrand added this to the S45 milestone Nov 2, 2022
@kbeaugrand kbeaugrand moved this to 📝 Todo in IoT Hub Portal Nov 2, 2022
@kbeaugrand
Copy link
Member

See https://github.com/Giorgi/EntityFramework.Exceptions to provide a better way to catch EntityFramework exceptions.

@Lucas-Zem Lucas-Zem moved this from 📝 Todo to 🚧 In Progress in IoT Hub Portal Nov 2, 2022
@kbeaugrand kbeaugrand moved this from 🚧 In Progress to 📝 Todo in IoT Hub Portal Nov 6, 2022
@kbeaugrand kbeaugrand assigned kbeaugrand and unassigned Lucas-Zem Nov 6, 2022
@hocinehacherouf hocinehacherouf moved this from 📝 Todo to 🚧 In Progress in IoT Hub Portal Nov 8, 2022
@hocinehacherouf hocinehacherouf assigned Sben65 and unassigned kbeaugrand Nov 8, 2022
Sben65 added a commit that referenced this issue Nov 10, 2022
kbeaugrand pushed a commit that referenced this issue Nov 11, 2022
* resolve #1247

* fix unit test

* update test

* delete useless using

* update test

* update test

* update test

* delete unused parameter

* add missing caracter

* add new test to edge device list

* request change
@kbeaugrand kbeaugrand moved this from 🚧 In Progress to 🚀 Ready in IoT Hub Portal Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: 🚀 Ready
Development

Successfully merging a pull request may close this issue.

4 participants