-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Convert DatabaseErrorPage to exception filter #24588
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cbe1da3
Convert DatabaseErrorPage middleware to exception filter
37e2433
Remove decoupling attempts
b47657f
Feedback and fixups
b0007b3
Revert webassembly file change
ed20f89
Fix musicstore
0f6ad6e
Feedback
69801ce
Fixup
cd6386d
Fix test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Middleware/Diagnostics.EntityFrameworkCore/src/DatabaseContextDetails.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore | ||
| { | ||
| internal class DatabaseContextDetails | ||
| { | ||
| public Type Type { get; } | ||
| public bool DatabaseExists { get; } | ||
| public bool PendingModelChanges { get; } | ||
| public IEnumerable<string> PendingMigrations { get; } | ||
|
|
||
| public DatabaseContextDetails(Type type, bool databaseExists, bool pendingModelChanges, IEnumerable<string> pendingMigrations) | ||
| { | ||
| Type = type; | ||
| DatabaseExists = databaseExists; | ||
| PendingModelChanges = pendingModelChanges; | ||
| PendingMigrations = pendingMigrations; | ||
| } | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/Middleware/Diagnostics.EntityFrameworkCore/src/DatabaseDeveloperPageExceptionFilter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Data.Common; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.Views; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| #nullable enable | ||
| namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore | ||
| { | ||
| public sealed class DatabaseDeveloperPageExceptionFilter : IDeveloperPageExceptionFilter | ||
| { | ||
| private readonly ILogger _logger; | ||
| private readonly DatabaseErrorPageOptions _options; | ||
|
|
||
| public DatabaseDeveloperPageExceptionFilter(ILogger<DatabaseDeveloperPageExceptionFilter> logger, IOptions<DatabaseErrorPageOptions> options) | ||
| { | ||
| _logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
| _options = options?.Value ?? throw new ArgumentNullException(nameof(options)); | ||
| } | ||
|
|
||
| public async Task HandleExceptionAsync(ErrorContext errorContext, Func<ErrorContext, Task> next) | ||
| { | ||
| if (!(errorContext.Exception is DbException)) | ||
| { | ||
| await next(errorContext); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| // Look for DbContext classes registered in the service provider | ||
| var registeredContexts = errorContext.HttpContext.RequestServices.GetServices<DbContextOptions>() | ||
| .Select(o => o.ContextType); | ||
|
|
||
| if (registeredContexts.Any()) | ||
| { | ||
| var contextDetails = new List<DatabaseContextDetails>(); | ||
|
|
||
| foreach (var registeredContext in registeredContexts) | ||
| { | ||
| var details = await errorContext.HttpContext.GetContextDetailsAsync(registeredContext, _logger); | ||
|
|
||
| if (details != null) | ||
| { | ||
| contextDetails.Add(details); | ||
| } | ||
| } | ||
|
|
||
| if (contextDetails.Any(c => c.PendingModelChanges || c.PendingMigrations.Any())) | ||
| { | ||
| var page = new DatabaseErrorPage | ||
| { | ||
| Model = new DatabaseErrorPageModel(errorContext.Exception, contextDetails, _options, errorContext.HttpContext.Request.PathBase) | ||
| }; | ||
|
|
||
| await page.ExecuteAsync(errorContext.HttpContext); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _logger.DatabaseErrorPageMiddlewareException(e); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
...gnostics.EntityFrameworkCore/src/DatabaseDeveloperPageExceptionFilterServiceExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
JunTaoLuo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using Microsoft.AspNetCore.Diagnostics; | ||
| using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
|
||
| #nullable enable | ||
| namespace Microsoft.Extensions.DependencyInjection | ||
| { | ||
| /// <summary> | ||
| /// Service extension methods for the <see cref="DatabaseDeveloperPageExceptionFilter"/>. | ||
| /// </summary> | ||
| public static class DatabaseDeveloperPageExceptionFilterServiceExtensions | ||
| { | ||
| /// <summary> | ||
| /// Add response caching services. | ||
| /// </summary> | ||
| /// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param> | ||
| /// <returns></returns> | ||
| public static IServiceCollection AddDatabaseDeveloperPageExceptionFilter(this IServiceCollection services) | ||
| { | ||
| if (services == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(services)); | ||
| } | ||
|
|
||
| services.TryAddEnumerable(new ServiceDescriptor(typeof(IDeveloperPageExceptionFilter), typeof(DatabaseDeveloperPageExceptionFilter), ServiceLifetime.Singleton)); | ||
|
|
||
| return services; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.