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:
Thomas Trompette
2024-07-01 13:49:17 +02:00
committed by GitHub
parent 4599f43b6c
commit a15884ea0a
48 changed files with 815 additions and 199 deletions

View File

@ -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`,
)