From 184f463ed7601467c8ea43e0a75f52a0b40bd745 Mon Sep 17 00:00:00 2001 From: JackNytely <48361205+JackNytely@users.noreply.github.com> Date: Tue, 15 Apr 2025 20:19:58 +0200 Subject: [PATCH] chore: log descriptive errors from the Entity Listener Metadata (#11234) * Added logging to the Entity Listener Metadata as it will throw an error (entity[this.propertyName] is not a function), which is not a descriptive error and makes it hard for users to debug their programs * Bound the entity to the Entity Method * Update src/metadata/EntityListenerMetadata.ts --------- Co-authored-by: Lucian Mocanu Co-authored-by: Mike Guida --- src/metadata/EntityListenerMetadata.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/metadata/EntityListenerMetadata.ts b/src/metadata/EntityListenerMetadata.ts index 425f491ac..a64a7e685 100644 --- a/src/metadata/EntityListenerMetadata.ts +++ b/src/metadata/EntityListenerMetadata.ts @@ -75,8 +75,30 @@ export class EntityListenerMetadata { * Executes listener method of the given entity. */ execute(entity: ObjectLiteral) { - if (!this.embeddedMetadata) return entity[this.propertyName]() + // Check if the Embedded Metadata does not exist + if (!this.embeddedMetadata) { + // Get the Entity's Method + const entityMethod = entity[this.propertyName] + // Check if the Entity Method does not exist + if (!entityMethod) + throw new Error( + `Entity listener method "${this.propertyName}" does not exist in entity "${entity.constructor.name}".`, + ) + + // Check if the Entity Method is not a function + if (typeof entityMethod !== "function") + throw new Error( + `Entity listener method "${this.propertyName}" in entity "${ + entity.constructor.name + }" must be a function but got "${typeof entityMethod}".`, + ) + + // Call and return the Entity Method + return entityMethod.call(entity) + } + + // Call the Embedded Method this.callEntityEmbeddedMethod( entity, this.embeddedMetadata.propertyPath.split("."),