From 48a48e0a4c7e6b1e884106cd42222e21c0bb7319 Mon Sep 17 00:00:00 2001 From: Jeremy Yin Date: Thu, 20 Jun 2019 21:59:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=AF=E7=94=A8=E4=B8=80=E4=B8=8B=E5=88=9A?= =?UTF-8?q?=E8=BF=99=E4=B8=AAerrors=E6=8B=A6=E6=88=AA=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E5=AE=83=E7=8E=B0=E5=9C=A8=E6=8A=8A403?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=8F=98=E6=88=90=E4=BA=86502=E9=94=99?= =?UTF-8?q?=E8=AF=AF=EF=BC=8C=E6=84=9F=E8=A7=89=E7=94=A8=E6=9D=A5=E5=81=9A?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E8=A7=84=E8=8C=83=E5=8C=96?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E9=80=82=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/interceptors/errors.interceptor.ts | 11 ++++++++--- src/modules/posts/posts.controller.ts | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-) 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')