forked from Sairyss/domain-driven-hexagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.interceptor.ts
39 lines (38 loc) · 1.09 KB
/
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
35
36
37
38
39
import {
CallHandler,
ExecutionContext,
NestInterceptor,
// To avoid confusion between internal exceptions and NestJS exceptions
ConflictException as NestConflictException,
NotFoundException as NestNotFoundException,
ForbiddenException as NestForbiddenException,
} from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import {
ExceptionBase,
ConflictException,
NotFoundException,
DomainException,
} from '@exceptions';
export class ExceptionInterceptor implements NestInterceptor {
intercept(
_context: ExecutionContext,
next: CallHandler,
): Observable<ExceptionBase> {
return next.handle().pipe(
catchError(err => {
if (err instanceof DomainException) {
throw new NestForbiddenException(err.message);
}
if (err instanceof NotFoundException) {
throw new NestNotFoundException(err.message);
}
if (err instanceof ConflictException) {
throw new NestConflictException(err.message);
}
return throwError(err);
}),
);
}
}