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 void ShouldThrowValidationExceptionOnAddWhenCalendarEntryAttachmentAlreadyExistsAndLogItAsync() { // given CalendarEntryAttachment randomCalendarEntryAttachment = CreateRandomCalendarEntryAttachment(); CalendarEntryAttachment alreadyExistsCalendarEntryAttachment = randomCalendarEntryAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsCalendarEntryAttachmentException = new AlreadyExistsCalendarEntryAttachmentException(duplicateKeyException); var expectedCalendarEntryAttachmentValidationException = new CalendarEntryAttachmentValidationException(alreadyExistsCalendarEntryAttachmentException); this.storageBrokerMock.Setup(broker => broker.InsertCalendarEntryAttachmentAsync(alreadyExistsCalendarEntryAttachment)) .ThrowsAsync(duplicateKeyException); // when ValueTask <CalendarEntryAttachment> addCalendarEntryAttachmentTask = this.calendarEntryAttachmentService.AddCalendarEntryAttachmentAsync(alreadyExistsCalendarEntryAttachment); // then await Assert.ThrowsAsync <CalendarEntryAttachmentValidationException>(() => addCalendarEntryAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.InsertCalendarEntryAttachmentAsync(It.IsAny <CalendarEntryAttachment>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCalendarEntryAttachmentValidationException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }