Skip to content

Commit f54ec57

Browse files
committed
Preserve error name across web worker boundary so rejections keep DOMException type
1 parent 3bce2b1 commit f54ec57

4 files changed

Lines changed: 54 additions & 3 deletions

File tree

dist/msrcrypto.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,19 @@ if (runningInWorkerInstance) {
900900
return msrcryptoWorker.returnResult({ initialized: true });
901901
}
902902

903-
if (workerInitialized === true) { msrcryptoWorker.jsCryptoRunner(e); }
903+
if (workerInitialized === true) {
904+
try {
905+
msrcryptoWorker.jsCryptoRunner(e);
906+
} catch (ex) {
907+
msrcryptoWorker.returnResult({
908+
error: {
909+
name: (ex && ex.name) || "OperationError",
910+
message: (ex && ex.message) || "",
911+
code: (ex && ex.code) || 0
912+
}
913+
});
914+
}
915+
}
904916

905917
};
906918
}
@@ -9295,6 +9307,15 @@ var workerManager = (function() {
92959307

92969308
e.target || (e.target = { data: worker.data });
92979309

9310+
if (e.data.error) {
9311+
jobCompleted(worker);
9312+
op.dispatchEvent({
9313+
type: "error",
9314+
data: utils.error(e.data.error.name, e.data.error.message)
9315+
});
9316+
return;
9317+
}
9318+
92989319
for (var i = 0; i < jobQueue.length; i++) {
92999320
if (jobQueue[i].operation === worker.operation) {
93009321
var job = jobQueue[i];

dist/msrcrypto.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/subtle/workerManager.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,19 @@ var workerManager = (function() {
226226
// tslint:disable-next-line: no-unused-expression
227227
e.target || (e.target = { data: worker.data });
228228

229+
// A thrown error inside the worker is posted back as a structured
230+
// envelope (a native ErrorEvent loses the original name/code).
231+
// Rebuild a proper DOMException and reject this operation, mirroring
232+
// the synchronous syncWorker error path.
233+
if (e.data.error) {
234+
jobCompleted(worker);
235+
op.dispatchEvent({
236+
type: "error",
237+
data: utils.error(e.data.error.name, e.data.error.message)
238+
});
239+
return;
240+
}
241+
229242
// Check if there are queued jobs for this operation
230243
for (var i = 0; i < jobQueue.length; i++) {
231244
if (jobQueue[i].operation === worker.operation) {

src/worker.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,24 @@ if (runningInWorkerInstance) {
106106
}
107107

108108
// Process the crypto operation
109-
if (workerInitialized === true) { msrcryptoWorker.jsCryptoRunner(e); }
109+
if (workerInitialized === true) {
110+
try {
111+
msrcryptoWorker.jsCryptoRunner(e);
112+
} catch (ex) {
113+
// A real web worker surfaces a thrown error as an ErrorEvent on
114+
// the main thread, stripped of the original name/code (and it
115+
// also bubbles as an uncaught error). Post a serializable
116+
// envelope instead so the worker manager can rebuild a proper
117+
// DOMException, matching the synchronous syncWorker path.
118+
msrcryptoWorker.returnResult({
119+
error: {
120+
name: (ex && ex.name) || "OperationError",
121+
message: (ex && ex.message) || "",
122+
code: (ex && ex.code) || 0
123+
}
124+
});
125+
}
126+
}
110127

111128
};
112129
}

0 commit comments

Comments
 (0)