From b673fc6ca1c1b557c70174416a229d0fdab0051b Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 17 Sep 2020 20:05:33 -0700 Subject: [PATCH] better formatted error messages also reworked error messages so they create Strings and dont use `&'static str`'s anymore --- server-api/db/src/common.rs | 9 ++++++++- server-api/db/src/lib.rs | 2 +- server-api/db/src/member.rs | 30 ++++++++++++++++-------------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/server-api/db/src/common.rs b/server-api/db/src/common.rs index 1d319f7..66a6620 100644 --- a/server-api/db/src/common.rs +++ b/server-api/db/src/common.rs @@ -7,7 +7,14 @@ use crate::UBigInt; #[macro_export] macro_rules! no_conn { ($spec:literal) => { - format!("No connection could be established: {}", $spec) + format!("[ CON Error ] : {}", $spec) + } +} + +#[macro_export] +macro_rules! sql_err { + ($spec:literal) => { + format!("[ SQL Error ] : {}", $spec) } } diff --git a/server-api/db/src/lib.rs b/server-api/db/src/lib.rs index 759fde4..f1c61d1 100644 --- a/server-api/db/src/lib.rs +++ b/server-api/db/src/lib.rs @@ -25,7 +25,7 @@ pub enum Response { // Duplicate keys can sometimes fuck us over for inserts/update(why tf tho) Duplicate, // Not sure what this will be used for but maybe its useful at some point - Other(&'static str) + Other(String) } diff --git a/server-api/db/src/member.rs b/server-api/db/src/member.rs index 857008f..ac67b34 100644 --- a/server-api/db/src/member.rs +++ b/server-api/db/src/member.rs @@ -4,10 +4,10 @@ use mysql_async::error::Error as SqlError; use async_trait::async_trait; -use crate::Response; +use crate::{Response, no_conn, sql_err}; use crate::{UBigInt, BigInt, Integer, VarChar}; -use crate::common::{FromDB, NO_CONN_MSG}; +use crate::common::{FromDB}; #[allow(dead_code)] // only because some fields are read by the user pub struct Member { @@ -36,7 +36,7 @@ impl FromDB for Member { let db_res : Result<(Conn, Self::Row), SqlError> = conn.first_exec(q, params!{"id" => id}).await; if let Ok((_, row)) = db_res { - match row { + return match row { Some(row) => Response::Row(Self { id: id, secret: row.0, @@ -48,19 +48,21 @@ impl FromDB for Member { None => Response::Empty } } - else { - return Response::Other("Query failed in Trait Member::get"); - } - } - else { - return Response::Other(NO_CONN_MSG); + return Response::Other(sql_err!("Fetch failed")); } + return Response::Other(no_conn!("Member::FromDB::get")); } async fn update(p: &Pool, row: Member) -> Response { let q = r#"UPDATE members SET status = :status, permissions = :perms, name = :name WHERE id = :id"#; + // TODO: remoev this retarded extra network hit + // technically this is really nice for the user but ultimately it either works or it + // doesn't so like the fucks the point of doing some bs like this + // the next a client queries this they'll get whatever data is in the db + // likewise clients are going likely going to re-get this data since its been updated + // or at least should if let Ok(conn) = p.get_conn().await { match Member::get(p, row.id).await { Response::Row(_) => { @@ -74,15 +76,15 @@ impl FromDB for Member { }).await; match db_result { Ok(_) => return Response::Success, - Err(_) => return Response::Other("Could not update yet entry was found") + Err(_) => return Response::Other(sql_err!("WARN member found | Member::FromDB::update failed - ")) } }, Response::Empty => return Response::Empty, Response::Other(msg) => return Response::Other(msg), - _ => return Response::Other("Magical failure") + _ => return Response::Other(sql_err!("Member::FromD::update Bro i dont even")) } } - return Response::Other(NO_CONN_MSG); + return Response::Other(no_conn!("Member::FromDB::update")); } async fn delete(p: &Pool, id: UBigInt) -> Response { @@ -95,13 +97,13 @@ impl FromDB for Member { if let Ok(conn) = db_result { return match conn.prep_exec("", params!{"id" => id}).await { Ok(_) => Response::Success, - Err(_) => Response::Other("Could not delete entry") + Err(_) => Response::Other(sql_err!("Member::FromDB::delete")) } } return Response::Success }, Response::Empty => return Response::Empty, - _ => return Response::Other("Query failed in Member::delete") + _ => return Response::Other(sql_err!("Member::FromDB::delete | another stupid get happened delid this")) } }