show error when mount() fails in SwitchView

This commit is contained in:
Bruno Windels 2020-04-22 20:53:18 +02:00
parent 449262e3c1
commit e0799181d9
2 changed files with 20 additions and 1 deletions

View File

@ -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);

View File

@ -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),
]);
}