启用一下刚这个errors拦截处理器,它现在把403错误变成了502错误,感觉用来做错误信息规范化比较适合

This commit is contained in:
Jeremy Yin 2019-06-20 21:59:56 +08:00
parent 3e83be0032
commit 48a48e0a4c
2 changed files with 12 additions and 4 deletions

View File

@ -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<any> {
return next.handle();
return next
.handle()
.pipe(
catchError(error => throwError(new BadGatewayException()))
);
}
}

View File

@ -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')