From c4d7eb9111aa23b2a729c9f845bc3a2b94f7abbd Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 21 Apr 2021 17:14:33 -0700 Subject: [PATCH] * api::channels::list_channels has more relevant docs Better matching against return types so we're not throwing away errors This just entails swapping the if let for a match statement * db-lib::Message::send has more updated docs on what variants of Response it returns * api::messages::send_message has better error logging now and more relevant comments --- json-api/db/src/messages.rs | 4 +++- json-api/src/channels.rs | 30 ++++++++++++++++-------------- json-api/src/messages.rs | 9 ++++++--- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/json-api/db/src/messages.rs b/json-api/db/src/messages.rs index 4b24bfb..1e9c7d7 100644 --- a/json-api/db/src/messages.rs +++ b/json-api/db/src/messages.rs @@ -65,7 +65,7 @@ impl Message { if let Ok(_) = file.write_all(data).await { Response::Row(msg) } else { - Response::Empty + Response::Other(format!("Couldn't write data to disk")) } } Err(e) => { @@ -77,6 +77,8 @@ impl Message { pub async fn send(p: &Pool, content: &str, content_type: &str, cid: UBigInt, uid: UBigInt) -> Result, SqlError> { //! @returns on_sucess -> Ok(Response::Row) + //! @returns on_user_fail -> Ok(Response::RestrictedInput) + //! @returns on_caught_server_fail -> Ok(Response::Other) //! @returns on_failure Err(SqlErr) if content_type == "text/plain" { match Self::insert_text(p, content, cid, uid).await { diff --git a/json-api/src/channels.rs b/json-api/src/channels.rs index ebef0ac..9dc5b07 100644 --- a/json-api/src/channels.rs +++ b/json-api/src/channels.rs @@ -15,7 +15,7 @@ use crate::rtc; pub async fn list_channels(pool: &Pool, response: &mut Response, params: HashMap) { /* - * @user-params -> for now none as i don't feel like dealing with it + * @kind : i32 1|2 => Specifies Voice or Text channels respectively */ if let Some(chan_kind) = qs_param!(params, "kind", i32) { @@ -53,20 +53,22 @@ pub async fn create_channel(pool: &Pool, response: &mut Response, params: *response.status_mut() = if name.is_some() && kind.is_some() { let (name, kind) = (name.unwrap(), kind.unwrap()); - if let Ok(resp) = Channel::add(pool, name, description, kind).await { - match resp { - Row(channel) => { - set_json_body(response, json!(channel)); - rtc::create_channel(channel).await; - StatusCode::OK - }, - - RestrictedInput(_) => StatusCode::UNPROCESSABLE_ENTITY, - Other(_) => StatusCode::INTERNAL_SERVER_ERROR, - _ => StatusCode::INTERNAL_SERVER_ERROR + match Channel::add(pool, name, description, kind).await { + Ok(resp) => { + match resp { + Row(channel) => { + set_json_body(response, json!({"channel": channel})); + rtc::create_channel(channel).await; + StatusCode::OK + }, + RestrictedInput(_) => StatusCode::UNPROCESSABLE_ENTITY, + _ => StatusCode::INTERNAL_SERVER_ERROR + } + }, + Err(e) => { + eprintln!("Error: {}", e); + StatusCode::INTERNAL_SERVER_ERROR } - } else { - StatusCode::INTERNAL_SERVER_ERROR } } else { StatusCode::BAD_REQUEST diff --git a/json-api/src/messages.rs b/json-api/src/messages.rs index 12599ea..1e3aa15 100644 --- a/json-api/src/messages.rs +++ b/json-api/src/messages.rs @@ -112,19 +112,22 @@ pub async fn send_message(pool: &Pool, response: &mut Response, body: Body *response.status_mut() = StatusCode::BAD_REQUEST; } else { - // ctype safe unwrap + // Safe unwrap with ctype - its container type is checked prior match db::Message::send(pool, &content, ctype.unwrap(), channel_id.unwrap(), uid).await { Ok(Row(msg)) => { use crate::rtc; rtc::new_message(pool, msg).await; }, - Ok(Empty) => { }, // idk anymore Ok(RestrictedInput(_msg)) => *response.status_mut() = StatusCode::BAD_REQUEST, Ok(Other(msg)) => { eprintln!("{}", msg); *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; }, - _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR + Ok(_) => {}, // Can't happen because no branches return remaining variants + Err(e) => { + eprintln!("/message/send error: {}", e); + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR + } } } }