private IQueryable <Course> TryCatch(ReturningQueryableCourseFunction returningQueryableCourseFunction) { try { return(returningQueryableCourseFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsCourseException = new AlreadyExistsCourseException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsCourseException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedCourseException = new LockedCourseException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { throw CreateAndLogServiceException(exception); } }
public async void ShouldThrowValidationExceptionOnCreateWhenCourseAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Course randomCourse = CreateRandomCourse(dateTime); Course alreadyExistsCourse = randomCourse; alreadyExistsCourse.UpdatedBy = alreadyExistsCourse.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsCourseException = new AlreadyExistsCourseException(duplicateKeyException); var expectedCourseValidationException = new CourseValidationException(alreadyExistsCourseException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertCourseAsync(alreadyExistsCourse)) .ThrowsAsync(duplicateKeyException); // when ValueTask <Course> createCourseTask = this.courseService.CreateCourseAsync(alreadyExistsCourse); // then await Assert.ThrowsAsync <CourseValidationException>(() => createCourseTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCourseAsync(alreadyExistsCourse), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <Course> TryCatch(ReturningCourseFunction returningCourseFunction) { try { return(await returningCourseFunction()); } catch (NullCourseException nullCourseException) { throw CreateAndLogValidationException(nullCourseException); } catch (InvalidCourseException invalidCourseException) { throw CreateAndLogValidationException(invalidCourseException); } catch (NotFoundCourseException nullCourseException) { throw CreateAndLogValidationException(nullCourseException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsCourseException = new AlreadyExistsCourseException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsCourseException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedCourseException = new LockedCourseException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedCourseException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedCourseServiceException = new FailedCourseServiceException(exception); throw CreateAndLogServiceException(failedCourseServiceException); } }