From 0f2c599c242b099d7bd3811314cc95486fffca7c Mon Sep 17 00:00:00 2001 From: Tim <27028176+twiescha@users.noreply.github.com> Date: Thu, 26 Oct 2023 18:14:53 +0200 Subject: [PATCH] fix(dicetray): error handling for roll button (#264) --- src/view/view.ts | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/view/view.ts b/src/view/view.ts index 3f0a31b..c8971a5 100644 --- a/src/view/view.ts +++ b/src/view/view.ts @@ -245,28 +245,31 @@ export default class DiceView extends ItemView { if (opts.expectedValue == ExpectedValue.None) { opts.expectedValue = ExpectedValue.Roll; } - const roller = await this.plugin.getRoller(formula, "view", opts); - - if (!(roller instanceof StackRoller)) { - new Notice("The Dice Tray only supports dice rolls."); - return; - } - roller.iconEl.detach(); - roller.containerEl.onclick = null; - roller.buildDiceTree(); - if (!roller.dice.length) { - new Notice("Invalid formula."); - return; + try { + const roller = + await this.plugin.getRoller(formula, "view", opts) + .catch((e) => { throw e }); + if (!(roller instanceof StackRoller)) { + throw new Error("The Dice Tray only supports dice rolls.") + } + roller.iconEl.detach(); + roller.containerEl.onclick = null; + roller.buildDiceTree(); + if (!roller.dice.length) { + throw new Error("No dice.") + } + await roller.roll(this.plugin.data.renderer) + .catch((e) => { throw e }); + this.addResult(roller); + } catch (e) { + new Notice("Invalid Formula: " + e.message); + } finally { + this.rollButton.setDisabled(false); + this.buildButtons(); + this.#formula = new Map(); + this.#add = 0; + this.setFormula(); } - await roller.roll(this.plugin.data.renderer); - this.rollButton.setDisabled(false); - - this.addResult(roller); - - this.buildButtons(); - this.#formula = new Map(); - this.#add = 0; - this.setFormula(); } buildFormula() { this.formulaEl.empty();