diff --git a/server-api/db/src/channels.rs b/server-api/db/src/channels.rs index 0bbef0c..82e8b0b 100644 --- a/server-api/db/src/channels.rs +++ b/server-api/db/src/channels.rs @@ -187,8 +187,10 @@ impl Channel { Err(_) => Response::Empty }; } - // TODO: change this to return Result<> which should help with ambiguity - return Response::Other("Could not fetch new channel".into()); + else { + return Response::RestrictedInput( + "Could not add channel, make sure the name is unique and not longer than 256 bytes".into()); + } } return Response::Other(no_conn!("db::channels::add")) } diff --git a/server-api/db/src/lib.rs b/server-api/db/src/lib.rs index 68e0f5d..d295ace 100644 --- a/server-api/db/src/lib.rs +++ b/server-api/db/src/lib.rs @@ -25,8 +25,9 @@ pub enum Response { Empty, // Generic success for things like update/delete's Success, - // Duplicate keys can sometimes fuck us over for inserts/update(why tf tho) - Duplicate, + // Mask 500's with probable user generated errors like duplicate keys being added + // or data that doesn't fit in column or something + RestrictedInput(String), // Not sure what this will be used for but maybe its useful at some point Other(String) } diff --git a/server-api/src/channels.rs b/server-api/src/channels.rs index 08efe93..76f16e9 100644 --- a/server-api/src/channels.rs +++ b/server-api/src/channels.rs @@ -5,7 +5,7 @@ use hyper::{ use mysql_async::Pool; -use serde_json::{Value, to_string}; +use serde_json::{json, Value, to_string}; use db::{ self, @@ -62,13 +62,24 @@ pub async fn create_channel(pool: &Pool, response: &mut Response, params: *response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into())); }, + // user error that the db doesn't deal with so we just blame the user + db::Response::RestrictedInput(msg) => { + *response.status_mut() = StatusCode::BAD_REQUEST; + response.headers_mut().insert("Content-Type", + HeaderValue::from_static("application/json")); + let bjson = json!({"error": msg}); + *response.body_mut() = Body::from(to_string(&bjson).unwrap_or("{}".into())); + }, + + // inserted but could not fetch db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND, - // TODO: loggin + + //like legit issues past here db::Response::Other(msg) => { - *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - eprintln!("\t{}", msg); - } - _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; // conn issue probably + eprintln!("\t[ Channels ] {}", msg); + }, + _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR // ngmi } } },