feat(server/webhook): log errors in webhook requests

This commit is contained in:
Paul Campbell 2024-05-18 20:26:26 +01:00
parent c3a5e50ad5
commit 6757723b77

View file

@ -2,7 +2,7 @@ use std::net::SocketAddr;
use actix::prelude::*; use actix::prelude::*;
use tracing::{debug, info}; use tracing::{info, warn};
use crate::actors::webhook::message::WebhookMessage; use crate::actors::webhook::message::WebhookMessage;
@ -25,17 +25,24 @@ pub async fn start(
// query: String, // query: String,
headers: warp::http::HeaderMap, headers: warp::http::HeaderMap,
body: bytes::Bytes| async move { body: bytes::Bytes| async move {
info!("POST received");
let bytes = body.to_vec(); let bytes = body.to_vec();
let request_data = String::from_utf8_lossy(&bytes).to_string(); let request_data = String::from_utf8_lossy(&bytes).to_string();
let id = ulid::Ulid::new().to_string(); let id = ulid::Ulid::new().to_string();
match headers.get("Authorization") { match headers.get("Authorization") {
Some(auhorisation) => { Some(auhorisation) => {
debug!(id, path, "Received webhook"); info!(id, path, "Received webhook");
let authorisation = auhorisation let authorisation = auhorisation
.to_str() .to_str()
.map_err(|_| warp::reject())? // valid characters .map_err(|e| {
warn!("Invalid value in authorization: {:?}", e);
warp::reject()
})? // valid characters
.strip_prefix("Basic ") .strip_prefix("Basic ")
.ok_or_else(warp::reject)? // must start with "Basic " .ok_or_else(|| {
warn!("Authorization must be 'Basic'");
warp::reject()
})? // must start with "Basic "
.to_string(); .to_string();
let message = WebhookMessage::new( let message = WebhookMessage::new(
id, id,
@ -45,10 +52,19 @@ pub async fn start(
); );
recipient recipient
.try_send(message) .try_send(message)
.map(|_| warp::reply::with_status("OK", warp::http::StatusCode::OK)) .map(|_| {
.map_err(|_| warp::reject()) info!("Message sent ok");
warp::reply::with_status("OK", warp::http::StatusCode::OK)
})
.map_err(|e| {
warn!("Unknown error: {:?}", e);
warp::reject()
})
}
_ => {
warn!("No Authorization header");
Err(warp::reject())
} }
_ => Err(warp::reject()),
} }
}, },
); );