Skip to content

Commit f21d60a

Browse files
committed
refactor(tauri): enhance error handling in execute function
- Improved error handling in the `execute` function by restructuring the parsing logic to separate error checks from the try/catch block, allowing for clearer error messages when parsing fails. - Updated related test cases to reflect the new error handling behavior, ensuring that specific error messages are thrown for parsing issues.
1 parent bb4085b commit f21d60a

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

packages/tauri-service/src/commands/execute.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,25 @@ export async function execute<ReturnValue, InnerArguments extends unknown[]>(
113113
);
114114

115115
// Extract result or error from wrapped response
116-
try {
117-
if (result && typeof result === 'string') {
118-
const parsed = JSON.parse(result) as { __wdio_error__?: string; __wdio_value__?: unknown };
119-
if (parsed.__wdio_error__) {
120-
throw new Error(parsed.__wdio_error__);
121-
}
122-
if (parsed.__wdio_value__ !== undefined) {
123-
log.debug(`Execute result:`, parsed.__wdio_value__);
124-
return parsed.__wdio_value__ as ReturnValue;
125-
}
116+
let parsed: { __wdio_error__?: string; __wdio_value__?: unknown } | undefined;
117+
if (result && typeof result === 'string') {
118+
try {
119+
parsed = JSON.parse(result) as { __wdio_error__?: string; __wdio_value__?: unknown };
120+
} catch (parseError) {
121+
throw new Error(
122+
`Failed to parse execute result: ${parseError instanceof Error ? parseError.message : String(parseError)}, raw result: ${result}`,
123+
);
126124
}
127-
} catch (parseError) {
128-
throw new Error(
129-
`Failed to parse execute result: ${parseError instanceof Error ? parseError.message : String(parseError)}, raw result: ${result}`,
130-
);
125+
}
126+
127+
// Check for script errors AFTER parsing (outside try/catch to avoid re-wrapping)
128+
if (parsed?.__wdio_error__) {
129+
throw new Error(parsed.__wdio_error__);
130+
}
131+
132+
if (parsed?.__wdio_value__ !== undefined) {
133+
log.debug(`Execute result:`, parsed.__wdio_value__);
134+
return parsed.__wdio_value__ as ReturnValue;
131135
}
132136

133137
log.debug(`Execute result:`, result);

packages/tauri-service/test/commands/execute.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,7 @@ describe('execute', () => {
294294
browser = createMockBrowser();
295295
(browser.execute as ReturnType<typeof vi.fn>).mockImplementation(mockExecute);
296296

297-
await expect(() => execute<string, []>(browser, '() => "fail"')).rejects.toThrow(
298-
/Failed to parse execute result:.*something went wrong/,
299-
);
297+
await expect(() => execute<string, []>(browser, '() => "fail"')).rejects.toThrow('something went wrong');
300298
});
301299

302300
it('should throw for window undefined error', async () => {

0 commit comments

Comments
 (0)