Skip to content

Commit 0f82ecd

Browse files
author
octo-patch
committed
fix(commands): also catch synchronous throws in command action
`Browser.commands.onCommand`'s `tab` parameter is documented as optional, so an action that dereferences `tab.*` (e.g. `openSidePanel` reading `tab.windowId` / `tab.id`) can throw synchronously before returning a Promise. The previous `.catch` only observed Promise rejections, so a synchronous throw would still surface as an uncaught error in the background script. Wrap the action invocation in try/catch and log+return on synchronous failure, mirroring the async rejection handling. Addresses CodeRabbit review on PR #963.
1 parent 810e8d3 commit 0f82ecd

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/background/commands.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ export function registerCommands() {
1616
// chrome.sidePanel.open() Promise). Keep the call synchronous so the
1717
// user-gesture context is preserved, but observe the Promise so a
1818
// rejection does not become an unhandled rejection in the background.
19-
const result = menuConfig[command].action(true, tab)
19+
// Also wrap in try/catch because Browser.commands.onCommand documents
20+
// `tab` as optional, so an action that dereferences tab.* (e.g. the
21+
// openSidePanel call) can throw synchronously.
22+
let result
23+
try {
24+
result = menuConfig[command].action(true, tab)
25+
} catch (error) {
26+
console.error(`failed to run command action "${command}"`, error)
27+
return
28+
}
2029
if (result && typeof result.catch === 'function') {
2130
result.catch((error) => {
2231
console.error(`failed to run command action "${command}"`, error)

0 commit comments

Comments
 (0)