private async ValueTask <CalendarEntryAttachment> TryCatch(
            ReturningCalendarEntryAttachmentFunction returningCalendarEntryAttachmentFunction)
        {
            try
            {
                return(await returningCalendarEntryAttachmentFunction());
            }
            catch (NullCalendarEntryAttachmentException nullCalendarEntryAttachmentException)
            {
                throw CreateAndLogValidationException(nullCalendarEntryAttachmentException);
            }
            catch (InvalidCalendarEntryAttachmentException invalidCalendarEntryAttachmentInputException)
            {
                throw CreateAndLogValidationException(invalidCalendarEntryAttachmentInputException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (NotFoundCalendarEntryAttachmentException notFoundCalendarEntryAttachmentException)
            {
                throw CreateAndLogValidationException(notFoundCalendarEntryAttachmentException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsCalendarEntryAttachmentException =
                    new AlreadyExistsCalendarEntryAttachmentException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsCalendarEntryAttachmentException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidCalendarEntryAttachmentReferenceException =
                    new InvalidCalendarEntryAttachmentReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidCalendarEntryAttachmentReferenceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedCalendarEntryAttachmentException =
                    new LockedCalendarEntryAttachmentException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedCalendarEntryAttachmentException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedCalendarEntryAttachment =
                    new FailedCalendarEntryAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedCalendarEntryAttachment);
            }
        }
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someAttachmentId    = Guid.NewGuid();
            Guid someCalendarEntryId = Guid.NewGuid();
            var  serviceException    = new Exception();

            var failedCalendarEntryAttachmentService =
                new FailedCalendarEntryAttachmentServiceException(serviceException);

            var expectedCalendarEntryAttachmentException =
                new CalendarEntryAttachmentServiceException(failedCalendarEntryAttachmentService);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarEntryAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <CalendarEntryAttachment> removeCalendarEntryAttachmentTask =
                this.calendarEntryAttachmentService.RemoveCalendarEntryAttachmentByIdAsync(
                    someCalendarEntryId,
                    someAttachmentId);

            // then
            await Assert.ThrowsAsync <CalendarEntryAttachmentServiceException>(() =>
                                                                               removeCalendarEntryAttachmentTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCalendarEntryAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()),
                                          Times.Once);

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteCalendarEntryAttachmentAsync(It.IsAny <CalendarEntryAttachment>()),
                                          Times.Never);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private IQueryable <CalendarEntryAttachment> TryCatch(
            ReturningCalendarEntryAttachmentsFunction returningCalendarEntryAttachmentsFunction)
        {
            try
            {
                return(returningCalendarEntryAttachmentsFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedCalendarAttachment =
                    new FailedCalendarEntryAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedCalendarAttachment);
            }
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            CalendarEntryAttachment someCalendarEntryAttachment = CreateRandomCalendarEntryAttachment();
            var serviceException = new Exception();

            var failedCalendarEntryAttachmentServiceException =
                new FailedCalendarEntryAttachmentServiceException(serviceException);

            var expectedCalendarEntryAttachmentServiceException =
                new CalendarEntryAttachmentServiceException(failedCalendarEntryAttachmentServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCalendarEntryAttachmentAsync(It.IsAny <CalendarEntryAttachment>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <CalendarEntryAttachment> addCalendarEntryAttachmentTask =
                this.calendarEntryAttachmentService.AddCalendarEntryAttachmentAsync(someCalendarEntryAttachment);

            // then
            await Assert.ThrowsAsync <CalendarEntryAttachmentServiceException>(() =>
                                                                               addCalendarEntryAttachmentTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCalendarEntryAttachmentAsync(It.IsAny <CalendarEntryAttachment>()),
                                          Times.Once);

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllCalendarEntryAttachmentsWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedCalendarEntryAttachmentService =
                new FailedCalendarEntryAttachmentServiceException(serviceException);

            var expectedCalendarEntryAttachmentServiceException =
                new CalendarEntryAttachmentServiceException(failedCalendarEntryAttachmentService);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllCalendarEntryAttachments())
            .Throws(serviceException);

            // when
            Action retrieveAllCalenderEntryAttachmentAction = () =>
                                                              this.calendarEntryAttachmentService.RetrieveAllCalendarEntryAttachments();

            // then
            Assert.Throws <CalendarEntryAttachmentServiceException>(
                retrieveAllCalenderEntryAttachmentAction);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectAllCalendarEntryAttachments(),
                                          Times.Once);

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }