你可以写一个
interceptor
这会导致
NotFoundException
在
undefined
:
@Injectable()
export class NotFoundInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, stream$: Observable<any>): Observable<any> {
// stream$ is an Observable of the controller's result value
return stream$
.pipe(tap(data => {
if (data === undefined) throw new NotFoundException();
}));
}
}
然后在控制器中使用拦截器。您可以在每个类或方法中使用它:
// Apply the interceptor to *all* endpoints defined in this controller
@Controller('user')
@UseInterceptors(NotFoundInterceptor)
export class UserController {
或
// Apply the interceptor only to this endpoint
@Get()
@UseInterceptors(NotFoundInterceptor)
getUser() {
return Promise.resolve(undefined);
}