Ends in:
2 DAYS
5 HRS
46 MIN
41 SEC
Ends in:
2 D
5 H
46 M
41 S
Open Closed

BusinessException do not show the message the same way as UserFriendlyException #3739


User avatar
1
christophe.baille created
  • ABP Framework version: v5.3.2
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

Some BusinessException have been implemented and follow this doc.

https://docs.abp.io/en/abp/latest/Exception-Handling#business-exceptions

If it put the correct information on AbpAuditLogs table which is good, the "popup" do not show exactly what I would expect.

It is not good as the final user will not understand the error

I use this code to throw the error:

            throw new BusinessException(AIGeneratorErrorCodes.ProjectManagement.ProjectMaxCountExceeded)
                .WithData("MaxCount", AIGeneratorConsts.Project.MaxNumberOfProjectsPerUser);

By replacing it with UserFriendlyException, it show the code correctly (apart that I will need to use localizer to get the text), but information are not that good on the AbpAuditLogs table, in Exception field, I do not have MaxCount.

Is there any way to make the popup text for BusinessException as clear as UserFriendlyException?


12 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Could you provide steps to reproduce? thanks.

  • User Avatar
    0
    christophe.baille created

    Here is my code on the Appservice which I call through a Blazor server page (I did put the thow here as example):

    UserFriendlyException show the text

    BusinessException do not

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi

    You can see the document: https://docs.abp.io/en/abp/latest/Exception-Handling#using-error-codes

    We also throw a BusinessException in our modules, you need to configure the localization https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleManager.cs#L67

  • User Avatar
    0
    christophe.baille created

    Hi,

    It remains the same, by configuring the localization it will change the text shown only. My issue here is that BusinessException do not return the message expected but "Exception of type..."

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I can't reproduce the problem.

    Here is my test code:

    BusinessException

    UserFriendlyException

  • User Avatar
    0
    christophe.baille created

    I see, from what you show me is that BusinessException do not show the error message but a generic error anytime right?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Yes, this is by design.

    BusinessException will show the generic error message if there is no localization text

    The document explains as well: https://docs.abp.io/en/abp/latest/Exception-Handling#business-exceptions

    However, you can try this:

    public class MyBusinessException : BusinessException, IUserFriendlyException
    {
        public MyBusinessException(string message) : base(code: Empty, message: message)
        {
        }
    }
    
    throw new MyBusinessException("Message");
    
  • User Avatar
    0
    christophe.baille created

    Hi,

    This is the point about "if there is no localization text" that annoy me, from the doc it is what I understood as well but even with a localization text I got generic message:

    I tried to define either with "Localizer" or "LocalizationRessource", with either define on constructor or using DI (then I guess BusinessException localize automatically without using Localizer[] as we used everywhere?):

    My json ressource file:

    But still got this generic error:

    the ressource seems ok as I got this on UserFriendlyException:

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    https://docs.abp.io/en/abp/latest/Exception-Handling#business-exceptions

    throw new BusinessException(QaErrorCodes.CanNotVoteYourOwnAnswer);

    QaErrorCodes.CanNotVoteYourOwnAnswer is just a const string. The following error code format is recommended:

    <code-namespace>:<error-code> Volo.Qa:010002

    it expects a code,

    And see this: https://docs.abp.io/en/abp/latest/Exception-Handling#using-error-codes

    You have to configure the code namespaces map.

    For example:

    throw new BusinessException(QaDomainErrorCodes.CanNotVoteYourOwnAnswer);

    • QaDomainErrorCodes.CanNotVoteYourOwnAnswer is Volo.Qa:010002 in this example.
    services.Configure<AbpExceptionLocalizationOptions>(options =>
    {
        options.MapCodeNamespace("Volo.Qa", typeof(QaResource));
    });
    
    {
      "culture": "en",
      "texts": {
        "Volo.Qa:010002": "You can not vote your own answer!"
      }
    }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    This is the point about "if there is no localization text" that annoy me

    If you want to show the error message if there is no localization text, you can try this:

    public class MyBusinessException: BusinessException, IUserFriendlyException
    {
        public MyBusinessException(string code, string message) : base(code: code, message: message)
        {
        }
    }
    
    throw new MyBusinessException("App:10001" , "The error message");
    

    In this way, if there is no App:10001 localization text, it will show the "The error message"

  • User Avatar
    0
    christophe.baille created

    Actually my issue is different:

    When I was doing some tests on my main project, I had the issue that it was not showing the message but "An internal error occured during your request".

    It was my mistake as while implementing it for testing, I forgot to add

            Configure&lt;AbpExceptionLocalizationOptions&gt;(options =>
            {
                options.MapCodeNamespace("MyApp.ErrorCodes", typeof(MyAppResource));
            });
    

    I then added it and now the test is working well.

    So you were completely right about when I was having "An internal error occured during your request" instead of my message, the localization text was missing.

    However my initial issue is coming from a module"linked" to the main project.

    I had a look on it and all is well implemented, localization is all fine.

    What I do not really understand, is that anytimes (I mean even with or without localization), the message I get is different:

    "Exception of type 'Volo.Abp.BusinessException' was thrown."

    which is not "An internal error occured during your request"

    Like if the BusinessException is shown differently.

    My main project is on Blazor server, the module UI is Blazor Webassembly

  • User Avatar
    0
    christophe.baille created

    I found the issue.

    The problem was on how to manage to show the error on the blazor view.

    The main project is using "HandleErrorAsync" method, and the module was using "UiMessageService.Error(ex.Message)"

    As "HandleErrorAsync" was not available on my blazor component, I had to inherit from AbpComponentBase (I actually did same as the main project, I created a new class MyModuleComponentBase who inherit from AbpComponentBase and define LocalizationResource on it)

    Thanks for your help anyway, I at least now understand way better on how BusinessException works (and how to show it as well).

Made with ❤️ on ABP v9.1.0-preview. Updated on December 02, 2024, 12:35