* Fixing message body unwrap to safer unwrap_or
unwrap had a chance to panic where as the unwrap_or now defaults to an empty Bytes object which lets the code after it fail with an HTTP 400. This is preferrable to a 500 error as there really is no error, just bad/unpollable input from the end user.
This commit is contained in:
parent
f1b1581588
commit
b810a5abba
@ -1,6 +1,7 @@
|
||||
use mysql_async::Pool;
|
||||
use hyper::{Response, Body, HeaderMap, StatusCode};
|
||||
use hyper::body::to_bytes;
|
||||
use hyper::body::Bytes;
|
||||
use serde_json::json;
|
||||
|
||||
use std::collections::HashMap;
|
||||
@ -94,7 +95,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, body: Body
|
||||
let channel_id = qs_param!(params, "channel_id", u64);
|
||||
|
||||
// Black magic
|
||||
let body_bytes: &[u8] = &to_bytes(body).await.unwrap(); // yolo
|
||||
let body_bytes: &[u8] = &to_bytes(body).await.unwrap_or(Bytes::new());
|
||||
let content = String::from_utf8_lossy(body_bytes);
|
||||
|
||||
// 400 on empty bodies or missing channel id's
|
||||
@ -164,3 +165,4 @@ pub async fn recent_messages(pool: &Pool, response: &mut Response<Body>, params:
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user