Should exception filters catch errors in the event emitter? #137
-
I saw that my tests were not passing because I expected a failure and received a success. The problem is that I can't catch the error. To recreate a similar scenario, you can try forcing the id of the wallet entity to one already persisted in the db: [Nest] 2229 - 10/19/2023, 2:13:58 PM DEBUG [UserRepository] [J6xG28] transaction started
[Nest] 2229 - 10/19/2023, 2:13:58 PM DEBUG [UserRepository] [J6xG28] writing 1 entities to "users" table: c8cdff4b-e90d-45fa-9f6c-0a799a949a84
[Nest] 2229 - 10/19/2023, 2:13:58 PM DEBUG [UserRepository] [J6xG28] "UserCreatedDomainEvent" event published for aggregate UserEntity : c8cdff4b-e90d-45fa-9f6c-0a799a949a84
[Nest] 2229 - 10/19/2023, 2:13:58 PM DEBUG [WalletRepository] [J6xG28] writing 1 entities to "wallets" table: ab1dd6d6-50bb-4f99-95e5-09e5ce37e871
[Nest] 2229 - 10/19/2023, 2:13:58 PM DEBUG [WalletRepository] [J6xG28] Key (id)=(ab1dd6d6-50bb-4f99-95e5-09e5ce37e871) already exists.
[Nest] 2229 - 10/19/2023, 2:13:58 PM ERROR [Event] Error: Record already exists
[Nest] 2229 - 10/19/2023, 2:13:58 PM DEBUG [UserRepository] [J6xG28] transaction committed response:
So should I validate this in the create user service? something like: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Beta Was this translation helpful? Give feedback.
-
Maybe you are missing a |
Beta Was this translation helpful? Give feedback.
-
If you upgraded to @nestjs/event-emitter "2.0.0" or major, try add "suppressErrors: false" @Injectable()
export class CreateWalletWhenUserIsCreatedDomainEventHandler {
constructor(
@Inject(WALLET_REPOSITORY)
private readonly walletRepo: WalletRepositoryPort,
) {}
// Handle a Domain Event by performing changes to other aggregates (inside the same Domain).
@OnEvent(UserCreatedDomainEvent.name, {
async: true,
promisify: true,
suppressErrors: false,
})
async handle(event: UserCreatedDomainEvent): Promise<any> {
const wallet = WalletEntity.create({
userId: event.aggregateId,
});
return this.walletRepo.insert(wallet);
}
} |
Beta Was this translation helpful? Give feedback.
If you upgraded to @nestjs/event-emitter "2.0.0" or major, try add "suppressErrors: false"