Open Closed

Customizing Generic Error Message for Unexpected Exceptions #8889


User avatar
0
nhontran created
  • ABP Framework version: v8.0.2

  • UI Type: Angular

  • Database System: EF Core (SQL Server)

  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

How can I set an error code and override the default generic error message ("An internal error occurred during your request!") for the unexpected exceptions?

{
"error": {
"code": null,
"message": "An internal error occurred during your request!",
"details": null,
"data": {},
"validationErrors": null
}
}


4 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, ABP provides an Error Handling System, which allow you to customize error message and the relevant HTTP status code. Please check the Exception Handling documentation. In the documentation, you can see how to set custom HTTP status code for certain exception and how to customize error message etc.

    For the exception that you shared, it's not handled by ABP it seems, so we need further info about the exception, where it's thrown, the related endpoint etc.

    Best regards.

  • User Avatar
    0
    nhontran created

    Hi @EngincanV, I understand that I can wrap each endpoint in a try-catch block to handle unexpected errors, but that would be time-consuming for us. Is there a way to centrally handle unexpected errors and return a specific error code and message?

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi @EngincanV, I understand that I can wrap each endpoint in a try-catch block to handle unexpected errors, but that would be time-consuming for us. Is there a way to centrally handle unexpected errors and return a specific error code and message?

    ABP already has a built-in mechanism for global exception handling so you don’t have to wrap each endpoint in a try-catch block. ABP automatically handles all exceptions and sends a standard formatted error message to the client for an API/AJAX request.

    But in your case, since it does not met the exception handling criteria it's not handled by ABP. So, you can subscribe to all of the thrown exceptions by implementing the ExceptionSubscriber base class, as described here. You can apply your own logic for the thrown exceptions.

  • User Avatar
    0
    nhontran created

    Hi @EngincanV,

    noted, I will explore the ExceptionSubcriber. Thank you!

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 13, 2025, 04:08