private async ValueTask <StudentAttachment> TryCatch( ReturningStudentAttachmentFunction returningStudentAttachmentFunction) { try { return(await returningStudentAttachmentFunction()); } catch (NullStudentAttachmentException nullStudentAttachmentInputException) { throw CreateAndLogValidationException(nullStudentAttachmentInputException); } catch (InvalidStudentAttachmentException invalidStudentAttachmentInputException) { throw CreateAndLogValidationException(invalidStudentAttachmentInputException); } catch (NotFoundStudentAttachmentException notFoundStudentAttachmentException) { throw CreateAndLogValidationException(notFoundStudentAttachmentException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsStudentAttachmentException = new AlreadyExistsStudentAttachmentException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsStudentAttachmentException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidStudentAttachmentReferenceException = new InvalidStudentAttachmentReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidStudentAttachmentReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedAttachmentException = new LockedStudentAttachmentException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedAttachmentException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentAttachmentServiceException = new FailedStudentAttachmentServiceException(exception); throw CreateAndLogServiceException(failedStudentAttachmentServiceException); } }
public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given var randomAttachmentId = Guid.NewGuid(); var randomStudentId = Guid.NewGuid(); Guid someAttachmentId = randomAttachmentId; Guid someStudentId = randomStudentId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttachmentException = new LockedStudentAttachmentException(databaseUpdateConcurrencyException); var expectedStudentAttachmentException = new StudentAttachmentDependencyException(lockedAttachmentException); this.storageBrokerMock.Setup(broker => broker.SelectStudentAttachmentByIdAsync(someStudentId, someAttachmentId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <StudentAttachment> removeStudentAttachmentTask = this.studentAttachmentService.RemoveStudentAttachmentByIdAsync(someStudentId, someAttachmentId); // then await Assert.ThrowsAsync <StudentAttachmentDependencyException>(() => removeStudentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentAttachmentException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentAttachmentByIdAsync(someStudentId, someAttachmentId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteStudentAttachmentAsync(It.IsAny <StudentAttachment>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }