From e0799181d93d9548c4b42193fdaba78adc45d23c Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 22 Apr 2020 20:53:18 +0200 Subject: [PATCH] show error when mount() fails in SwitchView --- src/ui/web/general/SwitchView.js | 9 ++++++++- src/ui/web/general/error.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/ui/web/general/error.js diff --git a/src/ui/web/general/SwitchView.js b/src/ui/web/general/SwitchView.js index 80ee7198..66950052 100644 --- a/src/ui/web/general/SwitchView.js +++ b/src/ui/web/general/SwitchView.js @@ -1,3 +1,5 @@ +import {errorToDOM} from "./error.js"; + export class SwitchView { constructor(defaultView) { this._childView = defaultView; @@ -23,7 +25,12 @@ export class SwitchView { const oldRoot = this.root(); this._childView.unmount(); this._childView = newView; - const newRoot = this._childView.mount(); + let newRoot; + try { + newRoot = this._childView.mount(); + } catch (err) { + newRoot = errorToDOM(err); + } const parent = oldRoot.parentElement; if (parent) { parent.replaceChild(newRoot, oldRoot); diff --git a/src/ui/web/general/error.js b/src/ui/web/general/error.js new file mode 100644 index 00000000..c218504e --- /dev/null +++ b/src/ui/web/general/error.js @@ -0,0 +1,12 @@ +import {tag} from "./html.js"; + +export function errorToDOM(error) { + const stack = new Error().stack; + const callee = stack.split("\n")[1]; + return tag.div([ + tag.h2("Something went wrong…"), + tag.h3(error.message), + tag.p(`This occurred while running ${callee}.`), + tag.pre(error.stack), + ]); +}