如何解决“ API”解决而不发送响应获取”。使用哨兵时

发布于 2025-01-25 03:51:23 字数 855 浏览 2 评论 0原文

我已经查看了无数其他帖子,找不到答案,为什么我在不发送/api/git/festment-commit的响应的情况下连续解决api,这可能会导致停滞的请求。 Next.js中的错误?一旦我禁用哨兵,它就消失了,还有其他人为此而苦苦挣扎?

import type { NextApiRequest, NextApiResponse } from 'next'
import { withSentry } from "@sentry/nextjs";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
    const response = await fetch(`https://api.github.com/repos/####/####/commits?per_page=1`, {
        method: 'GET'
    });

    const data = await response.json();
    const commit = data[0]
    res.status(200).json({
        sha: {
            full: commit.sha,
            short: commit.sha.substring(0,7)
        },
        committer: commit.commit.committer.name,
        time: commit.commit.committer.date,
        html_url: commit.html_url
    })
};

export default withSentry(handler);

I've looked at countless other posts and cannot find the answer to this, why am I continuously getting the API resolved without sending a response for /api/git/latest-commit, this may result in stalled requests. error in next.js? As soon as I disable sentry it goes away, has anyone else struggled with this?

import type { NextApiRequest, NextApiResponse } from 'next'
import { withSentry } from "@sentry/nextjs";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
    const response = await fetch(`https://api.github.com/repos/####/####/commits?per_page=1`, {
        method: 'GET'
    });

    const data = await response.json();
    const commit = data[0]
    res.status(200).json({
        sha: {
            full: commit.sha,
            short: commit.sha.substring(0,7)
        },
        committer: commit.commit.committer.name,
        time: commit.commit.committer.date,
        html_url: commit.html_url
    })
};

export default withSentry(handler);

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

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

发布评论

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

评论(1

雪化雨蝶 2025-02-01 03:51:23

运行代码在我的结尾上产生以下消息(下一个12.1.4@sentry/nextjs 6.19.7):

[sentry]如果Next.js记录了警告“ API无需发送响应而解决”,则是一个假阳性,我们正在努力纠正。
同时,要抑制此警告,请在您的env。
中设置sentry_ignore_api_api_resolution_error to 1
要抑制NextJs警告,请使用externalResolver api路由选项(请参阅 https://nextjs.org/docs/api-routes/api-middlewares#custom-config 有关详细信息)。

为了抑制Sentry的警告,我将此环境变量添加到.env.development文件:

SENTRY_IGNORE_API_RESOLUTION_ERROR=1

为了抑制Next.js API路由的警告,我将其添加到最新限制中。 TS

// ...

export const config = {
  api: {
    externalResolver: true,
  },
};

export default withSentry(handler);

两个警告不再出现,数据似乎正确返回。

经过一番挖掘,这就是他们对正在发生的事情的解释:
https://github.com/getsentry/sentry-javascript/pull/pull/pull/pull/4139

在DEV中,NextJS检查API路由处理者在解决客户时是否返回对客户的响应,如果没有发生,则会发出警告。同时,在withsentry()中,我们包装res.end()方法,以确保在请求/响应生命周期完成之前对事件进行冲洗。

结果,在某些情况下,处理程序在响应完成之前就可以解决,而冲洗仍在进行中。

Running your code produced the following message on my end (next 12.1.4, @sentry/nextjs 6.19.7):

[sentry] If Next.js logs a warning "API resolved without sending a response", it's a false positive, which we're working to rectify.
In the meantime, to suppress this warning, set SENTRY_IGNORE_API_RESOLUTION_ERROR to 1 in your env.
To suppress the nextjs warning, use the externalResolver API route option (see https://nextjs.org/docs/api-routes/api-middlewares#custom-config for details).

To suppress the warning from Sentry, I added this environment variable to an .env.development file:

SENTRY_IGNORE_API_RESOLUTION_ERROR=1

To suppress the warning from the Next.js API route, I added this to latest-commit.ts:

// ...

export const config = {
  api: {
    externalResolver: true,
  },
};

export default withSentry(handler);

Both warnings no longer appear and the data appears to return correctly.

After some digging, this was their explanation as to what's happening:
https://github.com/getsentry/sentry-javascript/pull/4139

In dev, nextjs checks that API route handlers return a response to the client before they resolve, and it throws a warning if this hasn't happened. Meanwhile, in withSentry(), we wrap the res.end() method to ensure that events are flushed before the request/response lifecycle finishes.

As a result, there are cases where the handler resolves before the response is finished, while flushing is still in progress.

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