diff --git a/src/core/interceptors/errors.interceptor.ts b/src/core/interceptors/errors.interceptor.ts index de5f413..a374e5b 100644 --- a/src/core/interceptors/errors.interceptor.ts +++ b/src/core/interceptors/errors.interceptor.ts @@ -1,9 +1,14 @@ -import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'; -import { Observable } from 'rxjs'; +import { CallHandler, ExecutionContext, Injectable, NestInterceptor, BadGatewayException } from '@nestjs/common'; +import { Observable, throwError } from 'rxjs'; +import { catchError } from 'rxjs/operators' @Injectable() export class ErrorsInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable { - return next.handle(); + return next + .handle() + .pipe( + catchError(error => throwError(new BadGatewayException())) + ); } } diff --git a/src/modules/posts/posts.controller.ts b/src/modules/posts/posts.controller.ts index 0f09f9b..46994d6 100644 --- a/src/modules/posts/posts.controller.ts +++ b/src/modules/posts/posts.controller.ts @@ -6,6 +6,7 @@ import { DemoAuthGuard } from '../../core/guards/demo-auth.guard' import { Roles } from '../../core/decorators/roles.decorator' import { LoggingInterceptor } from '../../core/interceptors/logging.interceptor' import { TransformInterceptor } from '../../core/interceptors/transform.interceptor' +import { ErrorsInterceptor } from '../../core/interceptors/errors.interceptor' @Controller('posts') // @UseGuards(DemoAuthGuard) @@ -17,8 +18,10 @@ export class PostsController { @Get() @UseInterceptors(TransformInterceptor) + @UseInterceptors(ErrorsInterceptor) index() { - return this.demoService.findAll(); + throw new ForbiddenException(); + // return this.demoService.findAll(); } @Get(':id')