Skip to content

Commit 083a1e2

Browse files
authored
[Comlink] Throw the original error in the error handler (#2407)
## Motivation for the change, related issues The [new Comlink error handler](#2357) rewired the logic around passing errors from web workers to the Playground web app. Specifically, it enriched the error with additional stack trace before rethrowing it: ```js throw new Error('Comlink method call failed', { cause: originalError }); ``` Unfortunately, this also put the original error at the bottom of the `error.cause` chain, making error handling confusing for the API consumers. This PR ensures the thrown error the original one. This way a simple `try {} catch(e) {}` is enough to log the error details without having to reason about `e.cause.cause` etc. The additional stack trace information is now attached at the bottom of the cause chain for when the developer needs to inspect it. ## Testing Instructions (or ideally a Blueprint) Run Playground with this Blueprint and confirm the top-level error message speaks about PHP failure. Specifically, it should not say "Comlink method call failed": ```json { "steps": [ { "step": "mkdir", "path": "/wordpress/wp-content/mu-plugins" }, { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/test.php", "data": "<?php undefined_function();" } ] } ```
1 parent a875920 commit 083a1e2

File tree

1 file changed

+15
-4
lines changed
  • packages/php-wasm/universal/src/lib

1 file changed

+15
-4
lines changed

packages/php-wasm/universal/src/lib/api.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,22 @@ const throwTransferHandlerCustom: Comlink.TransferHandler<
278278
if (serialized.isError) {
279279
const error = ErrorSerializer.deserializeError(serialized.value);
280280
/**
281-
* Rethrow to capture the stack trace of the original Comlink call.
281+
* The original error from the web worker does not include any call
282+
* stack from the Playground web app. Let's include that information
283+
* in the error chain.
284+
*
285+
* We'll place it at the bottom of the error chain. This way the API
286+
* consumer gets the original error object and not an opaque
287+
* "Comlink method call failed" error, but they can still inspect
288+
* it further to see the full call stack.
282289
*/
283-
throw new Error('Comlink method call failed', {
284-
cause: error,
285-
});
290+
const additionalCallStack = new Error('Comlink method call failed');
291+
let deepestError = error;
292+
while (deepestError.cause) {
293+
deepestError = deepestError.cause;
294+
}
295+
deepestError.cause = additionalCallStack;
296+
throw error;
286297
}
287298
throw serialized.value;
288299
},

0 commit comments

Comments
 (0)