Add relation exceptions (#12185)

Introducing a class of RelationException extending CustomException to
help grouping those exception in sentries by ExceptionCode.
I did not introduce a filter as these are thrown in utils that can be
used in multiple places now or in the future, and filters are to be
added at resolver-level.
This commit is contained in:
Marie
2025-05-21 15:53:25 +02:00
committed by GitHub
parent c982bcdb52
commit 8c6569be3b
5 changed files with 59 additions and 9 deletions

View File

@ -4,6 +4,10 @@ import { RelationType } from 'typeorm/metadata/types/RelationTypes';
import { FieldMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata.interface';
import { ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps';
import {
RelationException,
RelationExceptionCode,
} from 'src/engine/twenty-orm/exceptions/relation.exception';
import { converRelationTypeToTypeORMRelationType } from 'src/engine/twenty-orm/utils/convert-relation-type-to-typeorm-relation-type.util';
interface RelationDetails {
@ -26,7 +30,10 @@ export async function determineSchemaRelationDetails(
);
if (!fieldMetadata.relationTargetObjectMetadataId) {
throw new Error('Relation target object metadata ID is missing');
throw new RelationException(
'Relation target object metadata ID is missing',
RelationExceptionCode.RELATION_OBJECT_METADATA_NOT_FOUND,
);
}
const sourceObjectMetadata =
@ -35,11 +42,17 @@ export async function determineSchemaRelationDetails(
objectMetadataMaps.byId[fieldMetadata.relationTargetObjectMetadataId];
if (!sourceObjectMetadata || !targetObjectMetadata) {
throw new Error(`Object metadata not found for field ${fieldMetadata.id}`);
throw new RelationException(
`Object metadata not found for field ${fieldMetadata.id}`,
RelationExceptionCode.RELATION_OBJECT_METADATA_NOT_FOUND,
);
}
if (!fieldMetadata.relationTargetFieldMetadataId) {
throw new Error('Relation target field metadata ID is missing');
throw new RelationException(
'Relation target field metadata ID is missing',
RelationExceptionCode.RELATION_TARGET_FIELD_METADATA_ID_NOT_FOUND,
);
}
const targetFieldMetadata =