private async ValueTask <ExamFee> TryCatch( ReturningExamFeeFunction returningExamFeeFunction) { try { return(await returningExamFeeFunction()); } catch (NullExamFeeException nullExamFeeException) { throw CreateAndLogValidationException(nullExamFeeException); } catch (InvalidExamFeeException invalidExamFeeInputException) { throw CreateAndLogValidationException(invalidExamFeeInputException); } catch (NotFoundExamFeeException nullExamFeeException) { throw CreateAndLogValidationException(nullExamFeeException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsExamFeeException = new AlreadyExistsExamFeeException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsExamFeeException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidExamFeeReferenceException = new InvalidExamFeeReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidExamFeeReferenceException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedExamFeeException = new LockedExamFeeException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedExamFeeException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedExamFeeServiceException = new FailedExamFeeServiceException(exception); throw CreateAndLogServiceException(failedExamFeeServiceException); } }
public async void ShouldThrowValidationExceptionOnCreateWhenExamFeeAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); ExamFee randomExamFee = CreateRandomExamFee(dateTime); ExamFee alreadyExistsExamFee = randomExamFee; alreadyExistsExamFee.UpdatedBy = alreadyExistsExamFee.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsExamFeeException = new AlreadyExistsExamFeeException(duplicateKeyException); var expectedExamFeeValidationException = new ExamFeeValidationException(alreadyExistsExamFeeException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertExamFeeAsync(alreadyExistsExamFee)) .ThrowsAsync(duplicateKeyException); // when ValueTask <ExamFee> createExamFeeTask = this.examFeeService.AddExamFeeAsync(alreadyExistsExamFee); // then await Assert.ThrowsAsync <ExamFeeValidationException>(() => createExamFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertExamFeeAsync(alreadyExistsExamFee), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamFeeValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }