From 9a5a9aa47fd9a6c7ea6830a821689321e8fd9fbb Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 9 Sep 2017 11:36:50 -0700 Subject: better error handling --- ext/bg/js/api.js | 7 ++++++- ext/sb/js/sandbox.js | 13 +++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 5a307df3..6ab130a7 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -104,7 +104,12 @@ async function apiTemplateRender(template, data, dynamic) { const sequence = utilBackend().sequenceNew(); const handler = event => { if (event.data.sequence === sequence) { - resolve(event.data.result); + if (event.data.command === 'error') { + reject(event.data.result); + } else { + resolve(event.data.result); + } + window.removeEventListener('message', handler); } }; diff --git a/ext/sb/js/sandbox.js b/ext/sb/js/sandbox.js index 0a0ff31b..c3430afe 100644 --- a/ext/sb/js/sandbox.js +++ b/ext/sb/js/sandbox.js @@ -31,7 +31,16 @@ window.addEventListener('message', event => { window.yomichan_cache[event.data.template] = template; } - const result = template(event.data.data || {}); - event.source.postMessage({result, sequence: event.data.sequence}, '*'); + let result = null; + let command = null; + try { + command = 'render'; + result = template(event.data.data || {}); + } catch (e) { + command = 'error'; + result = e; + } + + event.source.postMessage({result, command, sequence: event.data.sequence}, '*'); } }); -- cgit v1.2.3