diff --git a/src/axum_lib.rs b/src/axum_lib.rs index b2bd120..de2a14f 100644 --- a/src/axum_lib.rs +++ b/src/axum_lib.rs @@ -227,7 +227,7 @@ async fn client_update( bearer: Option>>, Extension(redis_client): Extension, ) -> Result<(), CustomError> { - Ok(oidc::client_update(client_id, payload, bearer.map(|b| b.0 .0), &redis_client).await?) + oidc::client_update(client_id, payload, bearer.map(|b| b.0 .0), &redis_client).await } async fn client_delete( diff --git a/src/worker_lib.rs b/src/worker_lib.rs index dfde774..e2a3372 100644 --- a/src/worker_lib.rs +++ b/src/worker_lib.rs @@ -18,6 +18,16 @@ const RSA_PEM_KEY: &str = "RSA_PEM"; // #[global_allocator] // static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; +/// To be used in conjunction of Request::json +macro_rules! json_bad_request { + ($expression:expr) => { + match $expression { + Err(Error::SerdeJsonError(e)) => return Response::error(&e.to_string(), 400), + r => r, + } + }; +} + impl From for Result { fn from(error: CustomError) -> Self { match error { @@ -226,7 +236,7 @@ pub async fn main(req: Request, env: Env) -> Result { } }) .post_async(oidc::REGISTER_PATH, |mut req, ctx| async move { - let payload = req.json().await?; + let payload = json_bad_request!(req.json().await)?; let base_url = ctx.var(BASE_URL_KEY)?.to_string().parse().unwrap(); let url = req.url()?; let db_client = CFClient { ctx, url }; @@ -295,7 +305,7 @@ pub async fn main(req: Request, env: Env) -> Result { .and_then(|b| HeaderValue::from_str(b.as_ref()).ok()) .as_ref() .and_then(Bearer::decode); - let payload = req.json().await?; + let payload = json_bad_request!(req.json().await)?; let url = req.url()?; let db_client = CFClient { ctx, url }; match oidc::client_update(client_id, payload, bearer, &db_client).await {