forked from projectmoon/tenebrous-dicebot
Remove ExecutionError, as it is unnecessary.
This commit is contained in:
parent
1ebd13e912
commit
4ae871224a
|
@ -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::context::{Context, RoomContext};
|
||||||
use crate::db::sqlite::Database;
|
use crate::db::sqlite::Database;
|
||||||
use crate::error::BotError;
|
use crate::error::BotError;
|
||||||
|
@ -34,7 +34,7 @@ pub(super) async fn handle_multiple_results(
|
||||||
respond_to, respond_to
|
respond_to, respond_to
|
||||||
);
|
);
|
||||||
|
|
||||||
let errors: Vec<(&str, &ExecutionError)> = results
|
let errors: Vec<(&str, &BotError)> = results
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|(cmd, result)| match result {
|
.filter_map(|(cmd, result)| match result {
|
||||||
Err(e) => Some((cmd.as_ref(), e)),
|
Err(e) => Some((cmd.as_ref(), e)),
|
||||||
|
@ -96,7 +96,7 @@ pub(super) async fn execute(
|
||||||
stream::iter(commands)
|
stream::iter(commands)
|
||||||
.then(|command| async move {
|
.then(|command| async move {
|
||||||
match create_context(db, client, room, sender, command).await {
|
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) => {
|
Ok(ctx) => {
|
||||||
let cmd_result = execute_command(&ctx).await;
|
let cmd_result = execute_command(&ctx).await;
|
||||||
(command.to_owned(), cmd_result)
|
(command.to_owned(), cmd_result)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::commands::{ExecutionError, ExecutionResult};
|
use crate::commands::ExecutionResult;
|
||||||
use crate::config::*;
|
use crate::config::*;
|
||||||
use crate::db::sqlite::Database;
|
use crate::db::sqlite::Database;
|
||||||
use crate::db::DbState;
|
use crate::db::DbState;
|
||||||
|
@ -129,10 +129,7 @@ impl DiceBot {
|
||||||
let results: Vec<(String, ExecutionResult)> = if commands.len() < MAX_COMMANDS_PER_MESSAGE {
|
let results: Vec<(String, ExecutionResult)> = if commands.len() < MAX_COMMANDS_PER_MESSAGE {
|
||||||
command_execution::execute(commands, &self.db, &self.client, room, sender).await
|
command_execution::execute(commands, &self.db, &self.client, room, sender).await
|
||||||
} else {
|
} else {
|
||||||
vec![(
|
vec![("".to_owned(), Err(BotError::MessageTooLarge))]
|
||||||
"".to_owned(),
|
|
||||||
Err(ExecutionError(BotError::MessageTooLarge)),
|
|
||||||
)]
|
|
||||||
};
|
};
|
||||||
|
|
||||||
results
|
results
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use super::{Command, Execution, ExecutionError, ExecutionResult};
|
use super::{Command, Execution, ExecutionResult};
|
||||||
use crate::db::Users;
|
use crate::db::Users;
|
||||||
use crate::error::BotError::{AccountDoesNotExist, PasswordCreationError};
|
use crate::error::BotError::{AccountDoesNotExist, PasswordCreationError};
|
||||||
use crate::logic::hash_password;
|
use crate::logic::hash_password;
|
||||||
|
@ -35,7 +35,7 @@ impl Command for RegisterCommand {
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
if ctx.account.is_registered() {
|
if ctx.account.is_registered() {
|
||||||
return Err(ExecutionError(BotError::AccountAlreadyExists));
|
return Err(BotError::AccountAlreadyExists);
|
||||||
}
|
}
|
||||||
|
|
||||||
let user = User {
|
let user = User {
|
||||||
|
|
|
@ -3,7 +3,6 @@ use crate::error::BotError;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use BotError::DataError;
|
|
||||||
|
|
||||||
pub mod basic_rolling;
|
pub mod basic_rolling;
|
||||||
pub mod cofd;
|
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
|
/// Wraps either a successful command execution response, or an error
|
||||||
/// that occurred.
|
/// 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
|
/// Extract response messages out of a type, whether it is success or
|
||||||
/// failure.
|
/// failure.
|
||||||
|
@ -90,7 +69,9 @@ impl ResponseExtractor for ExecutionResult {
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
Ok(resp) => format!("<p>{}</p><p>{}</p>", username, resp.html).replace("\n", "<br/>"),
|
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) {
|
let result = match execution_allowed(cmd.as_ref(), ctx) {
|
||||||
Ok(_) => cmd.execute(ctx).await,
|
Ok(_) => cmd.execute(ctx).await,
|
||||||
Err(e) => Err(ExecutionError(e.into())),
|
Err(e) => Err(e.into()),
|
||||||
};
|
};
|
||||||
|
|
||||||
log_command(cmd.as_ref(), ctx, &result);
|
log_command(cmd.as_ref(), ctx, &result);
|
||||||
|
|
Loading…
Reference in New Issue