I want to insert a log into the database and throw a UserFriendlyException but it doesn't work as I expect
if (isTodayOpen) //today open but time not valid
{
var logMessage = string.Format("The door cannot be unlocked. Your booking is not yet in effect. It is valid from {0} to {1}. Please try again later.", application.Rules.TimeFrom.ToString(), application.Rules.TimeTo.ToString());
var openDoorLogDetails = new OpenDoorLog();
openDoorLogDetails.ApplicantId = application.User.Id;
openDoorLogDetails.OpenDoorTime = DateTime.Now;
openDoorLogDetails.isOpenSuccessful = false;
openDoorLogDetails.LogMessage = logMessage;
await _openDoorLogRepository.InsertAsync(openDoorLogDetails);
await CurrentUnitOfWork.SaveChangesAsync();
throw new UserFriendlyException(logMessage);
}
what I get on Swagger
{ "result": null, "targetUrl": null, "success": false, "error": { "code": 0, "message": "The door cannot be unlocked. Your booking is not yet in effect. It is valid from 09:00:00 to 10:00:00. Please try again later.", "details": null, "validationErrors": null }, "unAuthorizedRequest": false, "__abp": true }
even though I use the CurrentUnitOfWork.SaveChanges(); it only throws the exception but the log doesn't insert into a database. My expected result is the log can be inserted into the database and can throw UserFriendlyException to the user.
When you want to work with async operation in repository you need to write save changes as below: