Skip to content

Commit ca7c9a1

Browse files
committed
[feat] Saga에서 발생하는 처리되지 않은 예외 처리 추가
1 parent 267d3ab commit ca7c9a1

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/user/saga/user.saga.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
import { Injectable } from '@nestjs/common';
2-
import { ICommand, ofType, Saga } from '@nestjs/cqrs';
3-
import { map, Observable } from 'rxjs';
2+
import {
3+
ICommand,
4+
ofType,
5+
Saga,
6+
UnhandledExceptionBus,
7+
UnhandledExceptionInfo,
8+
} from '@nestjs/cqrs';
9+
import { map, Observable, Subject, takeUntil } from 'rxjs';
410
import { CreateUserEvent } from '../event';
511
import { CreateRegisterCouponCommand } from '../command';
612

713
@Injectable()
814
export class UserSaga {
15+
private destroy$ = new Subject<void>();
16+
17+
constructor(private unhandledExceptionsBus: UnhandledExceptionBus) {
18+
this.unhandledExceptionsBus
19+
.pipe(takeUntil(this.destroy$))
20+
.subscribe((exceptionInfo: UnhandledExceptionInfo) => {
21+
console.error('Unhandled exception in saga:', exceptionInfo);
22+
// 재시도나 에러 알림 등 처리
23+
});
24+
}
25+
926
/** @Saga() 가 있으면 애플리케이션의 이벤트 스트림을 구독하고 `ofType`으로 처리할 이벤트를 선택 */
1027
@Saga()
1128
public userCreated(event$: Observable<any>): Observable<ICommand> {
@@ -15,4 +32,9 @@ export class UserSaga {
1532
map((event) => new CreateRegisterCouponCommand(fakeCouponId, event.id)),
1633
);
1734
}
35+
36+
onModuleDestroy() {
37+
this.destroy$.next();
38+
this.destroy$.complete();
39+
}
1840
}

0 commit comments

Comments
 (0)