improved logging of errors in from_user fn

This commit is contained in:
jude 2020-10-13 13:48:47 +01:00
parent b2111616e8
commit c8ab8e66e3

View File

@ -10,6 +10,8 @@ use sqlx::{Cursor, MySqlPool, Row};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use chrono_tz::Tz; use chrono_tz::Tz;
use log::error;
use crate::consts::{LOCAL_LANGUAGE, PREFIX, STRINGS_TABLE}; use crate::consts::{LOCAL_LANGUAGE, PREFIX, STRINGS_TABLE};
pub struct GuildData { pub struct GuildData {
@ -188,7 +190,7 @@ impl UserData {
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> { ) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let user_id = user.id.as_u64().to_owned(); let user_id = user.id.as_u64().to_owned();
if let Ok(c) = sqlx::query_as_unchecked!( match sqlx::query_as_unchecked!(
Self, Self,
" "
SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ? SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ?
@ -198,38 +200,42 @@ SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ?
.fetch_one(pool) .fetch_one(pool)
.await .await
{ {
Ok(c) Ok(c) => Ok(c),
} else {
let dm_channel = user.create_dm_channel(ctx).await?;
let dm_id = dm_channel.id.as_u64().to_owned();
let pool_c = pool.clone(); Err(sqlx::Error::RowNotFound) => {
let dm_channel = user.create_dm_channel(ctx).await?;
let dm_id = dm_channel.id.as_u64().to_owned();
sqlx::query!( let pool_c = pool.clone();
"
sqlx::query!(
"
INSERT IGNORE INTO channels (channel) VALUES (?) INSERT IGNORE INTO channels (channel) VALUES (?)
", ",
dm_id dm_id
) )
.execute(&pool_c)
.await?;
sqlx::query!(
"
INSERT INTO users (user, name, dm_channel) VALUES (?, ?, (SELECT id FROM channels WHERE channel = ?))
", user_id, user.name, dm_id)
.execute(&pool_c) .execute(&pool_c)
.await?; .await?;
Ok(sqlx::query_as_unchecked!( sqlx::query!(
Self, "
" INSERT INTO users (user, name, dm_channel) VALUES (?, ?, (SELECT id FROM channels WHERE channel = ?))
", user_id, user.name, dm_id)
.execute(&pool_c)
.await?;
Ok(sqlx::query_as_unchecked!(
Self,
"
SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ? SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ?
", ",
user_id user_id
) )
.fetch_one(pool) .fetch_one(pool)
.await?) .await?)
}
Err(e) => error!("Error querying for user: {:?}", e),
} }
} }