private async ValueTask <TeacherAttachment> TryCatch( ReturningTeacherAttachmentFunction returningTeacherAttachmentFunction) { try { return(await returningTeacherAttachmentFunction()); } catch (NullTeacherAttachmentException nullTeacherAttachmentInputException) { throw CreateAndLogValidationException(nullTeacherAttachmentInputException); } catch (InvalidTeacherAttachmentException invalidTeacherAttachmentInputException) { throw CreateAndLogValidationException(invalidTeacherAttachmentInputException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (NotFoundTeacherAttachmentException notFoundTeacherAttachmentException) { throw CreateAndLogValidationException(notFoundTeacherAttachmentException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsTeacherAttachmentException = new AlreadyExistsTeacherAttachmentException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsTeacherAttachmentException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidTeacherAttachmentReferenceException = new InvalidTeacherAttachmentReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidTeacherAttachmentReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedTeacherAttachmentException = new LockedTeacherAttachmentException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedTeacherAttachmentException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedTeacherAttachmentServiceException = new FailedTeacherAttachmentServiceException(exception); throw CreateAndLogServiceException(failedTeacherAttachmentServiceException); } }
public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync() { // given TeacherAttachment randomTeacherAttachment = CreateRandomTeacherAttachment(); TeacherAttachment invalidTeacherAttachment = randomTeacherAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage); var invalidTeacherAttachmentReferenceException = new InvalidTeacherAttachmentReferenceException(foreignKeyConstraintConflictException); var expectedTeacherAttachmentValidationException = new TeacherAttachmentValidationException(invalidTeacherAttachmentReferenceException); this.storageBrokerMock.Setup(broker => broker.InsertTeacherAttachmentAsync(invalidTeacherAttachment)) .ThrowsAsync(foreignKeyConstraintConflictException); // when ValueTask <TeacherAttachment> addTeacherAttachmentTask = this.teacherAttachmentService.AddTeacherAttachmentAsync(invalidTeacherAttachment); // then await Assert.ThrowsAsync <TeacherAttachmentValidationException>(() => addTeacherAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertTeacherAttachmentAsync(invalidTeacherAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }