Beispiel #1
0
        public async Task ShouldThrowDependencyExceptionOnRetrieveAllIfDependencyApiErrorOccursAndLogItAsync(
            Exception dependencyApiException)
        {
            // given
            var failedStudentDependencyException =
                new FailedStudentDependencyException(dependencyApiException);

            var expectedStudentDependencyException =
                new StudentDependencyException(failedStudentDependencyException);

            this.apiBrokerMock.Setup(broker =>
                                     broker.GetAllStudentsAsync())
            .ThrowsAsync(dependencyApiException);
            // when
            ValueTask <List <Student> > retrievedStudentsTask =
                this.studentService.RetrieveAllStudentsAsync();

            // then
            await Assert.ThrowsAsync <StudentDependencyException>(() =>
                                                                  retrievedStudentsTask.AsTask());

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

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

            this.apiBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
        private async ValueTask <List <Student> > TryCatch(ReturningStudentsFunction returningStudentsFunction)
        {
            try
            {
                return(await returningStudentsFunction());
            }
            catch (HttpRequestException httpRequestException)
            {
                var failedStudentDependencyException =
                    new FailedStudentDependencyException(httpRequestException);

                throw CreateAndLogCriticalDependencyException(failedStudentDependencyException);
            }
            catch (HttpResponseUrlNotFoundException httpResponseUrlNotFoundException)
            {
                var failedStudentDependencyException =
                    new FailedStudentDependencyException(httpResponseUrlNotFoundException);

                throw CreateAndLogCriticalDependencyException(failedStudentDependencyException);
            }
            catch (HttpResponseUnauthorizedException httpResponseUnauthorizedException)
            {
                var failedStudentDependencyException =
                    new FailedStudentDependencyException(httpResponseUnauthorizedException);

                throw CreateAndLogCriticalDependencyException(failedStudentDependencyException);
            }
            catch (HttpResponseException httpResponseException)
            {
                var failedStudentDependencyException =
                    new FailedStudentDependencyException(httpResponseException);

                throw CreateAndLogDependencyException(failedStudentDependencyException);
            }
            catch (Exception serviceException)
            {
                var failedStudentDependencyException =
                    new FailedStudentServiceException(serviceException);

                throw CreateAndLogServiceException(failedStudentDependencyException);
            }
        }