Add exceptions for metadata modules (#6070)
Class exception for each metadata module + handler to map on graphql error TODO left : - find a way to call handler on auto-resolvers nestjs query (probably interceptors) - discuss what should be done for pre-hooks errors - discuss what should be done for Unauthorized exception
This commit is contained in:
@ -0,0 +1,15 @@
|
||||
import { CustomException } from 'src/utils/custom-exception';
|
||||
|
||||
export class RelationMetadataException extends CustomException {
|
||||
code: RelationMetadataExceptionCode;
|
||||
constructor(message: string, code: RelationMetadataExceptionCode) {
|
||||
super(message, code);
|
||||
}
|
||||
}
|
||||
|
||||
export enum RelationMetadataExceptionCode {
|
||||
RELATION_METADATA_NOT_FOUND = 'RELATION_METADATA_NOT_FOUND',
|
||||
INVALID_RELATION_INPUT = 'INVALID_RELATION_INPUT',
|
||||
RELATION_ALREADY_EXISTS = 'RELATION_ALREADY_EXISTS',
|
||||
FOREIGN_KEY_NOT_FOUND = 'FOREIGN_KEY_NOT_FOUND',
|
||||
}
|
||||
@ -7,6 +7,7 @@ import { RelationMetadataService } from 'src/engine/metadata-modules/relation-me
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { RelationMetadataDTO } from 'src/engine/metadata-modules/relation-metadata/dtos/relation-metadata.dto';
|
||||
import { DeleteOneRelationInput } from 'src/engine/metadata-modules/relation-metadata/dtos/delete-relation.input';
|
||||
import { relationMetadataGraphqlApiExceptionHandler } from 'src/engine/metadata-modules/relation-metadata/utils/relation-metadata-graphql-api-exception-handler.util';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Resolver()
|
||||
@ -20,9 +21,13 @@ export class RelationMetadataResolver {
|
||||
@Args('input') input: DeleteOneRelationInput,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
) {
|
||||
return this.relationMetadataService.deleteOneRelation(
|
||||
input.id,
|
||||
workspaceId,
|
||||
);
|
||||
try {
|
||||
return this.relationMetadataService.deleteOneRelation(
|
||||
input.id,
|
||||
workspaceId,
|
||||
);
|
||||
} catch (error) {
|
||||
relationMetadataGraphqlApiExceptionHandler(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,4 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
||||
@ -28,9 +23,15 @@ import {
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
|
||||
import { generateMigrationName } from 'src/engine/metadata-modules/workspace-migration/utils/generate-migration-name.util';
|
||||
import { InvalidStringException } from 'src/engine/metadata-modules/errors/InvalidStringException';
|
||||
import { validateMetadataName } from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
|
||||
import {
|
||||
validateMetadataName,
|
||||
InvalidStringException,
|
||||
} from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
|
||||
import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service';
|
||||
import {
|
||||
RelationMetadataException,
|
||||
RelationMetadataExceptionCode,
|
||||
} from 'src/engine/metadata-modules/relation-metadata/relation-metadata.exception';
|
||||
|
||||
import {
|
||||
RelationMetadataEntity,
|
||||
@ -66,8 +67,9 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
validateMetadataName(relationMetadataInput.toName);
|
||||
} catch (error) {
|
||||
if (error instanceof InvalidStringException) {
|
||||
throw new BadRequestException(
|
||||
throw new RelationMetadataException(
|
||||
`Characters used in name "${relationMetadataInput.fromName}" or "${relationMetadataInput.toName}" are not supported`,
|
||||
RelationMetadataExceptionCode.INVALID_RELATION_INPUT,
|
||||
);
|
||||
} else {
|
||||
throw error;
|
||||
@ -132,8 +134,9 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
if (
|
||||
relationMetadataInput.relationType === RelationMetadataType.MANY_TO_MANY
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
throw new RelationMetadataException(
|
||||
'Many to many relations are not supported yet',
|
||||
RelationMetadataExceptionCode.INVALID_RELATION_INPUT,
|
||||
);
|
||||
}
|
||||
|
||||
@ -142,8 +145,9 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
undefined ||
|
||||
objectMetadataMap[relationMetadataInput.toObjectMetadataId] === undefined
|
||||
) {
|
||||
throw new NotFoundException(
|
||||
throw new RelationMetadataException(
|
||||
'Can\t find an existing object matching with fromObjectMetadataId or toObjectMetadataId',
|
||||
RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
@ -177,12 +181,13 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
);
|
||||
|
||||
if (fieldAlreadyExists) {
|
||||
throw new ConflictException(
|
||||
throw new RelationMetadataException(
|
||||
`Field on ${
|
||||
objectMetadataMap[
|
||||
relationMetadataInput[`${relationDirection}ObjectMetadataId`]
|
||||
].nameSingular
|
||||
} already exists`,
|
||||
RelationMetadataExceptionCode.RELATION_ALREADY_EXISTS,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -335,7 +340,10 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
});
|
||||
|
||||
if (!relationMetadata) {
|
||||
throw new NotFoundException('Relation does not exist');
|
||||
throw new RelationMetadataException(
|
||||
'Relation does not exist',
|
||||
RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const foreignKeyFieldMetadataName = `${camelCase(
|
||||
@ -351,8 +359,9 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
});
|
||||
|
||||
if (!foreignKeyFieldMetadata) {
|
||||
throw new NotFoundException(
|
||||
throw new RelationMetadataException(
|
||||
`Foreign key fieldMetadata not found (${foreignKeyFieldMetadataName}) for relation ${relationMetadata.id}`,
|
||||
RelationMetadataExceptionCode.FOREIGN_KEY_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
@ -420,6 +429,7 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
|
||||
return (
|
||||
foundRelationMetadataItem ??
|
||||
// TODO: return a relation metadata not found exception
|
||||
new NotFoundException(
|
||||
`RelationMetadata with fieldMetadataId ${fieldMetadataId} not found`,
|
||||
)
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
import {
|
||||
RelationMetadataException,
|
||||
RelationMetadataExceptionCode,
|
||||
} from 'src/engine/metadata-modules/relation-metadata/relation-metadata.exception';
|
||||
import {
|
||||
UserInputError,
|
||||
ConflictError,
|
||||
InternalServerError,
|
||||
NotFoundError,
|
||||
} from 'src/engine/utils/graphql-errors.util';
|
||||
|
||||
export const relationMetadataGraphqlApiExceptionHandler = (error: Error) => {
|
||||
if (error instanceof RelationMetadataException) {
|
||||
switch (error.code) {
|
||||
case RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND:
|
||||
throw new NotFoundError(error.message);
|
||||
case RelationMetadataExceptionCode.INVALID_RELATION_INPUT:
|
||||
throw new UserInputError(error.message);
|
||||
case RelationMetadataExceptionCode.RELATION_ALREADY_EXISTS:
|
||||
throw new ConflictError(error.message);
|
||||
case RelationMetadataExceptionCode.FOREIGN_KEY_NOT_FOUND:
|
||||
default:
|
||||
throw new InternalServerError(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
throw error;
|
||||
};
|
||||
Reference in New Issue
Block a user