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 ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync() { // given Guid someAttachmentId = Guid.NewGuid(); Guid someExamId = Guid.NewGuid(); var serviceException = new Exception(); var failedExamAttachmentServiceException = new FailedExamAttachmentServiceException(serviceException); var expectedExamAttachmentException = new ExamAttachmentServiceException(failedExamAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectExamAttachmentByIdAsync(someExamId, someAttachmentId)) .ThrowsAsync(serviceException); // when ValueTask <ExamAttachment> removeExamAttachmentTask = this.examAttachmentService.RemoveExamAttachmentByIdAsync( someExamId, someAttachmentId); // then await Assert.ThrowsAsync <ExamAttachmentServiceException>(() => 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(); }
private IQueryable <ExamAttachment> TryCatch( ReturningExamAttachmentsFunction returningExamAttachmentsFunction) { try { return(returningExamAttachmentsFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (Exception exception) { var failedExamAttachmentServiceException = new FailedExamAttachmentServiceException(exception); throw CreateAndLogServiceException(failedExamAttachmentServiceException); } }
public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync() { // given ExamAttachment randomExamAttachment = CreateRandomExamAttachment(); ExamAttachment someExamAttachment = randomExamAttachment; var serviceException = new Exception(); var failedExamAttachmentServiceException = new FailedExamAttachmentServiceException(serviceException); var expectedExamAttachmentServiceException = new ExamAttachmentServiceException(failedExamAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.InsertExamAttachmentAsync(someExamAttachment)) .ThrowsAsync(serviceException); // when ValueTask <ExamAttachment> addExamAttachmentTask = this.examAttachmentService.AddExamAttachmentAsync(someExamAttachment); // then await Assert.ThrowsAsync <ExamAttachmentServiceException>(() => addExamAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamAttachmentServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertExamAttachmentAsync(someExamAttachment), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowServiceExceptionOnRetrieveAllExamAttachmentsWhenExceptionOccursAndLogIt() { // given var serviceException = new Exception(); var failedExamAttachmentServiceException = new FailedExamAttachmentServiceException(serviceException); var expectedExamAttachmentServiceException = new ExamAttachmentServiceException(failedExamAttachmentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllExamAttachments()) .Throws(serviceException); // when Action retrieveAllExamAttachmentsAction = () => this.examAttachmentService.RetrieveAllExamAttachments(); // then Assert.Throws <ExamAttachmentServiceException>( retrieveAllExamAttachmentsAction); this.storageBrokerMock.Verify(broker => broker.SelectAllExamAttachments(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedExamAttachmentServiceException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }