forked from Sairyss/domain-driven-hexagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.interceptor.ts
34 lines (33 loc) · 946 Bytes
/
exception.interceptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import {
CallHandler,
ExecutionContext,
NestInterceptor,
// To avoid confusion between internal exceptions and NestJS exceptions
ConflictException as NestConflictException,
NotFoundException as NestNotFoundException,
} from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import {
ExceptionBase,
ConflictException,
NotFoundException,
} from '@libs/exceptions';
export class ExceptionInterceptor implements NestInterceptor {
intercept(
_context: ExecutionContext,
next: CallHandler,
): Observable<ExceptionBase> {
return next.handle().pipe(
catchError(err => {
if (err instanceof NotFoundException) {
throw new NestNotFoundException(err.message);
}
if (err instanceof ConflictException) {
throw new NestConflictException(err.message);
}
return throwError(err);
}),
);
}
}