From d0a1f59ec74241a6be3c9ee0641ea89fd5a1a403 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 16 Oct 2020 21:30:02 +0000 Subject: [PATCH] Better error message when dice pool variable not defined. --- src/cofd/dice.rs | 13 ++++++++++++- src/error.rs | 4 ++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index 3c5a587..db645e7 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -1,9 +1,17 @@ use crate::context::Context; +use crate::db::DataError::KeyDoesNotExist; use crate::error::BotError; use crate::roll::{Roll, Rolled}; use itertools::Itertools; use std::convert::TryFrom; use std::fmt; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum DiceRollingError { + #[error("variable not found: {0}")] + VariableNotFound(String), +} #[derive(Debug, PartialEq, Eq, Clone)] pub enum Operator { @@ -350,7 +358,10 @@ fn roll_die(roller: &mut R, pool: &DicePool) -> Vec { fn handle_variable(ctx: &Context, variable: &str) -> Result { ctx.db .get_user_variable(&ctx.room_id, &ctx.username, variable) - .map_err(|e| e.into()) + .map_err(|e| match e { + KeyDoesNotExist(_) => DiceRollingError::VariableNotFound(variable.to_owned()).into(), + _ => e.into(), + }) } ///Roll the dice in a dice pool, according to behavior documented in the various rolling diff --git a/src/error.rs b/src/error.rs index fa34ac5..f16292f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,4 @@ +use crate::cofd::dice::DiceRollingError; use crate::commands::CommandError; use crate::config::ConfigError; use crate::db::DataError; @@ -55,6 +56,9 @@ pub enum BotError { #[error("dice parsing error: {0}")] DiceParsingError(#[from] crate::cofd::parser::DiceParsingError), + #[error("dice pool roll error: {0}")] + DicePoolError(#[from] DiceRollingError), + #[error("variable parsing error: {0}")] VariableParsingError(#[from] crate::variables::VariableParsingError),