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 <alumni@users.noreply.github.com>
Co-authored-by: Mike Guida <mike@mguida.com>
This commit is contained in:
JackNytely 2025-04-15 20:19:58 +02:00 committed by GitHub
parent 046aebe696
commit 184f463ed7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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("."),