From 83462a4663e9f4ab591ead3142b1ca6c77b851da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Thu, 16 Apr 2026 22:58:31 -0400 Subject: [PATCH] refactor(browser-action): extract getApprovalOrNull helper to drop nested try/catch --- src/main/browser-action-service.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/browser-action-service.ts b/src/main/browser-action-service.ts index 3e2c1166..ebab0dee 100644 --- a/src/main/browser-action-service.ts +++ b/src/main/browser-action-service.ts @@ -20,6 +20,19 @@ export function createBrowserActionService({ return browserManagerRef.current; } + /** + * Returns the approval record for `approvalId`, or null if the record is + * missing. Isolates the "approval row disappeared" case so the caller does + * not need to nest a try/catch around the state machine. + */ + function getApprovalOrNull(approvalId: string): ReturnType | null { + try { + return db.getApprovalRecord(approvalId); + } catch { + return null; + } + } + async function dispatchBrowserAction(actionId: string, companyId: string) { const action = db.getBrowserAction(actionId, companyId); if (!action) { @@ -29,12 +42,7 @@ export function createBrowserActionService({ throw new Error(`Browser action ${actionId} is ${action.status} and cannot be dispatched.`); } if (action.approvalId) { - let approval: ReturnType | null = null; - try { - approval = db.getApprovalRecord(action.approvalId); - } catch { - // Record not found - } + const approval = getApprovalOrNull(action.approvalId); if (!approval || approval.companyId !== companyId) { throw new Error(`Browser action approval ${action.approvalId} does not belong to company ${companyId}.`); }