From 631e9248786f87a5b2f7892fc73a53543744d288 Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Tue, 1 Feb 2022 08:32:07 -0600 Subject: [PATCH] Add try/catch w/ error message to plugin calls --- src/core/init/lifecycle.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/core/init/lifecycle.js b/src/core/init/lifecycle.js index c04fc1a72..cb2d967b4 100644 --- a/src/core/init/lifecycle.js +++ b/src/core/init/lifecycle.js @@ -36,14 +36,29 @@ export function Lifecycle(Base) { if (index >= queue.length) { next(data); } else if (typeof hookFn === 'function') { + const errTitle = `Docsify plugin ${ + hookFn.name ? '"' + hookFn.name + '"' : '' + } error (${hookName})`; + if (hookFn.length === 2) { - hookFn(data, result => { - data = result; - step(index + 1); - }); + try { + hookFn(data, result => { + data = result; + }); + } catch (err) { + console.error(errTitle, err); + } + step(index + 1); } else { - const result = hookFn(data); - data = result === undefined ? data : result; + let result; + + try { + result = hookFn(data); + } catch (err) { + console.error(errTitle, err); + } + + data = result || data; step(index + 1); } } else {