forked from Sairyss/domain-driven-hexagon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exception.interceptor.ts
38 lines (37 loc) · 1.08 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
import {
CallHandler,
ExecutionContext,
NestInterceptor,
// To avoid confusion between internal app 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 => {
/**
* Custom exceptions are converted to nest.js exceptions.
* This way we are not tied to a framework or HTTP protocol.
*/
if (err instanceof NotFoundException) {
throw new NestNotFoundException(err.message);
}
if (err instanceof ConflictException) {
throw new NestConflictException(err.message);
}
return throwError(err);
}),
);
}
}