Open Closed

New Exception Type #4378


0
mamak created

I want to handle new exception types, as done for AbpAuthorizationException, AbpValidationException ...

I mean, for ex when an exception about delete conflict occurs, i want to handle it and show specific message to user. (for ex: "You cant delete this record. Because it has been used") But now this delete conflict hasn't been handled, default message is shown to the user.

How can i do this?


7 Answer(s)
  • 0
    hikalkan created
    Support Team

    Hi,

    First, you should handle the conflict error. You have 2 options:

    A) Check it manually. B) Handle SQL exception (or related exception) in your code. In that case, you should use CurrentUnitOfWork.SaveChanges() in order to apply query to database.

    Then you can throw a UserFriendlyException. If you don't like UserFriendlyException, then you have 2 options:

    A) Create a custom exception class derived from UserFriendlyException. This is easier. ABP will handle the rest. B) Create a custom exception class not related to UserFriendlyException. In that case, you should replace IExceptionToErrorInfoConverter by your own implementation. Default implementation is like that: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Web.Common/Web/Models/DefaultErrorInfoConverter.cs">https://github.com/aspnetboilerplate/as ... nverter.cs</a>

    So, basic implementation can be something like that:

    try { ... your code that performs db operations... await CurrentUnitOfWork.SaveChangesAsync(); //or CurrentUnitOfWork.SaveChanges();
    } catch(SqlException ex) //SqlException or whatever exception it throws, I don't know exactly { throw new UserFriendlyException("You cant delete this record. Because it has been used"); }

  • 0
    bbakermmc created

    If you don't want to wrap everything in a try catch, you should be able to modify this code sample to fit your needs.

    <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/issues/711">https://github.com/aspnetzero/aspnet-ze ... issues/711</a>

  • 0
    mamak created

    try catch and UserFriendlyException method is ok. But as BBakerMMC said, we must wrap everything in tyr catch.

    So i tried new exception filter attribute. (we are working with mvc, not mvc core) I added this new class to Application Project. Bu when i debug, i saw that not enter OnException method.

  • 0
    ismcagdas created
    Support Team

    Hi @mamak,

    In ASP.NET MVC 5.x, it is handled in base controller, here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Web.Mvc/Web/Mvc/Controllers/AbpController.cs">https://github.com/aspnetboilerplate/as ... troller.cs</a>.

    So, you can override OnException method of your base controller and handle it there.

  • 0
    mamak created

    Thanks worked.

    So no filter attribute class added, only overrided OnException method. ;)

  • 0
    mamak created

    But if i call service directly fron view doesnt work since onexception method overrided in base controller.

  • 0
    ismcagdas created
    Support Team

    Hi @mamak,

    For that case, you can use this suggestion <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/issues/711#issuecomment-355491608">https://github.com/aspnetzero/aspnet-ze ... -355491608</a>.