Skip to content

Commit

Permalink
unified PX validation errors in a single class
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Jan 30, 2025
1 parent d06c277 commit cb08752
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
15 changes: 10 additions & 5 deletions app/api/paragraphExtraction/application/PXCreateExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { z } from 'zod';
import { UseCase } from 'api/common.v2/contracts/UseCase';
import { TemplatesDataSource } from 'api/templates.v2/contracts/TemplatesDataSource';

import { IdGenerator } from 'api/common.v2/contracts/IdGenerator';
import { PXExtractor } from '../domain/PXExtractor';
import { PXExtractorsDataSource } from '../domain/PXExtractorDataSource';
import { SourceTemplateNotFoundError } from '../domain/SourceTemplateNotFoundError';
import { TargetTemplateNotFoundError } from '../domain/TargetTemplateNotFoundError';
import { IdGenerator } from 'api/common.v2/contracts/IdGenerator';
import { PXErrorCode, PXValidationError } from '../domain/PXValidationError';

type Input = z.infer<typeof InputSchema>;
type Output = PXExtractor;
Expand All @@ -33,11 +32,17 @@ export class PXCreateExtractor implements UseCase<Input, Output> {
]);

if (!targetTemplate) {
throw new TargetTemplateNotFoundError(input.targetTemplateId);
throw new PXValidationError(
PXErrorCode.TARGET_TEMPLATE_NOT_FOUND,
`Target template with id ${input.targetTemplateId} was not found`
);
}

if (!sourceTemplate) {
throw new SourceTemplateNotFoundError(input.sourceTemplateId);
throw new PXValidationError(
PXErrorCode.SOURCE_TEMPLATE_NOT_FOUND,
`Source template with id ${input.sourceTemplateId} was not found`
);
}

const extractor = new PXExtractor({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { DefaultTemplatesDataSource } from 'api/templates.v2/database/data_sourc
import { getFixturesFactory } from 'api/utils/fixturesFactory';
import { testingEnvironment } from 'api/utils/testingEnvironment';

import { SourceTemplateNotFoundError } from '../../domain/SourceTemplateNotFoundError';
import { TargetSourceTemplateEqualError } from '../../domain/TargetSourceTemplateEqualError';
import { TargetTemplateInvalidError } from '../../domain/TargetTemplateInvalidError';
import { TargetTemplateNotFoundError } from '../../domain/TargetTemplateNotFoundError';
import { PXErrorCode } from 'api/paragraphExtraction/domain/PXValidationError';
import {
mongoPXExtractorsCollection,
MongoPXExtractorsDataSource,
Expand Down Expand Up @@ -72,28 +69,30 @@ describe('PXCreateExtractor', () => {

it('should throw if target Template does not exist', async () => {
const { createExtractor } = setUpUseCase();

const targetTemplateId = new ObjectId().toString();

const promise = createExtractor.execute({
sourceTemplateId: sourceTemplate._id.toString(),
targetTemplateId,
});

await expect(promise).rejects.toEqual(new TargetTemplateNotFoundError(targetTemplateId));
await expect(promise).rejects.toMatchObject({
code: PXErrorCode.TARGET_TEMPLATE_NOT_FOUND,
});
});

it('should throw if source Template does not exist', async () => {
const { createExtractor } = setUpUseCase();

const sourceTemplateId = new ObjectId().toString();

const promise = createExtractor.execute({
targetTemplateId: sourceTemplate._id.toString(),
sourceTemplateId,
});

await expect(promise).rejects.toEqual(new SourceTemplateNotFoundError(sourceTemplateId));
await expect(promise).rejects.toMatchObject({
code: PXErrorCode.SOURCE_TEMPLATE_NOT_FOUND,
});
});

it('should throw if target template is not valid for create an Extractor', async () => {
Expand All @@ -104,12 +103,11 @@ describe('PXCreateExtractor', () => {
targetTemplateId: invalidTargetTemplate._id.toString(),
});

await expect(promise).rejects.toEqual(
new TargetTemplateInvalidError(invalidTargetTemplate._id.toString())
);
await expect(promise).rejects.toMatchObject({
code: PXErrorCode.TARGET_TEMPLATE_INVALID,
});

const dbPXExtractors = await testingEnvironment.db.getAllFrom(mongoPXExtractorsCollection);

expect(dbPXExtractors).toEqual([]);
});

Expand All @@ -121,10 +119,11 @@ describe('PXCreateExtractor', () => {
targetTemplateId: targetTemplate._id.toString(),
});

await expect(promise).rejects.toEqual(new TargetSourceTemplateEqualError());
await expect(promise).rejects.toMatchObject({
code: PXErrorCode.TARGET_SOURCE_TEMPLATE_EQUAL,
});

const dbPXExtractors = await testingEnvironment.db.getAllFrom(mongoPXExtractorsCollection);

expect(dbPXExtractors).toEqual([]);
});
});
13 changes: 9 additions & 4 deletions app/api/paragraphExtraction/domain/PXExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Template } from 'api/templates.v2/model/Template';
import { TargetTemplateInvalidError } from './TargetTemplateInvalidError';
import { TargetSourceTemplateEqualError } from './TargetSourceTemplateEqualError';
import { PXValidationError, PXErrorCode } from './PXValidationError';

export type PXExtractorProps = {
id: string;
Expand All @@ -25,11 +24,17 @@ export class PXExtractor {

private validate() {
if (!this.targetTemplate.getPropertiesByType('markdown').length) {
throw new TargetTemplateInvalidError(this.targetTemplate.id);
throw new PXValidationError(
PXErrorCode.TARGET_TEMPLATE_INVALID,
`Target template with id ${this.targetTemplate.id} should have at least one rich text property`
);
}

if (this.targetTemplate.id === this.sourceTemplate.id) {
throw new TargetSourceTemplateEqualError();
throw new PXValidationError(
PXErrorCode.TARGET_SOURCE_TEMPLATE_EQUAL,
'Target and Source template cannot be the same'
);
}
}
}
17 changes: 17 additions & 0 deletions app/api/paragraphExtraction/domain/PXValidationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export enum PXErrorCode {
TARGET_TEMPLATE_NOT_FOUND = 'TARGET_TEMPLATE_NOT_FOUND',
SOURCE_TEMPLATE_NOT_FOUND = 'SOURCE_TEMPLATE_NOT_FOUND',
TARGET_TEMPLATE_INVALID = 'TARGET_TEMPLATE_INVALID',
TARGET_SOURCE_TEMPLATE_EQUAL = 'TARGET_SOURCE_TEMPLATE_EQUAL',
}

export class PXValidationError extends Error {
constructor(
public code: PXErrorCode,
message: string,
options?: ErrorOptions
) {
super(message, options);
this.name = 'PXValidationError';
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit cb08752

Please sign in to comment.