Skip to content

Commit 2fd2170

Browse files
authored
Merge pull request #7106 from umbraco/500-errors
Feedback on 500 error page
2 parents 1c4c3c0 + a64bc1e commit 2fd2170

File tree

3 files changed

+171
-70
lines changed

3 files changed

+171
-70
lines changed

.github/styles/UmbracoDocs/Acronyms.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ exceptions:
9090
- SSD
9191
- ASCII
9292
- CMD
93+
- NGINX

15/umbraco-cms/tutorials/custom-error-page.md

Lines changed: 85 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: >-
3-
A set of tutorials creating and implementating custom error pages in an
3+
A set of tutorials for creating and implementating custom error pages in an
44
Umbraco CMS project.
55
---
66

@@ -16,17 +16,17 @@ In Umbraco, in-code error page handling refers to managing and displaying custom
1616

1717
This article contains guides on how to create custom error pages for the most common scenarios:
1818

19-
* [404 errors ("Page not found")](custom-error-page.md#id-404-errors)
20-
* [500 errors ("Internal Server Error")](custom-error-page.md#id-500-errors)
21-
* [Boot Failed errors](custom-error-page.md#errors-with-booting-a-project)
19+
* [404 Errors ("Page not found")](#404-errors)
20+
* [500 Errors ("Internal Server Error")](#500-errors)
21+
* [Boot Failed Errors](#boot-failed-errors)
2222

2323
{% hint style="info" %}
2424
**Are you looking for a guide to create a custom maintenance page?**
2525

2626
This has been moved to a separate article: [Create a custom maintenance page](create-a-custom-maintenance-page.md).
2727
{% endhint %}
2828

29-
## 404 errors
29+
## 404 Errors
3030

3131
This kind of error can occur when something has been removed, when a path has been changed, or when the chosen path is invalid.
3232

@@ -37,7 +37,7 @@ This method will use a 404 page created via the backoffice.
3737
1. Navigate to the **Settings** section.
3838
2. Create a new "_Document Type with Template_".
3939
3. Name the Document Type **404**.
40-
4. \[Optional] Add properties on the Document Type.
40+
4. [Optional] Add properties on the Document Type.
4141
1. In most cases, the 404 not found page would be static.
4242
5. Fill out the Template with your custom markup.
4343
6. Create another **Document Type**, but create it without the Template.
@@ -65,7 +65,6 @@ Once that is done, the next step focuses on setting up the appropriate configura
6565
"Error404Collection": [
6666
{
6767
"Culture": "default",
68-
// Replace the value for ContentKey with the GUID from step 1
6968
"ContentKey": "81dbb860-a2e3-49df-9a1c-2e04a8012c03"
7069
}
7170
]
@@ -75,9 +74,22 @@ Once that is done, the next step focuses on setting up the appropriate configura
7574
}
7675
```
7776

78-
{% hint style="info" %}
79-
With this approach, you can set different 404 pages for specific languages (cultures) - such as `en-us`, `it` etc.
80-
{% endhint %}
77+
In the above code sample, replace the value for `ContentKey` with the GUID from step 1.
78+
79+
With this approach, you can set different 404 pages for specific languages/cultures (such as `en-us`, `da-dk`, and so on):
80+
81+
```json
82+
"Error404Collection": [
83+
{
84+
"Culture": "en-us",
85+
"ContentKey": "guid-for-english-404"
86+
},
87+
{
88+
"Culture": "da-dk",
89+
"ContentKey": "guid-for-danish-404"
90+
}
91+
]
92+
```
8193

8294
### Set a custom 404 page using IContentLastChanceFinder
8395

@@ -141,35 +153,38 @@ public class Mycomposer : IComposer
141153
```
142154
{% endcode %}
143155

144-
## 500 errors
156+
## 500 Errors
145157

146-
The following steps will guide you through setting up a page for internal server errors.
158+
This section guides you in setting up a custom page for handling internal server errors (500 errors) in your Umbraco site. This setup works when:
147159

148-
### Create a 500-page in the backoffice
160+
* A template throws an error.
161+
* A controller throws an unhandled exception.
162+
* A request hits the application, but something fails during rendering or processing.
149163

150-
The first step is to create the content and structure for it in the Umbraco backoffice.
164+
### Create a 500 error page in the Backoffice
151165

152166
1. Access the Umbraco Backoffice.
153167
2. Navigate to the **Settings** section.
154-
3. Create a new **Document Type with Template** called **500**.
155-
4. \[Optional] Add relevant properties to the Document Types and define the Template.
168+
3. Create a new **Document Type with Template** called *500*.
169+
4. [Optional] Add relevant properties to the Document Types and define the template layout.
156170
5. Fill out the Template with your custom markup.
157-
6. Follow steps 6-9 in the [Create a 404 page in the backoffice](custom-error-page.md#create-a-404-page-in-the-backoffice).
158-
1. This step can be skipped if you already have a content structure for error content nodes in the project.
171+
6. Follow steps 6-9 in the [Create a 404 page in the backoffice](custom-error-page.md#create-a-404-page-in-the-backoffice) section.
172+
* You can skip this if you already have a structure for status code content nodes.
159173
7. Add the **500** Document Type as an **Allowed child node type** on the **Statuscode** Document Type.
160-
8. Navigate to the **Content** section.
174+
8. Go to the **Content** section.
161175
9. Create a **Statuscodes** content item if one does not exist already.
162-
10. Create a **500** content item under the **Statuscodes** content.
176+
10. Create a new content node of type **500** under the **Statuscodes** content node.
163177

164-
### Configure the 500 error page programmatically
178+
### Configure programmatic error handling
165179

166-
The next step in this guide is to configure the 500 pages to be shown when an internal server error occurs.
180+
Now configure the application to display the 500 error page when internal server errors occur.
167181

168-
1. Create a folder in the root of your Umbraco project, called `Controllers`.
169-
2. Create a file in this folder, called `ErrorController.cs`.
182+
1. Create a folder called `Controllers` in the root of your Umbraco project.
183+
2. Create a new file called `ErrorController.cs` in the `Controllers` folder.
170184
3. Add the following code to the file:
171185

172186
{% code title="ErrorController.cs" %}
187+
173188
```csharp
174189
using Microsoft.AspNetCore.Mvc;
175190

@@ -192,29 +207,35 @@ public class ErrorController : Controller
192207
}
193208
}
194209
```
210+
195211
{% endcode %}
196212

197213
{% hint style="info" %}
198-
**Namespace:** Replace _YourProjectNamespace_ with the actual project name. In Visual Studio, you can use _Sync Namespaces_ from the project context menu (in _Solution Explorer_ View).
214+
Replace _YourProjectNamespace_ with the actual project name. In Visual Studio, you can use _Sync Namespaces_ from the project context menu (in _Solution Explorer_ View).
199215
{% endhint %}
200216

201-
4. Add an entry in `appSettings.json` for the new "Error" route:
217+
4. Add the `/error/` route to the list of reserved paths in the `appSettings.json` file:
202218

203219
{% code title="appSettings.json" %}
220+
204221
```json
205222
"Umbraco": {
206223
"CMS": {
207224
"Global": {
208225
"ReservedPaths": "~/app_plugins/,~/install/,~/mini-profiler-resources/,~/umbraco/,~/error/",
209226
...
227+
}
228+
}
229+
}
210230
```
231+
211232
{% endcode %}
212233

213-
5. Update `Program.cs` with the following code:
234+
5. Update `Program.cs` to ensure the error route is triggered by unhandled exceptions:
214235

215236
{% code title="Program.cs" %}
237+
216238
```csharp
217-
...
218239
WebApplication app = builder.Build();
219240

220241
if (builder.Environment.IsDevelopment())
@@ -226,30 +247,59 @@ else
226247
app.UseExceptionHandler("/error");
227248
}
228249
```
250+
229251
{% endcode %}
230252

231253
{% hint style="info" %}
232-
To **test this locally**, in Visual Studio replace `app.UseDeveloperExceptionPage();` by `app.UseExceptionHandler("/error");`. Otherwise, you will get the default error page.
254+
To test this locally, replace `app.UseDeveloperExceptionPage();` by `app.UseExceptionHandler("/error");`. Otherwise, you will get the default error page.
233255
{% endhint %}
234256

235-
### How to trigger a 500 error for testing
257+
### Trigger a 500 error (for testing)
258+
259+
To trigger a 500 error, change a `Model.Value` property in your template. For example, if your Document Type has a property called `test`you would normally render it with:
260+
261+
```csharp
262+
@Model.Value("test")
263+
```
264+
265+
To deliberately cause an error, change it to something invalid like:
266+
267+
```csharp
268+
@Model.ValueTest("test")
269+
```
270+
271+
This should throw an error, triggering your custom 500 page.
272+
273+
### Handling app startup failures
274+
275+
When Umbraco fails to start, you may see a blank screen or receive a `500.30` or `502.5` error. These indicate the web application crashed or failed to initialize.
276+
277+
#### Why can't the app serve an error page?
278+
279+
During startup, Umbraco relies on the ASP.NET Core pipeline. If the app crashes before this pipeline is fully initialized, it can't handle requests or serve custom error pages. That's why you can't rely on Umbraco or ASP.NET Core routing to show error content at this point as it has already failed. For more information, see the [Handle errors in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling) documentation.
280+
281+
Instead, the web server itself (IIS, NGINX, Apache, and so on) must serve a static fallback 500 page. This page is independent of the application and helps communicate the issue to users when the site is down.
282+
283+
To handle these types of issues:
236284

237-
You can trigger a 500 error on your front end by changing a `Model.Value` property in your template. For example, on a Document Type with a property called `test`. The way to render it in the front would be `Model.Value("test");` To trigger a 500 error page you can add anything after Value such as `Model.ValueTest("test");`
285+
* Configure your web server (IIS, NGINX, Apache) to serve a static HTML 500 page when the app fails to respond.
286+
* Use uptime monitoring to catch failed starts.
287+
* Check Umbraco logs in `App_Data/Logs` for startup errors.
238288

239-
## Errors with booting a project
289+
## Boot Failed Errors
240290

241291
Sometimes you might experience issues with booting up your Umbraco project. This could be a brand new project, or it could be an existing project after an upgrade.
242292

243293
You will be presented with a generic error page when there is an error during boot.
244294

245-
![Boot Failed. Umbraco failed to boot, if you are the owner of the website please see the log file for more details.](../../../10/umbraco-cms/tutorials/images/BootFailedGeneric.png)
295+
![Boot Failed](../../../10/umbraco-cms/tutorials/images/BootFailedGeneric.png)
246296

247-
This page can be overwritten with a custom BootFailed HTML page. Follow the steps below to set it up:
297+
You can replace the default BootFailed page with a custom static `BootFailed.html`. Follow the steps below to set it up:
248298

249299
1. Open your project files.
250300
2. Navigate to `wwwroot/config/errors`
251301
1. If this folder does not exist already, create it.
252-
3. Add a new file called **`BootFailed.html`**.
302+
3. Add a new file called *BootFailed.html*.
253303
4. Add your custom markup to the file.
254304

255305
The `BootFailed.html` page will only be shown if debugging is disabled in the `appsettings.json` file. Debugging is handled using the **Debug** key under `Umbraco:CMS:Hosting`:

0 commit comments

Comments
 (0)