Open Closed

How to redirect all exceptions by type to some http error code #9638


User avatar
0
sinancondo created

I want to override some defaults and make all Business Exceptions return as 422 , how can I do that ? I see there is map option but that for a single error code, what I want is by type of exception. thanks


2 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent

    To map all exceptions of a specific type (such as BusinessException) to a particular HTTP status code (e.g., 422 Unprocessable Entity) in ABP Framework, you need to configure custom mappings in your application's startup. While the built-in Map method is for error codes, you can achieve type-based mapping by replacing the default status code finder service.

    Here's how you can do it:

    1. Create a custom implementation of DefaultHttpExceptionStatusCodeFinder:
    using System.Net;
    using Volo.Abp.AspNetCore.ExceptionHandling;
    using Volo.Abp.ExceptionHandling;
    
    public class CustomHttpExceptionStatusCodeFinder : DefaultHttpExceptionStatusCodeFinder
    {
        public override HttpStatusCode GetStatusCode(HttpExceptionStatusCodeContext context)
        {
            if (context.Exception is BusinessException)
            {
                return (HttpStatusCode)422; // Unprocessable Entity
            }
    
            return base.GetStatusCode(context);
        }
    }
    
    1. Register your custom finder in the dependency injection container (usually in your module's ConfigureServices method):
    using Microsoft.Extensions.DependencyInjection;
    using Volo.Abp.AspNetCore.ExceptionHandling;
    
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddTransient<IHttpExceptionStatusCodeFinder, CustomHttpExceptionStatusCodeFinder>();
    }
    

    This will ensure that all exceptions of type BusinessException are returned with HTTP status code 422.

    Sources:

    • https://abp.io/docs/latest/framework/fundamentals/exception-handling

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    AI is right.

    You can override the DefaultHttpExceptionStatusCodeFinder.

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs#L70-L73

    Thanks.

Learn More, Pay Less
33% OFF
All Trainings!
Get Your Deal
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on September 12, 2025, 10:20