Back Exception Handler
This document explains how back-end exceptions can be handled through the BIA Framework.
Configure API Exception Handler
In the Startup class, within the Configure method, ensure there is a call to the extension ConfigureApiExceptionHandler, passing a boolean parameter to indicate whether the host environment is a development environment :
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJwtFactory jwtFactory)
{
// ...
app.ConfigureApiExceptionHandler(env.IsDevelopment());
// ...
}
This extension adds an exception handler middleware to the IApplicationBuilder to catch all unhandled exceptions before returning the HttpResponse. An error log will be automatically created with the exception content.
If the environment is not a development environment, the HttpStatusCode will be set to 500, and the HttpResponse.Body will be replaced with an "Internal server error" message to anonymize the application's errors.
FrontUserException
This is a custom exception used to display specific details to the end user of the application :
public class FrontUserException : Exception
{
/// <summary>
/// The error message key.
/// </summary>
public FrontUserExceptionErrorMessageKey ErrorMessageKey { get; } = FrontUserExceptionErrorMessageKey.Unknown;
/// <summary>
/// The parameters to format into the current <see cref="Exception.Message"/>.
/// </summary>
public string[] ErrorMessageParameters { get; } = [];
}
An ErrorMessageKey is used to identify the type of error and retrieve the corresponding user-friendly error message.
A set of ErrorMessageParameters can be used in combination with the manual Message or the corresponding message according to the ErrorMessageKey to format the final user-friendly error message to be returned.
The FrontUserExceptionErrorMessageKey enum is a list of common errors that can be automatically handled by the BIA Framework and configured to use a corresponding user-friendly error message.
FrontUserException Inheritance
If needed, the FrontUserException can be inherited by a custom exception, adding a new enum identifier to improve exception identification throughout the application :
public enum CustomErrorMessageKey
{
BusinessError,
TemplateBusinessError
}
public class CustomFrontUserException : FrontUserException
{
public CustomFrontUserException(CustomErrorMessageKey errorMessageKey, Exception? innerException, params string[] errorMessageParameters)
: base(GetErrorMessage(errorMessageKey), innerException, errorMessageParameters)
{
ErrorMessageKey = errorMessageKey;
}
// Mind the new instruction
public new CustomErrorMessageKey ErrorMessageKey { get; }
// Write your own method to get user friendly error message by key
private static string GetErrorMessage(CustomErrorMessageKey errorMessageKey)
{
return errorMessageKey switch
{
CustomErrorMessageKey.BusinessError => "This is a business error",
CustomErrorMessageKey.TemplateBusinessError => "This is a template business error - {0} - {1}",
};
}
}