private async ValueTask <CalendarEntryAttachment> TryCatch( ReturningCalendarEntryAttachmentFunction returningCalendarEntryAttachmentFunction) { try { return(await returningCalendarEntryAttachmentFunction()); } catch (InvalidCalendarEntryAttachmentException invalidCalendarEntryAttachmentInputException) { throw CreateAndLogValidationException(invalidCalendarEntryAttachmentInputException); } catch (NotFoundCalendarEntryAttachmentException notFoundCalendarEntryAttachmentException) { throw CreateAndLogValidationException(notFoundCalendarEntryAttachmentException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedCalendarEntryAttachmentException = new LockedCalendarEntryAttachmentException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedCalendarEntryAttachmentException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { throw CreateAndLogServiceException(exception); } }
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 ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid someAttachmentId = Guid.NewGuid(); Guid someCalendarEntryId = Guid.NewGuid(); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttachmentException = new LockedCalendarEntryAttachmentException(databaseUpdateConcurrencyException); var expectedCalendarEntryAttachmentException = new CalendarEntryAttachmentDependencyException(lockedAttachmentException); this.storageBrokerMock.Setup(broker => broker.SelectCalendarEntryAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <CalendarEntryAttachment> removeCalendarEntryAttachmentTask = this.calendarEntryAttachmentService.RemoveCalendarEntryAttachmentByIdAsync(someCalendarEntryId, someAttachmentId); // then await Assert.ThrowsAsync <CalendarEntryAttachmentDependencyException>(() => 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(); }