如何求解nestjs fastify错误typeerror:response.status(...)。json不是函数

发布于 2025-02-09 06:20:11 字数 494 浏览 0 评论 0原文

typeerror:wendesp.status(...)。json不是函数 在httpexceptionfilter.catch(/home/muhammedali/documents/100haryt_v2/src/common/http/http/http-exception.filter.ts:23:29) 在exceptionshandler.invokecustomfilters(/home/muhammedali/documents/100haryt_v2/node_modules/@nestjs/core/core/exceptions/exceptions/exceptions/exceptions handler.js:33:26) 在exceptionshandler.next(/home/muhammedali/documents/100haryt_v2/node_modules/@nestjs/core/core/exceptions/exceptions/exceptions/exceptions handler.js:13:18)

TypeError: response.status(...).json is not a function
at HttpExceptionFilter.catch (/home/muhammedali/Documents/100haryt_v2/src/common/http/http-exception.filter.ts:23:29)
at ExceptionsHandler.invokeCustomFilters (/home/muhammedali/Documents/100haryt_v2/node_modules/@nestjs/core/exceptions/exceptions-handler.js:33:26)
at ExceptionsHandler.next (/home/muhammedali/Documents/100haryt_v2/node_modules/@nestjs/core/exceptions/exceptions-handler.js:13:18)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2025-02-16 06:20:11
  response
      .status(status)
      .json({
        statusCode: status,
        timestamp: new Date().toISOString(),
        path: request.url,
      });

而不是使用上述代码。尝试如下改变

  response
      .status(status)
      .send({
        statusCode: status,
        timestamp: new Date().toISOString(),
        path: request.url,
      });
  response
      .status(status)
      .json({
        statusCode: status,
        timestamp: new Date().toISOString(),
        path: request.url,
      });

instead of using the above code. try to change like as below

  response
      .status(status)
      .send({
        statusCode: status,
        timestamp: new Date().toISOString(),
        path: request.url,
      });
浮生未歇 2025-02-16 06:20:11

对我来说,当使用graphQl需要不同的异常处理程序时,发生这种情况。我只想将消息发送wenders.status在使用http作为协议时。您可以将其添加到Nestjs的自定义过滤器处理程序(也可以在 https:///docs.nestjs)中进行解释。 com/exception-filters )可以为您解决这个问题。 他们只会捕获httpexception,而这将通过不向@catch提供类型来捕获所有异常

import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
import { Catch, HttpException } from '@nestjs/common';
import type { Request, Response } from 'express';
import type { GqlArgumentsHost, GqlContextType } from '@nestjs/graphql';

const errorExceptionHandlingMap: Record<GqlContextType, (exception: unknown, host: GqlArgumentsHost) => void> = {
    http: (exception: unknown, host: GqlArgumentsHost) => {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse<Response>();
        const request = ctx.getRequest<Request>();
        const status = exception instanceof HttpException ? exception.getStatus() : 500;

        response.status(status).json({
            statusCode: status,
            timestamp: new Date().toISOString(),
            path: request.url,
        });
    },
    rpc: (exception) => {
        // custom handling here..
    },
    graphql: (error, host) => {
        // custom handling here
    },
    ws: (exception: unknown, host: ArgumentsHost) => {
        //custom error handling here
    },
};

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
    catch(exception: unknown, host: GqlArgumentsHost): void {
        switch (host.getType<GqlContextType>()) {
            case 'http':
            case 'rpc':
            case 'ws':
            case 'graphql':
                errorExceptionHandlingMap[host.getType()](exception, host);
                break;
            default:
                throw new Error('AllExceptionsFilter: Unknown host type' + host.getType());
        }
    }
}

尽管在该解释中 Nestjs的文件您会添加

app.useglobalfilters(new AllexceptionsFiler());

这应该照顾它,并让所有您的非HTTP错误通过而不会崩溃。

For me this happened when using graphql which needs a different exception handler. I only want the messages send with response.status when using http as protocol. You can add a custom filter handler for this to NestJS (as also explained at https://docs.nestjs.com/exception-filters) which can solve this for you. Though in that explanation they only catch the HttpException while this will catch all exceptions by not providing a type to @Catch

For example my AllExceptionsFilter.ts looks like this:

import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
import { Catch, HttpException } from '@nestjs/common';
import type { Request, Response } from 'express';
import type { GqlArgumentsHost, GqlContextType } from '@nestjs/graphql';

const errorExceptionHandlingMap: Record<GqlContextType, (exception: unknown, host: GqlArgumentsHost) => void> = {
    http: (exception: unknown, host: GqlArgumentsHost) => {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse<Response>();
        const request = ctx.getRequest<Request>();
        const status = exception instanceof HttpException ? exception.getStatus() : 500;

        response.status(status).json({
            statusCode: status,
            timestamp: new Date().toISOString(),
            path: request.url,
        });
    },
    rpc: (exception) => {
        // custom handling here..
    },
    graphql: (error, host) => {
        // custom handling here
    },
    ws: (exception: unknown, host: ArgumentsHost) => {
        //custom error handling here
    },
};

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
    catch(exception: unknown, host: GqlArgumentsHost): void {
        switch (host.getType<GqlContextType>()) {
            case 'http':
            case 'rpc':
            case 'ws':
            case 'graphql':
                errorExceptionHandlingMap[host.getType()](exception, host);
                break;
            default:
                throw new Error('AllExceptionsFilter: Unknown host type' + host.getType());
        }
    }
}

In the setup file for NestJS you would add

app.useGlobalFilters(new AllExceptionsFiler());

This should take care of it and let all your non-http errors through without crashing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文