Skip to content

Commit

Permalink
Enable backtraces and log VariantNotFound (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Kozarin committed Dec 18, 2024
1 parent be21342 commit 73bf555
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tinytemplate = "1.2.1"
strum = "0.26.3"
strum_macros = "0.26.4"
derive_more = { version = "1.0.0", features = ["display", "from", "constructor", "error", "from_str"] }
anyhow = "1.0.94"
anyhow = { version = "1.0.94", features = ["backtrace"] }
# Other basic stuff
regex = "1.11.1"
rand = "0.8.5"
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/import.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter};
use anyhow::anyhow;
use anyhow::{anyhow, bail};
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use rust_i18n::t;
Expand Down Expand Up @@ -247,7 +247,7 @@ async fn import_impl(repos: &repo::Repositories, chat_id: ChatId, parsed: ParseR
.map(|pos| pos.as_ref().unwrap_err().clone())
.collect();
if !invalid_lines.is_empty() {
return Err(anyhow!(InvalidLines(invalid_lines)))
bail!(InvalidLines(invalid_lines))
}

let top: Vec<OriginalUser> = top.into_iter()
Expand Down Expand Up @@ -283,7 +283,7 @@ async fn import_impl(repos: &repo::Repositories, chat_id: ChatId, parsed: ParseR
.map(|u| repo::ExternalUser::new(u.uid, u.length))
.collect();
if users.len() != to_import.len() {
return Err(anyhow!("couldn't convert integers for external users"))
bail!("couldn't convert integers for external users")
}
repos.import.import(chat_id, &users).await?;

Expand Down
1 change: 1 addition & 0 deletions src/handlers/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ fn parse_callback_data(data: &str, user_id: UserId) -> Result<CallbackDataParseR
if uid == user_id.0.to_string() {
InlineCommand::from_str(data)
.map(CallbackDataParseResult::Ok)
.inspect_err(|err| log::error!("couldn't parse callback data '{data}': {err}"))
} else {
Ok(CallbackDataParseResult::AnotherUser)
}
Expand Down
1 change: 1 addition & 0 deletions src/handlers/pvp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ pub async fn inline_chosen_handler() -> HandlerResult {
Ok(())
}

#[inline]
pub fn callback_filter(query: CallbackQuery) -> bool {
BattleCallbackData::check_prefix(query)
}
Expand Down
20 changes: 20 additions & 0 deletions src/handlers/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,23 @@ fn decode_promo_code(promo_code_base64: &str) -> anyhow::Result<String> {
let promo_code = String::from_utf8(bytes)?;
Ok(promo_code)
}

#[cfg(test)]
mod tests {
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;

// TODO: implement a separate domain type for promo codes
#[test]
fn test_encode_decode_promo_code() {
let code = "TEST_CODE";
let encoded = URL_SAFE_NO_PAD.encode(code);

let decoded_bytes = URL_SAFE_NO_PAD.decode(encoded.as_bytes())
.expect("couldn't decode the encoded promo code");
let decoded_str = String::from_utf8(decoded_bytes)
.expect("couldn't convert promo code to a string");

assert_eq!(code, decoded_str);
}
}
4 changes: 2 additions & 2 deletions src/repo/chats.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::Formatter;
use anyhow::anyhow;
use anyhow::bail;
use sqlx::{Postgres, Transaction};
use teloxide::types::ChatId;
use super::{ChatIdFull, ChatIdKind, ChatIdPartiality, ChatIdSource, ensure_only_one_row_updated};
Expand Down Expand Up @@ -71,7 +71,7 @@ repository!(Chats, with_feature_toggles,
1 => Self::update_chat(&mut tx, chats[0].internal_id, id, instance).await,
0 => Self::create_chat(&mut tx, id, instance).await,
2 => Self::merge_chats(&mut tx, [&chats[0], &chats[1]]).await,
x => Err(anyhow!("unexpected count of chats ({x}): {chats:?}")),
x => bail!("unexpected count of chats ({x}): {chats:?}"),
}?;
tx.commit().await?;
Ok(internal_id)
Expand Down

0 comments on commit 73bf555

Please sign in to comment.