This article outlines how I handle errors in Umbraco 10.

What I have done is create a CustomError Document type, following the Umbraco docs for a custom 404 page, using the same template I created some more error pages, an errorNoAcess and errorHandler. The CustomError Document type just basic template with a title and message.

I discovered that depending how the error is raised/returned, you could get either an exception or a http statuscode, so you have to handle both :)

So, I created my own exceptionhandler midddleware to handle the exceptions and then added some code to my startup.cs to handle the status codes.

using System.Net;
using Microsoft.AspNetCore.Http;

namespace MediaWiz.Core.MiddleWare
{
    public class ExceptionHandlingMiddleware
    {
        private readonly RequestDelegate _next;

        public ExceptionHandlingMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            var allowedcodes = new[] { 200, 301, 302, 401,403,404,500 };
            try
            {
                await _next(context);
                if (!allowedcodes.Contains(context.Response.StatusCode))
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    context.Response.Redirect("/errorHandler");

                }
            }
            catch (UnauthorizedAccessException)
            {
                context.Response.Clear();
                context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                context.Response.Redirect("/errorNoAccess");
            }
            catch (Exception ex)
            {
                if (context.Response.StatusCode is 404)
                {
                    //may be from an exception in startup, so lets just let it get handled by the default 404;
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    context.Response.Redirect("/errorHandler");
                }

            }
        }
    }
}

I also added an extension method to register the middleware.

public static class MiddleWareExtensions
{
    public static IApplicationBuilder MyExceptionHandler(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<ExceptionHandlingMiddleware>();
    }
}

 

I then registered it in my startup.cs and added some code to handle errors returned as statuscodes in the response

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env,IBackgroundJobClient backgroundJobClient, IRecurringJobManager recurringJobManager)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.MyExceptionHandler();
            }

            app.Use(async (context, next) =>
            {
                context.Response.Headers.Add("X-Xss-Protection", "1; mode=block");
                context.Response.Headers.Add("Strict-Transport-Security", "max-age=31536000");
                context.Response.Headers.Add("X-Content-Type-Options", "nosniff");
                context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
                await next();

                if (context.Response.StatusCode == 404)
                {
                    context.Request.Path = "/error404/";
                    await next();
                }
                if (context.Response.StatusCode is 401 or 403)
                {
                    context.Request.Path = "/errorNoAccess/";
                    await next();
                }
                //This is a BadRequest so use the default handler (500)
                if (context.Response.StatusCode == 500)
                {
                    context.Request.Path = "/errorHandler/";
                    await next();
                }
            });

            app.UseAuthentication();
            app.UseRouting();

            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    u.UseBackOffice();
                    u.UseWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                });

        }

 

backoffice Umbraco 10 Extension appsetting RichText Vite package LIT IMailService Umbraco Media Wizard Umbraco 9 BlockGrid TypeScript MediaWiz Umbraco 17 Membership nuget Email Validate MediaWiz Forum

This guide walks through building a real Tiptap extension for the Umbraco backoffice — a DateTime inserter that lets editors insert the current date, time, or both into a Rich Text Editor (RTE) field from a toolbar dropdown menu

building a custom collection view for Umbraco 17 that displays documents as beautiful cards with images, using the new extension system and Lit Element web components.

In this post, I'll show you how to build a flexible, on-the-fly watermarking system using SixLabors.ImageSharp.Web that doesn't require modifying your original images.