Skip to content

Commit

Permalink
Make error message smaller
Browse files Browse the repository at this point in the history
I've noticed that the long, noisy error messages often get in the way of conversations,
and also that users don't realize they can edit or delete their message to make it go away.
This addresses both of those issues.
  • Loading branch information
mattfbacon committed Jan 28, 2024
1 parent 71978c8 commit 95d8359
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ serenity = { version = "0.12", default_features = false, features = [
rusqlite = { version = "0.29", features = ["bundled"] }
thiserror = "1"
tokio = { version = "1", features = ["rt", "macros", "sync"] }
tracing = "0.1"
29 changes: 29 additions & 0 deletions bot/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,28 @@ async fn list_tags(
Ok(())
}

async fn handle_error(
error: poise::FrameworkError<'_, Data, Box<dyn std::error::Error + Send + Sync>>,
) -> serenity::Result<()> {
if let poise::FrameworkError::ArgumentParse {
ctx, input, error, ..
} = error
{
let name = &ctx.command().name;
let usage = format!(
"Use `?help {name}` for usage. Feel free to edit or delete your message and the bot will react.",
);
let response = input.map_or_else(
|| format!("**{error}**\n{usage}"),
|input| format!("**Cannot parse `{input}` as argument: {error}**\n{usage}"),
);
ctx.reply(response).await?;
Ok(())
} else {
poise::builtins::on_error(error).await
}
}

pub async fn run() {
let database = Connection::open_with_flags(
std::env::var_os("DB_PATH").expect("need `DB_PATH` env var"),
Expand Down Expand Up @@ -674,6 +696,13 @@ pub async fn run() {
list_tags(),
],
allowed_mentions: Some(CreateAllowedMentions::new()),
on_error: |error| {
Box::pin(async move {
if let Err(error) = handle_error(error).await {
tracing::error!(?error, "Error while handling error");
}
})
},
..Default::default()
})
.setup(|ctx, _ready, framework| {
Expand Down

0 comments on commit 95d8359

Please sign in to comment.