private async ValueTask <ExamAttachment> TryCatch( ReturningExamAttachmentFunction returningExamAttachmentFunction) { try { return(await returningExamAttachmentFunction()); } catch (NullExamAttachmentException nullExamAttachmentException) { throw CreateAndLogValidationException(nullExamAttachmentException); } catch (InvalidExamAttachmentException invalidExamAttachmentInputException) { throw CreateAndLogValidationException(invalidExamAttachmentInputException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (NotFoundExamAttachmentException notFoundExamAttachmentException) { throw CreateAndLogValidationException(notFoundExamAttachmentException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsExamAttachmentException = new AlreadyExistsExamAttachmentException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsExamAttachmentException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidExamAttachmentReferenceException = new InvalidExamAttachmentReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidExamAttachmentReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedExamAttachmentException = new LockedExamAttachmentException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedExamAttachmentException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedExamAttachmentServiceException = new FailedExamAttachmentServiceException(exception); throw CreateAndLogServiceException(failedExamAttachmentServiceException); } }
public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid someAttachmentId = Guid.NewGuid(); Guid someExamId = Guid.NewGuid(); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttachmentException = new LockedExamAttachmentException(databaseUpdateConcurrencyException); var expectedExamAttachmentException = new ExamAttachmentDependencyException(lockedAttachmentException); this.storageBrokerMock.Setup(broker => broker.SelectExamAttachmentByIdAsync(someExamId, someAttachmentId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <ExamAttachment> removeExamAttachmentTask = this.examAttachmentService.RemoveExamAttachmentByIdAsync(someExamId, someAttachmentId); // then await Assert.ThrowsAsync <ExamAttachmentDependencyException>(() => removeExamAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectExamAttachmentByIdAsync(someExamId, someAttachmentId), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamAttachmentException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteExamAttachmentAsync(It.IsAny <ExamAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }