Skip to content

Commit 1f60647

Browse files
authored
fix: prevent unhandledRejection if --open fails (#16726)
1 parent c735cc7 commit 1f60647

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

packages/vite/src/node/server/openBrowser.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function openBrowser(
3434
const browserArgs = process.env.BROWSER_ARGS
3535
? process.env.BROWSER_ARGS.split(' ')
3636
: []
37-
startBrowserProcess(browser, browserArgs, url)
37+
startBrowserProcess(browser, browserArgs, url, logger)
3838
}
3939
}
4040

@@ -72,6 +72,7 @@ async function startBrowserProcess(
7272
browser: string | undefined,
7373
browserArgs: string[],
7474
url: string,
75+
logger: Logger,
7576
) {
7677
// If we're on OS X, the user hasn't specifically
7778
// requested a different browser, we can try opening
@@ -122,7 +123,17 @@ async function startBrowserProcess(
122123
const options: open.Options = browser
123124
? { app: { name: browser, arguments: browserArgs } }
124125
: {}
125-
open(url, options).catch(() => {}) // Prevent `unhandledRejection` error.
126+
127+
new Promise((_, reject) => {
128+
open(url, options)
129+
.then((subprocess) => {
130+
subprocess.on('error', reject)
131+
})
132+
.catch(reject)
133+
}).catch((err) => {
134+
logger.error(err.stack || err.message)
135+
})
136+
126137
return true
127138
} catch (err) {
128139
return false

0 commit comments

Comments
 (0)