Skip to content

Commit

Permalink
fix: prevent unhandledRejection if --open fails (#16726)
Browse files Browse the repository at this point in the history
  • Loading branch information
gtm-nayan authored May 30, 2024
1 parent c735cc7 commit 1f60647
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/vite/src/node/server/openBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function openBrowser(
const browserArgs = process.env.BROWSER_ARGS
? process.env.BROWSER_ARGS.split(' ')
: []
startBrowserProcess(browser, browserArgs, url)
startBrowserProcess(browser, browserArgs, url, logger)
}
}

Expand Down Expand Up @@ -72,6 +72,7 @@ async function startBrowserProcess(
browser: string | undefined,
browserArgs: string[],
url: string,
logger: Logger,
) {
// If we're on OS X, the user hasn't specifically
// requested a different browser, we can try opening
Expand Down Expand Up @@ -122,7 +123,17 @@ async function startBrowserProcess(
const options: open.Options = browser
? { app: { name: browser, arguments: browserArgs } }
: {}
open(url, options).catch(() => {}) // Prevent `unhandledRejection` error.

new Promise((_, reject) => {
open(url, options)
.then((subprocess) => {
subprocess.on('error', reject)
})
.catch(reject)
}).catch((err) => {
logger.error(err.stack || err.message)
})

return true
} catch (err) {
return false
Expand Down

0 comments on commit 1f60647

Please sign in to comment.