Better error message when dice pool variable not defined.

This commit is contained in:
projectmoon 2020-10-16 21:30:02 +00:00 committed by ProjectMoon
parent af2e58351f
commit d0a1f59ec7
2 changed files with 16 additions and 1 deletions

View File

@ -1,9 +1,17 @@
use crate::context::Context; use crate::context::Context;
use crate::db::DataError::KeyDoesNotExist;
use crate::error::BotError; use crate::error::BotError;
use crate::roll::{Roll, Rolled}; use crate::roll::{Roll, Rolled};
use itertools::Itertools; use itertools::Itertools;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt; use std::fmt;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum DiceRollingError {
#[error("variable not found: {0}")]
VariableNotFound(String),
}
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub enum Operator { pub enum Operator {
@ -350,7 +358,10 @@ fn roll_die<R: DieRoller>(roller: &mut R, pool: &DicePool) -> Vec<i32> {
fn handle_variable(ctx: &Context, variable: &str) -> Result<i32, BotError> { fn handle_variable(ctx: &Context, variable: &str) -> Result<i32, BotError> {
ctx.db ctx.db
.get_user_variable(&ctx.room_id, &ctx.username, variable) .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 ///Roll the dice in a dice pool, according to behavior documented in the various rolling

View File

@ -1,3 +1,4 @@
use crate::cofd::dice::DiceRollingError;
use crate::commands::CommandError; use crate::commands::CommandError;
use crate::config::ConfigError; use crate::config::ConfigError;
use crate::db::DataError; use crate::db::DataError;
@ -55,6 +56,9 @@ pub enum BotError {
#[error("dice parsing error: {0}")] #[error("dice parsing error: {0}")]
DiceParsingError(#[from] crate::cofd::parser::DiceParsingError), DiceParsingError(#[from] crate::cofd::parser::DiceParsingError),
#[error("dice pool roll error: {0}")]
DicePoolError(#[from] DiceRollingError),
#[error("variable parsing error: {0}")] #[error("variable parsing error: {0}")]
VariableParsingError(#[from] crate::variables::VariableParsingError), VariableParsingError(#[from] crate::variables::VariableParsingError),