Base solution for your next web application
Open Closed

Passing UserFriendlyException messages to ModalManager? #7208


User avatar
0
rucksackdigital created

Hello,

running v7.0 with MVC/jQuery. I've read through the ABP docs on exception handling and understand the basic premise but am struggling to find a solution to effectively showing errors other than the standard "An Internal Error Has Occurred."

As an example scenario, if I use the RadTool to scaffold a new entity, FooBar, with the following methods in my Application Service:

          [AbpAuthorize(AppPermissions.Pages_FooBars_Edit)]
		 private async Task Update(CreateOrEditFooBarDto input)
         {
            throw new UserFriendlyException("This is an Update Exception");
            var fooBar = await _fooBarRepository.FirstOrDefaultAsync((int)input.Id);
             ObjectMapper.Map(input, fooBar);
         }
         
          [AbpAuthorize(AppPermissions.Pages_FooBars_Create)]
		 private async Task Create(CreateOrEditFooBarDto input)
         {
            throw new UserFriendlyException("This is a test exception");
            var fooBar = ObjectMapper.Map<FooBar>(input);

			
			if (AbpSession.TenantId != null)
			{
				fooBar.TenantId = (int?) AbpSession.TenantId;
			}
		

            await _fooBarRepository.InsertAsync(fooBar);
         }

		 [AbpAuthorize(AppPermissions.Pages_FooBars_Delete)]
         public async Task Delete(EntityDto input)
         {
            throw new UserFriendlyException("This is a Deletion Exception");
            await _fooBarRepository.DeleteAsync(input.Id);
         } 
         
         [AbpAuthorize(AppPermissions.Pages_FooBars_Edit)]
		 public async Task<GetFooBarForEditOutput> GetFooBarForEdit(EntityDto input)
         {
            throw new UserFriendlyException("This is an Edit Exception");
            var fooBar = await _fooBarRepository.FirstOrDefaultAsync(input.Id);
           
		    var output = new GetFooBarForEditOutput {FooBar = ObjectMapper.Map<CreateOrEditFooBarDto>(fooBar)};
			
            return output;
         }

A sample snippet from the js:

{
                            text: app.localize('Edit'),
                            visible: function () {
                                return _permissions.edit;
                            },
                            action: function (data) {
                                _createOrEditModal.open({ id: data.record.fooBar.id });
                            }
                        }, 
						{
                            text: app.localize('Delete'),
                            visible: function () {
                                return _permissions.delete;
                            },
                            action: function (data) {
                                deleteFooBar(data.record.fooBar);
                            }
                        }]

Any service calls that return ajax, work as expected; attempting to Delete, Update, or Create shows my user-friendly error message, as expected. Calls that expect a View or a PartialView, such as _createOrEditModal.open({ id: data.record.fooBar.id }); return the generic "internal error" message.

So, is there a built-in way to catch UserFriendlyExceptions in this manner? I'm not sure if the framework supports returning either json or text/html, and if the modal manager is capable of catching that.

Or - is it something where I need to have a separate service I call before opening the modal, to perform whatever pre-edit validations I am looking to perform?


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @dnard82,

    Do you get the same behaviour if you run your app in release mode ?

    Thanks.

  • User Avatar
    0
    rucksackdigital created

    @ismcagdas I do, yes. Which I think is the expected behavior, based on https://aspnetboilerplate.com/Pages/Documents/MVC-Controllers#exception-handling-result-wrapping if I'm reading it correctly. I just wasn't sure if ASPNZ had something baked in that already handled this sort of overriding, or if I need to homebrew something.

  • User Avatar
    0
    ryancyq created
    Support Team

    Calls that expect a View or a PartialView, such as _createOrEditModal.open({ id: data.record.fooBar.id }); return the generic "internal error" message.

    Is the UserFriendlyException thrown in a method that returning ViewResult?

    https://aspnetboilerplate.com/Pages/Documents/MVC-Controllers#exception-handling-result-wrapping Abp by default does not wrap any exception other than JsonResult.

  • User Avatar
    0
    rucksackdigital created

    @ryancyq, thanks for your reply.

    The solution I came up with this morning, which works but is not the most elegant -- I've modified ModalManager.js to look for the existence of either an UnauthorizedException or UserFriendlyException in the html response. If found, I parse out the message and display it, rather than the default InternalServerError.

  • User Avatar
    0
    ryancyq created
    Support Team

    @dnard82

    you should consider try catching the exception in the method that returns ViewResult. That is a more appropriate way to handle it.