Base solution for your next web application
Open Closed

Error 500 popup still shows even when exception is catched #5997


User avatar
0
antonis created

I am using aspnet core & angular and I have:

public async Task<Response> CreateClient(Client input)
        {
            input.Name = null;
            Response result = new Response();
            try
            {
                await repo.InsertAsync(input);
                await unitOfWorkManager.Current.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    result.ErrorMessage = ex.InnerException.Message;
                else
                    result.ErrorMessage = ex.Message;
            }
            return result;
        }

based on the fact that I set input.Name = null when await repo.SaveChangesAsync(); is called it throws an exception (Name is required). I catch the exception and have custom logic to return the response with error in it. Problem is that in client I still get this ugly popup:

which I dont want to show for custom forms. The purpose of this logic inside the app service is to avoid using the popups for CRUD and errors and instead develop using my own controls.


11 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    In fact, although you caught the exception of await unitOfWorkManager.Current.SaveChangesAsync(). the unit of work still calls SaveChanges when the method call ends. causing a new exception.

    You can consider using user friendly exceptions. https://aspnetboilerplate.com/Pages/Documents/Handling-Exceptions#userfriendlyexception

  • User Avatar
    0
    antonis created

    @maliming How can I use user friendly exception in my scenario since the unitofwork throws exception?

  • User Avatar
    0
    maliming created
    Support Team

    First of all, I think that the verification of model parameters and the verification of other data should not be handled by EF. You should verify it before storing it in the database. The error information of the database is also difficult for users to understand.

    When you detect a parameter or other business problem, just throw an exception. Throw new UserFriendlyException("message")

    Please refer to:https://aspnetboilerplate.com/Pages/Documents/Handling-Exceptions#userfriendlyexception

  • User Avatar
    0
    antonis created

    Maliming you are correct this was just quick test code to show my problem. I already did what you suggested Throw new UserFriendlyException("message") but still I get the popup on angular which I dont want to display.

    I have my own logic on how to handle errors on client and I dont want to use this ugly popup

  • User Avatar
    0
    maliming created
    Support Team

    You can implement your own abp.message.error

    https://github.com/aspnetzero/aspnet-zero-core/blob/dev/angular/src/assets/abp-web-resources/abp.sweet-alert.js#L36

  • User Avatar
    0
    maliming created
    Support Team

    https://github.com/aspnetboilerplate/abp-ng2-module/blob/master/src/abpHttpInterceptor.ts#L233

    All requests are intercepted, maybe you can refer to abp's interceptor to implement your own interceptor.

  • User Avatar
    0
    antonis created

    No I dont want that solution which is not convenient at all. I have dozens of CRUD forms. Some of the form needs to handle errors differently.

    I want to get the response back as usual and will handle the errors by my self. This is not a solution for me

  • User Avatar
    0
    antonis created

    It is possible yes but that means that I have to go on all aspnetzero crud forms (users, organization units, roles etc..) and also change their error handling behavior. The way aspnetzero team implemented error handling is not scalable and convenient at all. Their implemenation is problematic as it guides the developer to a specific direction rather than give the freedom to choose whatever is needed based on client requirements. Such a little thing but so important is implemented in a strict way. Shame

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @antonis

    It is not related to client side but it is related to server side actually (related to https://support.aspnetzero.com/QA/Questions/5990).

    I think an exceptinon is thrown even if you use a try-catch block is the problem here.

  • User Avatar
    0
    antonis created

    @ismcagdas I think the problem is on the design of aspnetboilerplate and aspnetzero exception and error handling.

    As I explained earlier:

    It is possible yes but that means that I have to go on all aspnetzero crud forms (users, organization units, roles etc..) and also change their error handling behavior. The way aspnetzero team implemented error handling is not scalable and convenient at all. Their implemenation is problematic as it guides the developer to a specific direction rather than give the freedom to choose whatever is needed based on client requirements. Such a little thing but so important is implemented in a strict way.

  • User Avatar
    0
    aaron created
    Support Team

    Correctness is more important than flexibility.

    try
    {
        using (var uow = unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)) // Add this scope
        {
            await repo.InsertAsync(input);
            await unitOfWorkManager.Current.SaveChangesAsync();
            await uow.CompleteAsync(); // Add this
        }
    }