[nestjs/telegraf] polling fetchError: ETIMEDOUT 导致 bot 被停止的问题

当 polling 访问 telegram api 超时的时候,就会出现这个问题(node v22.4.1)

这个问题,除了会导致 bot 无响应之外,还会导致主进程被阻塞。

nestjs 的全局异常过滤器只会捕获它定义的 http 相关的异常。至于这个 system 类型的异常是不会被捕获的,所以需要

// 未捕获的异常处理:防止 非 http 异常 导致程序崩溃
process.on('uncaughtException', (err) => {
  this.logger.warn('uncaughtException: ', err);
  // 重启机器人,解决 FetchError: read ECONNRESET 问题
  if (err.stack?.startsWith('FetchError:') || false) {
    this.logger.verbose('Bot restart...');
    // 重启
    void (async () => {
      await this.bot.launch();
    })();
  }
});

发表评论


*