Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: log errors from event handlers #728

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/event-bus.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable, OnModuleDestroy, Type } from '@nestjs/common';
import { Logger } from '@nestjs/common/services/logger.service';
import { ModuleRef } from '@nestjs/core';
import { Observable, Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import { filter, tap } from 'rxjs/operators';
import { isFunction } from 'util';
import { CommandBus } from './command-bus';
import { EVENTS_HANDLER_METADATA, SAGA_METADATA } from './decorators/constants';
Expand All @@ -24,10 +25,12 @@ export type EventHandlerType<EventBase extends IEvent = IEvent> = Type<
@Injectable()
export class EventBus<EventBase extends IEvent = IEvent>
extends ObservableBus<EventBase>
implements IEventBus<EventBase>, OnModuleDestroy {
implements IEventBus<EventBase>, OnModuleDestroy
{
protected getEventName: (event: EventBase) => string;
protected readonly subscriptions: Subscription[];

private readonly logger = new Logger('EventBus');
private _publisher: IEventPublisher<EventBase>;

constructor(
Expand Down Expand Up @@ -65,7 +68,14 @@ export class EventBus<EventBase extends IEvent = IEvent>

bind(handler: IEventHandler<EventBase>, name: string) {
const stream$ = name ? this.ofEventName(name) : this.subject$;
const subscription = stream$.subscribe((event) => handler.handle(event));
const subscription = stream$
.pipe(tap((event) => handler.handle(event)))
.subscribe({
error: (error) => {
this.logger.error(`${handler.constructor.name} produced an error.`);
throw error;
},
});
this.subscriptions.push(subscription);
}

Expand Down