From 7a302c4489803c087e0722b5934b296116b5bd98 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sat, 31 Oct 2020 13:19:13 +0000 Subject: [PATCH] Extract Ruma error messages from matrix SDK errors. --- src/bot.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 18510fe..86fadb9 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -7,6 +7,7 @@ use crate::state::DiceBotState; use async_trait::async_trait; use dirs; use log::{debug, error, info, trace, warn}; +use matrix_sdk::Error as MatrixError; use matrix_sdk::{ self, events::{ @@ -56,6 +57,15 @@ fn create_client(config: &Config) -> Result { Ok(Client::new_with_config(homeserver_url, client_config)?) } +/// Extracts more detailed error messages out of a matrix SDK error. +fn extract_error_message(error: MatrixError) -> String { + use matrix_sdk::Error::RumaResponse; + match error { + RumaResponse(ruma_error) => ruma_error.to_string(), + _ => error.to_string(), + } +} + impl DiceBot { /// Create a new dicebot with the given configuration and state /// actor. This function returns a Result because it is possible @@ -141,7 +151,8 @@ impl DiceBot { let result = self.client.room_send(&room_id, response, None).await; if let Err(e) = result { - error!("Error sending message: {}", e.to_string()); + let message = extract_error_message(e); + error!("Error sending message: {}", message); }; } else { let message = format!("{}: Executed {} commands", sender_username, results.len()); @@ -151,7 +162,8 @@ impl DiceBot { let result = self.client.room_send(&room_id, response, None).await; if let Err(e) = result { - error!("Error sending message: {}", e.to_string()); + let message = extract_error_message(e); + error!("Error sending message: {}", message); }; }