Skip to content

Commit dcd49c0

Browse files
authored
feat(worker): capture worker errors (#2709)
1 parent 779cdcc commit dcd49c0

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/client/client-log.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type * as d from '../declarations';
22
import { BUILD } from '@app-data';
33

4-
export let consoleError: d.ErrorHandler = (e: any, _?: any) => console.error(e);
4+
let customError: d.ErrorHandler ;
5+
export const consoleError: d.ErrorHandler = (e: any, el?: any) => (customError || console.error)(e, el);
56

67
export const STENCIL_DEV_MODE = BUILD.isTesting
78
? ['STENCIL:'] // E2E testing
@@ -13,4 +14,4 @@ export const consoleDevWarn = (...m: any[]) => console.warn(...STENCIL_DEV_MODE,
1314

1415
export const consoleDevInfo = (...m: any[]) => console.info(...STENCIL_DEV_MODE, ...m);
1516

16-
export const setErrorHandler = (handler: d.ErrorHandler) => consoleError = handler;
17+
export const setErrorHandler = (handler: d.ErrorHandler) => customError = handler;

src/compiler/bundle/worker-plugin.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Plugin, TransformResult, PluginContext } from 'rollup';
33
import { bundleOutput } from './bundle-output';
44
import { normalizeFsPath, hasError } from '@utils';
55
import { optimizeModule } from '../optimize/optimize-module';
6+
import { STENCIL_INTERNAL_ID } from './entry-alias-ids';
67

78
export const workerPlugin = (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, platform: string, inlineWorkers: boolean): Plugin => {
89
if (platform === 'worker' || platform === 'hydrate') {
@@ -248,7 +249,6 @@ addEventListener('message', async ({data}) => {
248249
249250
} catch (e) {
250251
value = null;
251-
${isDev ? 'console.error("Error when calling worker routine", e);' : ''}
252252
if (e instanceof Error) {
253253
err = {
254254
isError: true,
@@ -279,6 +279,8 @@ addEventListener('message', async ({data}) => {
279279
`;
280280

281281
export const WORKER_HELPERS = `
282+
import { consoleError } from '${STENCIL_INTERNAL_ID}';
283+
282284
let pendingIds = 0;
283285
let callbackIds = 0;
284286
const pending = new Map();
@@ -299,10 +301,12 @@ export const createWorker = (workerPath, workerName, workerMsgId) => {
299301
pending.delete(id);
300302
301303
if (err) {
302-
reject((err.isError)
304+
const errObj = (err.isError)
303305
? Object.assign(new Error(err.value.message), err.value)
304-
: err.value
305-
);
306+
: err.value;
307+
308+
consoleError(errObj);
309+
reject(errObj);
306310
} else {
307311
if (callbackIds) {
308312
callbackIds.forEach(id => callbacks.delete(id));
@@ -313,7 +317,7 @@ export const createWorker = (workerPath, workerName, workerMsgId) => {
313317
try {
314318
callbacks.get(id)(...value);
315319
} catch (e) {
316-
console.error(e);
320+
consoleError(e);
317321
}
318322
}
319323
}

0 commit comments

Comments
 (0)