Commit 2abd369
fix: implement ASP.NET Core 10 error handling (#685)
* fix: implement ASP.NET Core 10 error handling best practices
- Add GrandExceptionHandler implementing IExceptionHandler (ASP.NET Core 8+ pattern):
logs exceptions, returns RFC 7807 ProblemDetails JSON for API (Bearer) requests,
returns false for web requests so the configured error page handles the response.
Guards with HasStarted before writing.
- Register AddExceptionHandler<GrandExceptionHandler>() and AddProblemDetails() in
ErrorHandlerStartup.ConfigureServices.
- Fix UseGrandExceptionHandler: remove dual UseExceptionHandler registration bug.
Single UseExceptionHandler('/errorpage.htm') now invokes the IExceptionHandler chain
first, then falls back to re-executing at /errorpage.htm for non-API requests.
- Fix UsePageNotFound: replace 302 Redirect with UseStatusCodePagesWithReExecute
('/page-not-found') which preserves the original 404 status code. Add inline
middleware to disable status code pages for API (Bearer) and static-resource requests
so those callers receive their original response unchanged.
- Fix ApiAuthenticationRegistrar: replace catch(Exception ex) { throw new Exception(ex.Message) }
with bare throw to preserve the original stack trace; update OnAuthenticationFailed to
return application/problem+json via IProblemDetailsService instead of plain text.
- Fix BaseController.LogException: use a constant message template instead of
exception.Message to avoid structured-logging format issues.
Agent-Logs-Url: https://github.com/grandnode/grandnode2/sessions/ecc051c4-72f1-4d90-8ec9-025f19ab46c3
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
* fix: use StartsWith for Authorization header scheme check (safer than Split()[0])"
Agent-Logs-Url: https://github.com/grandnode/grandnode2/sessions/ecc051c4-72f1-4d90-8ec9-025f19ab46c3
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
* refactor: extract IsApiRequest helper to eliminate duplicate auth-header check
Agent-Logs-Url: https://github.com/grandnode/grandnode2/sessions/1ec7db4a-11fc-49b4-8aa2-85f12990ec16
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
* fix: restrict UseStatusCodePagesWithReExecute to 404-only; static ContentTypeProvider; remove redundant try/catch; add OnAuthenticationFailed fallback
Agent-Logs-Url: https://github.com/grandnode/grandnode2/sessions/24b98e75-69d0-4ef3-a43e-2feb96bb1479
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
* fix: GrandExceptionHandler now handles web requests with redirect; remove path fallback from UseExceptionHandler
Agent-Logs-Url: https://github.com/grandnode/grandnode2/sessions/5190d235-2337-4c71-add0-49709d3379b1
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
* Disable full error stack display in production
Changed "DisplayFullErrorStack" in appsettings.json from true to false to prevent full error stack traces from being shown in production environments. This enhances security and user experience by limiting error details exposed to end users.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
Co-authored-by: KrzysztofPajak <krzysiek@grandnode.com>1 parent 43bcfca commit 2abd369
6 files changed
Lines changed: 184 additions & 93 deletions
File tree
- src
- Modules/Grand.Module.Api/Infrastructure
- Web
- Grand.Web.Common
- Controllers
- Infrastructure
- Startup
- Grand.Web/App_Data
Lines changed: 60 additions & 39 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
39 | 57 | | |
40 | 58 | | |
41 | | - | |
| 59 | + | |
42 | 60 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
54 | 65 | | |
55 | | - | |
| 66 | + | |
56 | 67 | | |
57 | | - | |
| 68 | + | |
58 | 69 | | |
59 | 70 | | |
60 | 71 | | |
| |||
80 | 91 | | |
81 | 92 | | |
82 | 93 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
87 | 115 | | |
88 | 116 | | |
89 | | - | |
| 117 | + | |
90 | 118 | | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
103 | 124 | | |
104 | | - | |
| 125 | + | |
105 | 126 | | |
106 | | - | |
| 127 | + | |
107 | 128 | | |
108 | 129 | | |
109 | 130 | | |
110 | 131 | | |
111 | 132 | | |
112 | 133 | | |
113 | 134 | | |
114 | | - | |
| 135 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
| 119 | + | |
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| |||
Lines changed: 45 additions & 52 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
26 | 25 | | |
27 | 26 | | |
28 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
29 | 43 | | |
30 | 44 | | |
31 | 45 | | |
| |||
39 | 53 | | |
40 | 54 | | |
41 | 55 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 56 | + | |
| 57 | + | |
72 | 58 | | |
73 | 59 | | |
74 | 60 | | |
75 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
76 | 64 | | |
77 | 65 | | |
78 | 66 | | |
79 | 67 | | |
80 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
81 | 77 | | |
82 | | - | |
83 | | - | |
| 78 | + | |
84 | 79 | | |
85 | | - | |
86 | | - | |
87 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
88 | 86 | | |
89 | | - | |
90 | | - | |
| 87 | + | |
91 | 88 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
97 | 95 | | |
98 | | - | |
99 | | - | |
100 | 96 | | |
101 | 97 | | |
102 | 98 | | |
| |||
112 | 108 | | |
113 | 109 | | |
114 | 110 | | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 111 | + | |
119 | 112 | | |
120 | 113 | | |
121 | 114 | | |
| |||
Lines changed: 72 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
22 | 27 | | |
23 | 28 | | |
24 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
0 commit comments