'Adding details in custom exception handling

I am currently developing a custom exception middleware and I also have my own exceptions. The goal is to be able to have a common library for multiple APIs and in this middleware, it handles the error responses that are sent out. Middleware:

public static void ConfigureExceptionHandler(this IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseExceptionHandler(appError =>
        {
            appError.Run(async context =>
            {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Response.ContentType = "application/json";

                var exception = context.Features.Get<IExceptionHandlerFeature>();
                if (exception != null)
                {
                    var ex = exception?.Error;
                    var isDev = env.IsDevelopment();

                    switch (exception.Error)
                    {
                        case ArgumentException:
                            context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                            break;
                        case NotFoundException:
                            context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                            break;
                        default:
                            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            break;
                    }

                    //using RFC7807 standard detailed here: https://datatracker.ietf.org/doc/html/rfc7807
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(
                        new ProblemDetails
                        {
                            Type = ex.GetType().Name,
                            Status = context.Response.StatusCode,
                            Instance = exception?.Path,
                            Title = isDev ? $"{ex.Message}" : "An error occurred.",
                            Detail = isDev ? ex.StackTrace : null,
                            Extensions = {  }
                        }));
                }
            });
        });
    }
}

And this is the exception:

[Serializable]
public class NotFoundException : Exception
{
    public object? MissingObject { get; }

    public NotFoundException() { }

    public NotFoundException(string message)
        : base(message) { }

    public NotFoundException(string message, Exception innerException)
        : base(message, innerException) { }

    public NotFoundException(string message, object missingObject)
    : this(message)
    {
        MissingObject = missingObject;
    }
}

Is there a way so that whenever I create exceptions, extra fields that I create such as the MissingObject in the NotFoundException is added as an extra detail? I assume it would go in Extensions but I do not know how to include it in a generic sense.

Also if there is a NuGet package that I can use that does all of this for me including handling custom exceptions, feel free to recommend and I will research about them



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source