+ Added Response::RestrictedInput to db-lib
Should make it more dsecriptive with really weird input and prevent even more confusing 500 responses to the client + db-lib::Channels::add now also potentially returns this new RestrictedInput variant
This commit is contained in:
parent
b7209c000c
commit
549c69f668
@ -187,8 +187,10 @@ impl Channel {
|
|||||||
Err(_) => Response::Empty
|
Err(_) => Response::Empty
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// TODO: change this to return Result<> which should help with ambiguity
|
else {
|
||||||
return Response::Other("Could not fetch new channel".into());
|
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"))
|
return Response::Other(no_conn!("db::channels::add"))
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,9 @@ pub enum Response<T> {
|
|||||||
Empty,
|
Empty,
|
||||||
// Generic success for things like update/delete's
|
// Generic success for things like update/delete's
|
||||||
Success,
|
Success,
|
||||||
// Duplicate keys can sometimes fuck us over for inserts/update(why tf tho)
|
// Mask 500's with probable user generated errors like duplicate keys being added
|
||||||
Duplicate,
|
// 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
|
// Not sure what this will be used for but maybe its useful at some point
|
||||||
Other(String)
|
Other(String)
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ use hyper::{
|
|||||||
|
|
||||||
use mysql_async::Pool;
|
use mysql_async::Pool;
|
||||||
|
|
||||||
use serde_json::{Value, to_string};
|
use serde_json::{json, Value, to_string};
|
||||||
|
|
||||||
use db::{
|
use db::{
|
||||||
self,
|
self,
|
||||||
@ -62,13 +62,24 @@ pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params:
|
|||||||
|
|
||||||
*response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into()));
|
*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,
|
db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND,
|
||||||
// TODO: loggin
|
|
||||||
|
//like legit issues past here
|
||||||
db::Response::Other(msg) => {
|
db::Response::Other(msg) => {
|
||||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; // conn issue probably
|
||||||
eprintln!("\t{}", msg);
|
eprintln!("\t[ Channels ] {}", msg);
|
||||||
}
|
},
|
||||||
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR // ngmi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user