-
Notifications
You must be signed in to change notification settings - Fork 528
fix: implement ASP.NET Core 10 error handling #685
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
KrzysztofPajak
merged 6 commits into
develop
from
copilot/check-error-handling-implementation
May 3, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b6e761
fix: implement ASP.NET Core 10 error handling best practices
Copilot 4684aef
fix: use StartsWith for Authorization header scheme check (safer than…
Copilot ad1c5be
refactor: extract IsApiRequest helper to eliminate duplicate auth-hea…
Copilot 2e3490f
fix: restrict UseStatusCodePagesWithReExecute to 404-only; static Con…
Copilot 6b56346
fix: GrandExceptionHandler now handles web requests with redirect; re…
Copilot 999033f
Disable full error stack display in production
KrzysztofPajak 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
72 changes: 72 additions & 0 deletions
72
src/Web/Grand.Web.Common/Infrastructure/GrandExceptionHandler.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,72 @@ | ||
| using Grand.Data; | ||
| using Microsoft.AspNetCore.Diagnostics; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Grand.Web.Common.Infrastructure; | ||
|
|
||
| /// <summary> | ||
| /// Handles unhandled exceptions according to ASP.NET Core best practices. | ||
| /// For API requests (Bearer token) it writes an RFC 7807 ProblemDetails JSON response. | ||
| /// For regular web (Razor/MVC) requests it redirects to the static error page (/errorpage.htm). | ||
| /// </summary> | ||
| public class GrandExceptionHandler : IExceptionHandler | ||
| { | ||
| private readonly ILogger<GrandExceptionHandler> _logger; | ||
|
|
||
| public GrandExceptionHandler(ILogger<GrandExceptionHandler> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public async ValueTask<bool> TryHandleAsync( | ||
| HttpContext httpContext, | ||
| Exception exception, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| if (httpContext.Response.HasStarted) | ||
| return false; | ||
|
|
||
| // Log the exception when the database is available | ||
| if (DataSettingsManager.DatabaseIsInstalled()) | ||
| _logger.LogError(exception, "An unhandled exception has occurred"); | ||
|
|
||
| if (!ApplicationBuilderExtensions.IsApiRequest(httpContext.Request)) | ||
| { | ||
| // For Razor/MVC web requests, redirect to the static error page so the | ||
| // browser always sees a user-friendly page (the path-based re-execution | ||
| // fallback in UseExceptionHandler is unreliable in an MVC pipeline). | ||
| httpContext.Response.Redirect("/errorpage.htm", permanent: false); | ||
| return true; | ||
| } | ||
|
|
||
| httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; | ||
|
|
||
| var problemDetailsService = httpContext.RequestServices.GetService<IProblemDetailsService>(); | ||
| if (problemDetailsService != null) | ||
| { | ||
| await problemDetailsService.WriteAsync(new ProblemDetailsContext { | ||
| HttpContext = httpContext, | ||
| Exception = exception, | ||
| ProblemDetails = new ProblemDetails { | ||
| Status = StatusCodes.Status500InternalServerError, | ||
| Title = "An error occurred while processing your request", | ||
| Instance = httpContext.Request.Path | ||
| } | ||
| }); | ||
| } | ||
| else | ||
| { | ||
| httpContext.Response.ContentType = "application/problem+json"; | ||
| await httpContext.Response.WriteAsJsonAsync(new ProblemDetails { | ||
| Status = StatusCodes.Status500InternalServerError, | ||
| Title = "An error occurred while processing your request", | ||
| Instance = httpContext.Request.Path | ||
| }, cancellationToken); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } |
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.