Base solution for your next web application
Open Closed

Error handling using InsertAsync,DeleteAsync #5407


User avatar
0
pratiksa created

I am creating new record using "InsertAsync" using asp.net zero tool. But i am getting an error message "An internal error occurred during your request" on UI and i want to show an exact error message on UI. I tried with Try-Catch but it is not caching any error message. So how to get the exact error message from "InsertAsync".

Same issue is getting during Update and DeleteAsync

Below are my project details-

Project Details: i) Created Project Structure using Asp.Net Zero tool ii) Using Asp.Net Core iii) UI Angular-5


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

    There will be some logs when "An internal error occurred during your request".

    Please take a look at the application's log.

  • User Avatar
    0
    alper created
    Support Team
    try
    {
      
    	//db operations
    
    }
    catch (DbUpdateException e)
    {
    	SqlException s = e.InnerException.InnerException as SqlException;
    	//handle and see your exception
    }
    
  • User Avatar
    0
    pratiksa created

    <cite>alper: </cite>

    I tried this but not working.
    
    
         try
               {
    
                   
               }
               catch (DbUpdateException e)
               {
                   SqlException s = e.InnerException.InnerException as SqlException;
                   //handle and see your exception
               } 
    
    }
    
  • User Avatar
    0
    pratiksa created

    <cite>maliming: </cite>

    Hi, I want to show an exact error message on UI . I know the reason why insertion is failing but not getting any error message from InsertAsync.

  • User Avatar
    0
    maliming created
    Support Team

    Call CurrentUnitOfWork.SaveChangesAsync() to catch exceptions

    try
    {
      
       //db operations.
       CurrentUnitOfWork.SaveChangesAsync()
    
    }
    catch (DbUpdateException e)
    {
       SqlException s = e.InnerException.InnerException as SqlException;
       //handle and see your exception
    }
    

    These are the database error messages, I think should not be displayed to the UI. The application should avoid this error. For example, do parameter verification and supplement unit testing.

    Even if this kind of message is displayed to the UI, sometimes it does not understand what is going on.

  • User Avatar
    0
    pratiksa created

    <cite>maliming: </cite> Call CurrentUnitOfWork.SaveChangesAsync() to catch exceptions

    try
    {
     
      //db operations.
      CurrentUnitOfWork.SaveChangesAsync()
    
    }
    catch (DbUpdateException e)
    {
      SqlException s = e.InnerException.InnerException as SqlException;
      //handle and see your exception
    }
    

    These are the database error messages, I think should not be displayed to the UI. The application should avoid this error. For example, do parameter verification and supplement unit testing.

    Even if this kind of message is displayed to the UI, sometimes it does not understand what is going on.

    I tried but it is not working.

    Actually have added the unique constraints in my database and I want to check the validation message for duplicate field. So i am trying to catch the error message and sent it to UI by editing in error message so user can easily understand.

  • User Avatar
    0
    maliming created
    Support Team
    await CurrentUnitOfWork.SaveChangesAsync()
    

    You have to remember to add await

    Still can't catch the exception?

  • User Avatar
    0
    maliming created
    Support Team

    You should check in the application whether the field is unique in the database. ([https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Zero/Authorization/Users/AbpUserManager.cs#L396])) Instead of waiting for the database to return an exception message.

    The database exception message is "unreadable" to the user. Non-professionals may not understand the database error message.

  • User Avatar
    0
    alper created
    Support Team

    Hi,

    If you know the exception then throw a UserFriendlyException so that users will see a dialog with an error message.

    throw new UserFriendlyException("Ooppps! There is a problem!", "You are trying to see a product that is deleted...");
    
  • User Avatar
    0
    alper created
    Support Team

    As an alternative; if you want to send all the exception messages to the clients.

    Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
    

    See #5152@598db304-d2cd-42bb-936c-fb1b1cce9f94

  • User Avatar
    0
    pratiksa created

    Thanks it is working.