Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent unhandledRejection if --open fails #16726

Merged
merged 3 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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