Some more error handling
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
use super::Method;
|
||||
use crate::usimp;
|
||||
use crate::websocket;
|
||||
use crate::error::*;
|
||||
use serde_json;
|
||||
|
||||
pub struct HttpStream {
|
||||
@ -69,8 +70,8 @@ fn request_handler(client: &mut super::HttpStream) {
|
||||
}
|
||||
Err(e) => {
|
||||
res.status(400);
|
||||
res.error_info(format!("Unable to parser header: {}", &e));
|
||||
println!("Unable to parser header: {}", &e);
|
||||
res.error_info(format!("{}", &e));
|
||||
println!("{}", &e);
|
||||
client.server_keep_alive = false;
|
||||
}
|
||||
}
|
||||
@ -90,14 +91,17 @@ fn endpoint_handler(
|
||||
) {
|
||||
res.add_header("Cache-Control", "no-store");
|
||||
|
||||
let mut error = |code: u16, err_str: &str, client: &mut super::HttpStream| {
|
||||
println!("{}", err_str);
|
||||
res.status(code);
|
||||
res.error_info(err_str.to_string());
|
||||
let mut error = |error: Error, client: &mut super::HttpStream| {
|
||||
println!("{}", error.to_string());
|
||||
res.status(match &error.class() {
|
||||
Class::ClientError => 400,
|
||||
Class::ServerError => 500,
|
||||
});
|
||||
res.error_info(error.to_string());
|
||||
|
||||
let mut obj = serde_json::Value::Object(serde_json::Map::new());
|
||||
obj["status"] = serde_json::Value::String("error".to_string());
|
||||
obj["message"] = serde_json::Value::String(err_str.to_string());
|
||||
obj["message"] = serde_json::Value::String(error.to_string());
|
||||
let buf = obj.to_string() + "\r\n";
|
||||
|
||||
let length = buf.as_bytes().len();
|
||||
@ -117,8 +121,8 @@ fn endpoint_handler(
|
||||
Some(length) => length,
|
||||
None => {
|
||||
return error(
|
||||
400,
|
||||
"Unable to parse header: Content-Length missing",
|
||||
Error::new(Kind::HttpRequestParseError, Class::ClientError)
|
||||
.set_desc("field 'Content-Length' missing".to_string()),
|
||||
client,
|
||||
)
|
||||
}
|
||||
@ -128,8 +132,8 @@ fn endpoint_handler(
|
||||
Ok(length) => length,
|
||||
Err(e) => {
|
||||
return error(
|
||||
400,
|
||||
format!("Unable to parse Content-Length: {}", &e).as_str(),
|
||||
Error::new(Kind::HttpRequestParseError, Class::ClientError)
|
||||
.set_desc(format!("unable to parse field 'Content-Length': {}", &e).to_string()),
|
||||
client,
|
||||
)
|
||||
}
|
||||
@ -142,17 +146,13 @@ fn endpoint_handler(
|
||||
let input = match serde_json::from_slice(&buf[..length]) {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
return error(
|
||||
400,
|
||||
format!("Unable to parse JSON: {}", &e).as_str(),
|
||||
client,
|
||||
)
|
||||
return error(e.into(), client)
|
||||
}
|
||||
};
|
||||
|
||||
let buf = match usimp::endpoint(endpoint, input) {
|
||||
Ok(output) => output.to_string() + "\r\n",
|
||||
Err(e) => return error(500, e.to_string().as_str(), client),
|
||||
Err(e) => return error(e, client),
|
||||
};
|
||||
|
||||
// TODO compress
|
||||
|
Reference in New Issue
Block a user