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 void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync() { // given GuardianAttachment randomGuardianAttachment = CreateRandomGuardianAttachment(); GuardianAttachment invalidGuardianAttachment = randomGuardianAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage); var invalidGuardianAttachmentReferenceException = new InvalidGuardianAttachmentReferenceException(foreignKeyConstraintConflictException); var expectedGuardianAttachmentValidationException = new GuardianAttachmentValidationException(invalidGuardianAttachmentReferenceException); this.storageBrokerMock.Setup(broker => broker.InsertGuardianAttachmentAsync(invalidGuardianAttachment)) .ThrowsAsync(foreignKeyConstraintConflictException); // when ValueTask <GuardianAttachment> addGuardianAttachmentTask = this.guardianAttachmentService.AddGuardianAttachmentAsync(invalidGuardianAttachment); // then await Assert.ThrowsAsync <GuardianAttachmentValidationException>(() => addGuardianAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAttachmentAsync(invalidGuardianAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }