abp icon indicating copy to clipboard operation
abp copied to clipboard

Automatic Exception Handling issue

Open waleedalward opened this issue 3 years ago • 4 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Description

I just upgraded my app to abp 7.4 and tried to get use of the concurrency mechanism and I have every thing done right and the behavior is working right but I have issue in the case of AbpDbConcurrencyException which is I have cached it successfully at the application layer and I returned my own result (object) to the controller like:

  try
  {
     await Repository.UpdateAsync(entity, true);
  }
   catch (AbpDbConcurrencyException exc)
   { 
       result.SetMessage(L["FailedToUpdateAsTheEntityWasAlreadyUpdatedPleaseRefreshAndTryAgain"]);
       result.Status = false;
       return result;
   }

and the controller return that result like:

  try
  {
      Result res = await testService.UpdateAsync(param);
      return res;
  }
  catch (Exception exc)
  {
      Result result = Result.Instance(HttpContext);
      result.AddException(exc);
      result.Message= L["DataOperationFaild"];
      result.Status = CRUDEnum.Failed;
      return result;
  }

but the client receive the automatic exception handling result not my result. It's very strange from my understanding that the automatic exception handling will engage incase of the exception not handled all the way up to the controller action and out of the action in this case it will be handled and wrapped. it looks like abp is somehow keep the exception alive and when the request is finished it neglect what I have done and process the exception and return the result

Reproduction Steps

first of all I have custom class that wrap any api result and when the AbpDbConcurrencyException occured I catch it as usual and built my own result. TestAbpConcurrency.zip start the attached test app and follow this steps: 1- first call the get API "https://localhost:44326/Depts/StepOneCreateData" to seed initial deptartments 2- second call the get API "https://localhost:44326/Depts/StepTowUpdate" which will try to update the first initial record from step 1 without setting the proper ConcurrencyStamp which will cause an exception of type AbpDbConcurrencyException which is handled using try catch and the string "FailedByAbpDbConcurrencyException" is returned from the service all up to the controller action which return the same result but the UI always receives the abp automatic exception handling result.

Expected behavior

the returned string from the controller action is returned to the api caller (because the exception was watched and handeled by my code and the abp automatic exception handling should not engage in this case

Actual behavior

abp automatic exception handling engage and neglect the controller action returned result and return its own result { "error": { "code": null, "message": "The data you have submitted has already changed by another user/client. Please discard the changes you've done and try from the beginning.", "details": null, "data": null, "validationErrors": null } }

Regression?

No response

Known Workarounds

No response

Version

7.4

User Interface

Angular

Database Provider

EF Core (Default)

Tiered or separate authentication server

Separate Auth Server

Operation System

Windows (Default)

Other information

No response

waleedalward avatar Nov 11 '23 10:11 waleedalward

Reproduction Steps

Please include minimal steps to reproduce the problem if possible. E.g.: the smallest possible code snippet; or a small project, with steps to run it. If possible include text as text rather than screenshots (so it shows up in searches).

realLiangshiwei avatar Nov 13 '23 08:11 realLiangshiwei

as @realLiangshiwei said, please share minimal and simple steps to reproduce, also share code with code fonts/screenshot. thanks

tarizshahid avatar Nov 15 '23 13:11 tarizshahid

You can try:

try
{
    Dept dept = new Dept(1) //id one as example
    {
        Name = "Test dept",
    };
    await _repository.UpdateAsync(dept, autoSave: true);
    return "success";
}
catch (AbpDbConcurrencyException exc)
{
    await UnitOfWorkManager.Current.RollbackAsync();
    return "FailedByAbpDbConcurrencyException"; // this result expected to go to the ui ;
}

Or update in a new Uow:

try
{
    using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: UnitOfWorkManager.Current?.Options.IsTransactional ?? false))
    {
        Dept dept = new Dept(1) //id one as example
        {
            Name = "Test dept",
        };
        await _repository.UpdateAsync(dept);
        await uow.CompleteAsync();
        return "success";
    }
    
}
catch (AbpDbConcurrencyException exc)
{
    return "FailedByAbpDbConcurrencyException"; // this result expected to go to the ui ;
}

realLiangshiwei avatar Nov 16 '23 07:11 realLiangshiwei

@realLiangshiwei that solved the issue. thanks for your kind support.

waleedalward avatar Nov 22 '23 20:11 waleedalward