Abhijeet
Abhijeet

Reputation: 13906

Suppress error from Unit of Work transaction

I'm trying to suppress error in code, but MVC action still returns "500 internal server error".

What events are fired in ASP.NET Boilerplate framework after action returns?

public async Task<IActionResult> Post([FromBody]PaymentViewModel model)
{
    var result = false;

    // Storing of card must pass
    try
    {
        // ...
    }
    catch (Exception ex)
    {
        // Catch business exception, but storing
    }

    return Json(new { result });
}

Upvotes: 1

Views: 616

Answers (1)

aaron
aaron

Reputation: 43098

To recover in a catch block, begin a UnitOfWork with RequiresNew:

public async Task<IActionResult> Post([FromBody]PaymentViewModel model)
{
    var result = false;

    // Storing of card must pass
    try
    {
        using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
        {
            // ...

            await CurrentUnitOfWork.SaveChangesAsync();
            await uow.CompleteAsync();
        }
    }
    catch (Exception ex)
    {
        // Catch business exception, but storing
    }

    return Json(new { result });
}

Further explanation: aspnetboilerplate/aspnetboilerplate#2732 (comment)

Upvotes: 2

Related Questions