private async ValueTask <StudentRegistration> TryCatch( ReturningStudentRegistrationFunction returningStudentRegistrationFunction) { try { return(await returningStudentRegistrationFunction()); } catch (NullStudentRegistrationException nullStudentRegistrationException) { throw CreateAndLogValidationException(nullStudentRegistrationException); } catch (InvalidStudentRegistrationException invalidStudentRegistrationException) { throw CreateAndLogValidationException(invalidStudentRegistrationException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (NotFoundStudentRegistrationException nullStudentRegistrationException) { throw CreateAndLogValidationException(nullStudentRegistrationException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsStudentRegistrationException = new AlreadyExistsStudentRegistrationException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsStudentRegistrationException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedStudentRegistrationException = new LockedStudentRegistrationException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedStudentRegistrationException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidStudentRegistrationReferenceException = new InvalidStudentRegistrationReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidStudentRegistrationReferenceException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentRegistrationServiceException = new FailedStudentRegistrationServiceException(exception); throw CreateAndLogServiceException(failedStudentRegistrationServiceException); } }
public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync() { // given StudentRegistration randomStudentRegistration = CreateRandomStudentRegistration(); StudentRegistration someStudentRegistration = randomStudentRegistration; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage); var invalidStudentRegistrationReferenceException = new InvalidStudentRegistrationReferenceException(foreignKeyConstraintConflictException); var expectedStudentRegistrationValidationException = new StudentRegistrationValidationException(invalidStudentRegistrationReferenceException); this.storageBrokerMock.Setup(broker => broker.InsertStudentRegistrationAsync(someStudentRegistration)) .ThrowsAsync(foreignKeyConstraintConflictException); // when ValueTask <StudentRegistration> addStudentRegistrationTask = this.studentRegistrationService.AddStudentRegistrationAsync(someStudentRegistration); // then await Assert.ThrowsAsync <StudentRegistrationValidationException>(() => addStudentRegistrationTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.InsertStudentRegistrationAsync(It.IsAny <StudentRegistration>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentRegistrationValidationException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }