From 500a04fa177f5bd3a481c08a1446654efd14029c Mon Sep 17 00:00:00 2001 From: monochromer Date: Wed, 13 Dec 2023 19:41:31 +0500 Subject: [PATCH] Add error hander --- scripts/main.js | 10 +++++++--- scripts/worker.js | 11 +++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/scripts/main.js b/scripts/main.js index 6b21c29..38b0991 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -2,7 +2,7 @@ import { formatFileSize, parseFileName } from './utils.js'; const worker = new Worker(new URL('worker.js', import.meta.url)) function subsetFont(file, options) { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const opts = { 'desubroutinize': false, 'no-hinting': false, @@ -29,9 +29,13 @@ function subsetFont(file, options) { worker.addEventListener('message', function onWorkerMessage(event) { const { type, payload } = event.data - if (type === 'file' && fileId === payload.fileId) { + if (fileId === payload.fileId) { worker.removeEventListener('message', onWorkerMessage) - resolve(payload) + if (type === 'file' ) { + resolve(payload) + } else if (type === 'error') { + reject(payload.error) + } } }) }) diff --git a/scripts/worker.js b/scripts/worker.js index c1a9d7e..a3b4824 100644 --- a/scripts/worker.js +++ b/scripts/worker.js @@ -69,9 +69,9 @@ pythonSourcePromise = loadPythonSource() .catch(console.error) self.addEventListener('message', async (event) => { - try { - const { type, payload } = event.data + const { type, payload } = event.data + try { if (type === 'file') { const pythonSource = await pythonSourcePromise const fileBuffer = await payload.file.arrayBuffer() @@ -87,5 +87,12 @@ self.addEventListener('message', async (event) => { } } catch (error) { console.error(error) + self.postMessage({ + type: 'error', + payload: { + fileId: payload.fileId, + error + } + }) } })