private async ValueTask <GuardianAttachment> TryCatch( ReturningGuardianAttachmentFunction returningGuardianAttachmentFunction) { try { return(await returningGuardianAttachmentFunction()); } catch (NullGuardianAttachmentException nullGuardianAttachmentException) { throw CreateAndLogValidationException(nullGuardianAttachmentException); } catch (InvalidGuardianAttachmentException invalidGuardianAttachmentInputException) { throw CreateAndLogValidationException(invalidGuardianAttachmentInputException); } catch (NotFoundGuardianAttachmentException notFoundGuardianAttachmentException) { throw CreateAndLogValidationException(notFoundGuardianAttachmentException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsGuardianAttachmentException = new AlreadyExistsGuardianAttachmentException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsGuardianAttachmentException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidGuardianAttachmentReferenceException = new InvalidGuardianAttachmentReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidGuardianAttachmentReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedGuardianAttachmentException = new LockedGuardianAttachmentException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedGuardianAttachmentException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedGuardianAttachmentServiceException = new FailedGuardianAttachmentServiceException(exception); throw CreateAndLogServiceException(failedGuardianAttachmentServiceException); } }
public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given var randomAttachmentId = Guid.NewGuid(); var randomGuardianId = Guid.NewGuid(); Guid someAttachmentId = randomAttachmentId; Guid someGuardianId = randomGuardianId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttachmentException = new LockedGuardianAttachmentException(databaseUpdateConcurrencyException); var expectedGuardianAttachmentException = new GuardianAttachmentDependencyException(lockedAttachmentException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianAttachmentByIdAsync(someGuardianId, someAttachmentId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <GuardianAttachment> removeGuardianAttachmentTask = this.guardianAttachmentService.RemoveGuardianAttachmentByIdAsync(someGuardianId, someAttachmentId); // then await Assert.ThrowsAsync <GuardianAttachmentDependencyException>(() => removeGuardianAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianAttachmentException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianAttachmentByIdAsync(someGuardianId, someAttachmentId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteGuardianAttachmentAsync(It.IsAny <GuardianAttachment>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }