You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron 6.0.9调用autoUpdater.checkForUpdates无网络时崩溃求助

Fixing Electron Updater Crash on Offline Update Checks

Let’s break down why your try-catch isn’t stopping the crash and how to fix this issue:

First, the core problem: autoUpdater.checkForUpdates() returns a Promise, not a synchronous function that throws an error immediately. Your basic try-catch block only catches synchronous errors—it can’t catch the rejected Promise that happens when there’s no network connection. That’s why you’re seeing the UnhandledPromiseRejectionWarning: the Promise is being rejected but no code is handling it, which in older Electron/Node.js versions can terminate the entire process.

Here are actionable solutions to resolve this:

1. Use Promise .catch() directly

Attach a .catch() handler to the Promise returned by checkForUpdates() to explicitly handle the rejection:

autoUpdater.checkForUpdates()
  .then(updateResult => {
    // Optional: Handle successful update check logic if needed
    console.log('Update check completed:', updateResult);
  })
  .catch(e => {
    logEverywhere('Error, Failed to check for updates!', e);
  });

2. Use async/await with try-catch

Wrap the update check in an async function so you can use await—this lets your try-catch block handle the Promise rejection as if it were a synchronous error:

async function runUpdateCheck() {
  try {
    const updateResult = await autoUpdater.checkForUpdates();
    // Optional: Handle successful check
    console.log('Update check result:', updateResult);
  } catch(e) {
    logEverywhere('Error, Failed to check for updates!', e);
  }
}

// Call the async function to trigger the check
runUpdateCheck();

3. Add an error event listener (extra safety layer)

For broader error coverage, listen to the error event emitted by autoUpdater—this will catch any other update-related errors that might occur:

autoUpdater.on('error', (error) => {
  logEverywhere('Update process encountered an error:', error.message);
});

Bonus: Avoid unnecessary calls by checking network status

To skip the update check entirely when offline, you can first verify network access:

  • In the main process (Node.js context), use Electron’s net module:
    const { net } = require('electron');
    
    const hasNetwork = () => {
      return new Promise(resolve => {
        const testRequest = net.request('https://example.com');
        testRequest.on('response', () => resolve(true));
        testRequest.on('error', () => resolve(false));
        testRequest.end();
      });
    };
    
    // Usage
    hasNetwork().then(online => {
      if (online) {
        autoUpdater.checkForUpdates().catch(e => logEverywhere(e));
      } else {
        logEverywhere('No network connection—skipping update check');
      }
    });
    
  • In the renderer process, use the browser’s built-in API:
    if (navigator.onLine) {
      // Trigger update check (via IPC to main process if needed)
    } else {
      logEverywhere('Offline—update check skipped');
    }
    

Keep in mind that in Electron 6.x and older, unhandled Promise rejections can force the process to exit, so always ensure every Promise’s rejection path is handled.

内容的提问来源于stack exchange,提问作者Freddy Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:42:17