Remove ExecutionError, as it is unnecessary.

This commit is contained in:
projectmoon 2021-05-26 21:25:32 +00:00
parent 1ebd13e912
commit 4ae871224a
4 changed files with 12 additions and 34 deletions

View File

@ -1,4 +1,4 @@
use crate::commands::{execute_command, ExecutionError, ExecutionResult, ResponseExtractor};
use crate::commands::{execute_command, ExecutionResult, ResponseExtractor};
use crate::context::{Context, RoomContext};
use crate::db::sqlite::Database;
use crate::error::BotError;
@ -34,7 +34,7 @@ pub(super) async fn handle_multiple_results(
respond_to, respond_to
);
let errors: Vec<(&str, &ExecutionError)> = results
let errors: Vec<(&str, &BotError)> = results
.into_iter()
.filter_map(|(cmd, result)| match result {
Err(e) => Some((cmd.as_ref(), e)),
@ -96,7 +96,7 @@ pub(super) async fn execute(
stream::iter(commands)
.then(|command| async move {
match create_context(db, client, room, sender, command).await {
Err(e) => (command.to_owned(), Err(ExecutionError(e))),
Err(e) => (command.to_owned(), Err(e)),
Ok(ctx) => {
let cmd_result = execute_command(&ctx).await;
(command.to_owned(), cmd_result)

View File

@ -1,4 +1,4 @@
use crate::commands::{ExecutionError, ExecutionResult};
use crate::commands::ExecutionResult;
use crate::config::*;
use crate::db::sqlite::Database;
use crate::db::DbState;
@ -129,10 +129,7 @@ impl DiceBot {
let results: Vec<(String, ExecutionResult)> = if commands.len() < MAX_COMMANDS_PER_MESSAGE {
command_execution::execute(commands, &self.db, &self.client, room, sender).await
} else {
vec![(
"".to_owned(),
Err(ExecutionError(BotError::MessageTooLarge)),
)]
vec![("".to_owned(), Err(BotError::MessageTooLarge))]
};
results

View File

@ -1,4 +1,4 @@
use super::{Command, Execution, ExecutionError, ExecutionResult};
use super::{Command, Execution, ExecutionResult};
use crate::db::Users;
use crate::error::BotError::{AccountDoesNotExist, PasswordCreationError};
use crate::logic::hash_password;
@ -35,7 +35,7 @@ impl Command for RegisterCommand {
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
if ctx.account.is_registered() {
return Err(ExecutionError(BotError::AccountAlreadyExists));
return Err(BotError::AccountAlreadyExists);
}
let user = User {

View File

@ -3,7 +3,6 @@ use crate::error::BotError;
use async_trait::async_trait;
use log::{error, info};
use thiserror::Error;
use BotError::DataError;
pub mod basic_rolling;
pub mod cofd;
@ -46,29 +45,9 @@ impl Execution {
}
}
/// Wraps a command execution failure. Provides HTML formatting for
/// any error message from the BotError type, similar to how Execution
/// provides formatting for successfully executed commands.
#[derive(Error, Debug)]
#[error("{0}")]
pub struct ExecutionError(#[from] pub BotError);
impl From<crate::db::errors::DataError> for ExecutionError {
fn from(error: crate::db::errors::DataError) -> Self {
Self(DataError(error))
}
}
impl ExecutionError {
/// Error message in bolded HTML.
pub fn html(&self) -> String {
format!("<strong>{}</strong>", self.0)
}
}
/// Wraps either a successful command execution response, or an error
/// that occurred.
pub type ExecutionResult = Result<Execution, ExecutionError>;
pub type ExecutionResult = Result<Execution, BotError>;
/// Extract response messages out of a type, whether it is success or
/// failure.
@ -90,7 +69,9 @@ impl ResponseExtractor for ExecutionResult {
match self {
Ok(resp) => format!("<p>{}</p><p>{}</p>", username, resp.html).replace("\n", "<br/>"),
Err(e) => format!("<p>{}</p><p>{}</p>", username, e.html()).replace("\n", "<br/>"),
Err(e) => {
format!("<p>{}</p><p><strong>{}</strong></p>", username, e).replace("\n", "<br/>")
}
}
}
}
@ -125,7 +106,7 @@ pub async fn execute_command(ctx: &Context<'_>) -> ExecutionResult {
let result = match execution_allowed(cmd.as_ref(), ctx) {
Ok(_) => cmd.execute(ctx).await,
Err(e) => Err(ExecutionError(e.into())),
Err(e) => Err(e.into()),
};
log_command(cmd.as_ref(), ctx, &result);