* 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
This commit is contained in:
shockrah 2021-04-21 17:14:33 -07:00
parent ef73b39f4f
commit c4d7eb9111
3 changed files with 25 additions and 18 deletions

View File

@ -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<Response<Self>, SqlError> {
//! @returns on_sucess -> Ok(Response::Row<Message>)
//! @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 {

View File

@ -15,7 +15,7 @@ use crate::rtc;
pub async fn list_channels(pool: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
/*
* @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<Body>, 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

View File

@ -112,19 +112,22 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, 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
}
}
}
}