From fbee8708a288d35d46edf87bd2f325d833b18083 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Wed, 27 May 2020 21:45:33 -0500 Subject: [PATCH] Event handler error handling + if the config is not found, return and log it --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- src/main.rs | 23 ++++++++++++++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 740b6f4..84471b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -531,7 +531,7 @@ dependencies = [ "log4rs 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", - "serenity 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)", + "serenity 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)", "strfmt 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "white_rabbit 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1509,7 +1509,7 @@ dependencies = [ [[package]] name = "serenity" -version = "0.8.4" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "base64 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2220,7 +2220,7 @@ dependencies = [ "checksum serde_test 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "110b3dbdf8607ec493c22d5d947753282f3bae73c0f56d322af1e8c78e4c23d5" "checksum serde_urlencoded 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9ec5d77e2d4c73717816afac02670d5c4f534ea95ed430442cad02e7a6e32c97" "checksum serde_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)" = "691b17f19fc1ec9d94ec0b5864859290dff279dbd7b03f017afda54eb36c3c35" -"checksum serenity 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)" = "37d44aa085b004e306fe41feb4658d0cf8ae880664c00b29bdb8f91fdef27cca" +"checksum serenity 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3dcb21751f749cd60c0779dddb08a8d339d87b79318c536eaf875b3985c22c" "checksum sha-1 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df" "checksum skeptic 0.13.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6fb8ed853fdc19ce09752d63f3a2e5b5158aeb261520cd75eb618bd60305165" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" diff --git a/Cargo.toml b/Cargo.toml index 59b64f6..9cdd1cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,5 +22,5 @@ url = "2.1.1" white_rabbit = "0.1.1" [dependencies.serenity] -version = "0.8.4" +version = "0.8.6" features = ["framework", "standard_framework"] diff --git a/src/main.rs b/src/main.rs index e8fefed..174006f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,7 +64,13 @@ struct Handler; impl EventHandler for Handler { /// On reaction add fn reaction_add(&self, ctx: Context, reaction: Reaction) { - let config = get_config(&ctx.data).unwrap(); + let config = match get_config(&ctx.data) { + Ok(config) => config, + Err(e) => { + error!("Unable to get config: {}", e.0); + return; + } + }; if reaction.channel_id.0 == config.event_channel && reaction.emoji.as_data() == INTERESTED_EMOJI { send_message_to_reaction_users( &ctx, @@ -76,7 +82,13 @@ impl EventHandler for Handler { /// On reaction remove fn reaction_remove(&self, ctx: Context, reaction: Reaction) { - let config = get_config(&ctx.data).unwrap(); + let config = match get_config(&ctx.data) { + Ok(config) => config, + Err(e) => { + error!("Unable to get config: {}", e.0); + return; + } + }; if reaction.channel_id.0 == config.event_channel && reaction.emoji.as_data() == INTERESTED_EMOJI { send_message_to_reaction_users( &ctx, @@ -111,16 +123,17 @@ fn setup_logging(config: &HypeBotConfig) -> HypeBotResult<()> { // Build log file path let log_file_path = Path::new(&config.log_path); let log_file_path = log_file_path.join("hype_bot.log"); + let archive = log_file_path.join("hype_bot.{}.log"); // Number of logs to keep let window_size = 10; // 10MB file size limit - let size_limit = 1 * 1024 * 1024; + let size_limit = 10 * 1024 * 1024; let size_trigger = SizeTrigger::new(size_limit); let fixed_window_roller = FixedWindowRoller::builder() - .build("hype_bot.{}.log", window_size) + .build(archive.to_str().unwrap(), window_size) .unwrap(); let compound_policy = @@ -236,7 +249,7 @@ fn main() -> HypeBotResult<()> { }); // Create scheduler - let scheduler = Scheduler::new(4); + let scheduler = Scheduler::new(2); let scheduler = Arc::new(RwLock::new(scheduler)); data.insert::(scheduler); }