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 ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); ExamFee randomExamFee = CreateRandomExamFee(dateTime); randomExamFee.UpdatedBy = randomExamFee.CreatedBy; randomExamFee.UpdatedDate = randomExamFee.CreatedDate; ExamFee invalidExamFee = randomExamFee; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage); var invalidExamFeeReferenceException = new InvalidExamFeeReferenceException(foreignKeyConstraintConflictException); var expectedExamFeeValidationException = new ExamFeeValidationException(invalidExamFeeReferenceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertExamFeeAsync(invalidExamFee)) .ThrowsAsync(foreignKeyConstraintConflictException); // when ValueTask <ExamFee> addExamFeeTask = this.examFeeService.AddExamFeeAsync(invalidExamFee); // then await Assert.ThrowsAsync <ExamFeeValidationException>(() => addExamFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertExamFeeAsync(invalidExamFee), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamFeeValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }