Promise(…).catch does not work for Error: There is no endpoint matching that name of fn_index matching that number.

用户在一个基于 Next.js 的 Web 应用中,通过 @gradio/client 调用远程 Gradio 服务的 API 端点(例如 /api/gradio/[name].tsx ),传入的端点名或 fn_index 在服务端不存在,导致 get_endpoint_info() 同步抛出错误,

Promise(...).catch does not work for Error: There is no endpoint matching that name of fn_index matching that number.

Promise(…).catch does not work for Error: There is no endpoint matching that name of fn_index matching that number.

快速结论:该报错出现在使用 Gradio Client 的 predict()submit() 方法时,前端传入的 API 端点名称或编号无效。错误在 Promise 链外层同步抛出,导致 .catch()try-catch 都无法捕获,表现为未处理的 Promise 拒绝。

问题场景

用户在一个基于 Next.js 的 Web 应用中,通过 @gradio/client 调用远程 Gradio 服务的 API 端点(例如 /api/gradio/[name].tsx),传入的端点名或 fn_index 在服务端不存在,导致 get_endpoint_info() 同步抛出错误,进而触发 Node.js 的 unhandledRejection。已知复现工具包括 tts-generation-webui 以及其它通过 Gradio Client 进行远程调用的脚本。

报错原文

Submit function encountered an error: Error: There is no endpoint matching that name of fn_index matching that number.
    at get_endpoint_info (file:///C:/Users/user/Desktop/tts-generation-webui-main/react-ui/node_modules/@gradio/client/dist/index.js:2255:11)
    at Client.submit (file:///C:/Users/user/Desktop/tts-generation-webui-main/react-ui/node_modules/@gradio/client/dist/index.js:1601:51)
    at file:///C:/Users/user/Desktop/tts-generation-webui-main/react-ui/node_modules/@gradio/client/dist/index.js:971:22
    at new Promise ()
    at Client.predict (file:///C:/Users/user/Desktop/tts-generation-webui-main/react-ui/node_modules/@gradio/client/dist/index.js:970:10)
    at eval (webpack-internal:///(api)/./src/pages/api/gradio/[name].tsx:83:62)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async handler (webpack-internal:///(api)/./src/pages/api/gradio/[name].tsx:51:24)
 ⨯ unhandledRejection: Error: There is no endpoint matching that name of fn_index matching that number.
    at get_endpoint_info (file:///C:/Users/user/Desktop/tts-generation-webui-main/react-ui/node_modules/@gradio/client/dist/index.js:2255:11)
    at Client.submit (file:///C:/Users/user/Desktop/tts-generation-webui-main/react-ui/node_modules/@gradio/client/dist/index.js:1601:51)
    ...

原因分析

经 Issue 贡献者分析,问题出在 @gradio/client 库内部的错误处理机制:

  • get_endpoint_info() 函数在 client.predict()client.submit() 被调用的 Promise 链之外同步抛出 Error,导致该错误脱离 Promise 的 .catch() 捕获范围。
  • 由于错误并未被包装为 Promise.reject().catch()try-catch(在 async 函数中使用)均无法处理该异常,最终触发 Node.js 的 unhandledRejection
  • 可能原因:客户端传入的端点名称(name)或 fn_index 与 Gradio 服务端返回的端点列表不匹配,例如服务端版本更新导致端点名称变更,或是客户端缓存了过期的端点信息。

环境排查

  • Gradio 版本:确认 @gradio/client 的版本(未在 Issue 中明确,但可检查 package.json)。
  • Node.js 版本:使用者环境中的 Node.js 版本(上述栈追踪使用 Node.js)。
  • Gradio 服务端:远程 Gradio 服务的版本及运行状态,确认端点列表是否包含所请求的 namefn_index
  • 调用方式:确认客户端是使用 client.predict()client.submit() 还是更低阶的 API。

解决步骤

  1. 核对端点名称:直接通过浏览器访问 Gradio 服务(例如 http://localhost:7770/)或使用 Gradio 的 API 文档页面,确认可用的端点名称和对应的 fn_index。在 Issue 中,评论中提到“直接访问 http://localhost:7770/ 并安装扩展”曾作为临时工作区。
  2. 包装调用逻辑(可优先尝试):client.predict()client.submit() 放入 async 函数并使用 try-catch,如果错误仍是未处理,则在调用前显式验证端点是否存在:
    const endpoints = await client.view_api(); // 获取所有可用端点
    // 在调用前检查端点名称或 fn_index 是否存在
    if (!endpoints.some(ep => ep.name === targetName || ep.index === targetIndex)) {
      throw new Error(`Endpoint ${targetName} or index ${targetIndex} not found`);
    }
    
  3. 确保错误被 Promise 包装:如果开发环境允许,在调用 get_endpoint_info() 之前,将可能同步抛出的错误显式转为 Promise.reject()(这需要修改 @gradio/client 源码或提交 PR)。Issue 中建议的修复方向包括:
    • client.connect() 阶段预验证端点可用性。
    • get_endpoint_info() 内的同步 throw 改为 return Promise.reject(new Error(...))
  4. 升级或等待修复:检查 @gradio/client 是否有更新版本包含对此错误的修复。如果无法修改客户端代码,可临时在全局监听 unhandledRejection 事件避免进程崩溃:
    process.on('unhandledRejection', (reason, promise) => {
      console.error('Unhandled Rejection at:', promise, 'reason:', reason);
      // 可选:尝试重试或记录日志
    });
    
  5. 若使用 Next.js:确保 API 路由(如 [name].tsx)中正确传递 fn_indexname,检查是否有拼写错误或动态路由参数解析问题。

验证方法

修复后,调用之前触发报错的 client.predict()client.submit()

  • 如果使用 .catch(),错误应被正常捕获,不再出现 unhandledRejection
  • 如果使用 async/await 配合 try-catchcatch 块应能收到 "There is no endpoint matching that name of fn_index matching that number." 错误对象。
  • Node.js 进程不应崩溃,全局 unhandledRejection 事件数量为零。

参考来源

gradio-app/gradio #12101

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 11382

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注