Basic errors

This commit is contained in:
2021-05-18 21:14:20 +02:00
parent 7a5eea3f85
commit 806054144a
4 changed files with 61 additions and 13 deletions

View File

@ -88,8 +88,6 @@ fn endpoint_handler(
res: &mut super::Response,
endpoint: &str,
) {
let mut buf = [0; 8192];
res.add_header("Cache-Control", "no-store");
let mut error = |code: u16, err_str: &str, client: &mut super::HttpStream| {
@ -97,10 +95,21 @@ fn endpoint_handler(
res.status(code);
res.error_info(err_str.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());
let buf = obj.to_string() + "\r\n";
let length = buf.as_bytes().len();
res.add_header("Content-Length", length.to_string().as_str());
res.add_header("Content-Type", "application/json; charset=utf-8");
if let Err(e) = res.send(&mut client.stream) {
println!("Unable to send: {}", e);
client.server_keep_alive = false;
}
client.stream.write_all(buf.as_bytes());
};
let length = req.find_header("Content-Length");
@ -126,6 +135,7 @@ fn endpoint_handler(
}
};
let mut buf = [0; 8192];
client.stream.read_exact(&mut buf[..length]);
// TODO decompress
@ -142,7 +152,7 @@ fn endpoint_handler(
let buf = match usimp::endpoint(endpoint, input) {
Ok(output) => output.to_string() + "\r\n",
Err(e) => format!("{{\"status\":\"error\",\"message\":\"{}\"}}\r\n", e),
Err(e) => return error(500, e.to_string().as_str(), client),
};
// TODO compress