public async Task ShouldThrowDependencyExceptionOnRetrieveAllIfDependencyApiErrorOccursAndLogItAsync(
            Exception dependencyApiException)
        {
            // given
            var failedCourseDependencyException =
                new FailedCourseDependencyException(dependencyApiException);

            var expectedCourseDependencyException =
                new CourseDependencyException(failedCourseDependencyException);

            this.apiBrokerMock.Setup(broker =>
                                     broker.GetAllCoursesAsync())
            .ThrowsAsync(dependencyApiException);
            // when
            ValueTask <List <Course> > retrieveAllCoursesTask =
                this.courseService.RetrieveAllCoursesAsync();

            // then
            await Assert.ThrowsAsync <CourseDependencyException>(() =>
                                                                 retrieveAllCoursesTask.AsTask());

            this.apiBrokerMock.Verify(broker =>
                                      broker.GetAllCoursesAsync(),
                                      Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedCourseDependencyException))),
                                          Times.Once);

            this.apiBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemplo n.º 2
0
        private async ValueTask <List <Course> > TryCatch(ReturningCoursesFunction returningCoursesFunction)
        {
            try
            {
                return(await returningCoursesFunction());
            }
            catch (HttpRequestException httpRequestException)
            {
                var failedCourseDependencyException =
                    new FailedCourseDependencyException(httpRequestException);

                throw CreateAndLogCriticalDependencyException(failedCourseDependencyException);
            }
            catch (HttpResponseUrlNotFoundException httpResponseUrlNotFoundException)
            {
                var failedCourseDependencyException =
                    new FailedCourseDependencyException(httpResponseUrlNotFoundException);

                throw CreateAndLogCriticalDependencyException(failedCourseDependencyException);
            }
            catch (HttpResponseUnauthorizedException httpResponseUnauthorizedException)
            {
                var failedCourseDependencyException =
                    new FailedCourseDependencyException(httpResponseUnauthorizedException);

                throw CreateAndLogCriticalDependencyException(failedCourseDependencyException);
            }
            catch (HttpResponseException httpResponseException)
            {
                var failedCourseDependencyException =
                    new FailedCourseDependencyException(httpResponseException);

                throw CreateAndLogDependencyException(failedCourseDependencyException);
            }
            catch (Exception serviceException)
            {
                var failedCourseServiceException =
                    new FailedCourseServiceException(serviceException);

                throw CreateAndLogServiceException(failedCourseServiceException);
            }
        }