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 ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync() { // given var randomAttachmentId = Guid.NewGuid(); var randomGuardianId = Guid.NewGuid(); Guid someAttachmentId = randomAttachmentId; Guid someGuardianId = randomGuardianId; var serviceException = new Exception(); var failedGuardianAttachmentServiceException = new FailedGuardianAttachmentServiceException(serviceException); var expectedGuardianAttachmentException = new GuardianAttachmentServiceException(failedGuardianAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianAttachmentByIdAsync(someGuardianId, someAttachmentId)) .ThrowsAsync(serviceException); // when ValueTask <GuardianAttachment> removeGuardianAttachmentTask = this.guardianAttachmentService.RemoveGuardianAttachmentByIdAsync( someGuardianId, someAttachmentId); // then await Assert.ThrowsAsync <GuardianAttachmentServiceException>(() => 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(); }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given GuardianAttachment randomGuardianAttachment = CreateRandomGuardianAttachment(); GuardianAttachment someGuardianAttachment = randomGuardianAttachment; var serviceException = new Exception(); var failedGuardianAttachmentServiceException = new FailedGuardianAttachmentServiceException(serviceException); var expectedGuardianAttachmentServiceException = new GuardianAttachmentServiceException(failedGuardianAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.InsertGuardianAttachmentAsync(someGuardianAttachment)) .ThrowsAsync(serviceException); // when ValueTask <GuardianAttachment> addGuardianAttachmentTask = this.guardianAttachmentService.AddGuardianAttachmentAsync(someGuardianAttachment); // then await Assert.ThrowsAsync <GuardianAttachmentServiceException>(() => addGuardianAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianAttachmentServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAttachmentAsync(someGuardianAttachment), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }