use super::{empty, BLOCK_NEXT_INVOCATION};
use http_body_util::{combinators::BoxBody, BodyExt};
use hyper::body::Bytes;
use hyper::Error;
use hyper::{Request, Response};
use tracing::{debug, error, info};
pub(crate) async fn handler(req: Request<hyper::body::Incoming>) -> Response<BoxBody<Bytes, Error>> {
let resp = match req.into_body().collect().await {
Ok(v) => v.to_bytes(),
Err(e) => panic!("Failed to read lambda response: {:?}", e),
};
match String::from_utf8(resp.as_ref().to_vec()) {
Ok(v) => {
info!("Lambda error: {v}");
}
Err(e) => {
error!(
"Non-UTF-8 error response from Lambda. {:?}\n{}",
e,
hex::encode(resp.as_ref())
);
}
}
if let Ok(mut w) = BLOCK_NEXT_INVOCATION.write() {
debug!("Blocking the next invocation");
*w = true;
} else {
error!("Write deadlock on BLOCK_NEXT_INVOCATION. It's a bug");
}
Response::builder()
.status(hyper::StatusCode::INTERNAL_SERVER_ERROR)
.body(empty())
.expect("Failed to create a response")
}