forked from projectmoon/tenebrous-dicebot
Extract Ruma error messages from matrix SDK errors.
This commit is contained in:
parent
88db00cc3d
commit
7a302c4489
16
src/bot.rs
16
src/bot.rs
|
@ -7,6 +7,7 @@ use crate::state::DiceBotState;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use dirs;
|
use dirs;
|
||||||
use log::{debug, error, info, trace, warn};
|
use log::{debug, error, info, trace, warn};
|
||||||
|
use matrix_sdk::Error as MatrixError;
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
self,
|
self,
|
||||||
events::{
|
events::{
|
||||||
|
@ -56,6 +57,15 @@ fn create_client(config: &Config) -> Result<Client, BotError> {
|
||||||
Ok(Client::new_with_config(homeserver_url, client_config)?)
|
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 {
|
impl DiceBot {
|
||||||
/// Create a new dicebot with the given configuration and state
|
/// Create a new dicebot with the given configuration and state
|
||||||
/// actor. This function returns a Result because it is possible
|
/// 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;
|
let result = self.client.room_send(&room_id, response, None).await;
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
error!("Error sending message: {}", e.to_string());
|
let message = extract_error_message(e);
|
||||||
|
error!("Error sending message: {}", message);
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
let message = format!("{}: Executed {} commands", sender_username, results.len());
|
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;
|
let result = self.client.room_send(&room_id, response, None).await;
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
error!("Error sending message: {}", e.to_string());
|
let message = extract_error_message(e);
|
||||||
|
error!("Error sending message: {}", message);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue