Translate deserialisation error as 400 (#27)

Also fixes a Clippy warning
This commit is contained in:
Simon Bihel 2022-04-19 13:46:16 +01:00 committed by GitHub
parent 2e54c4446a
commit b172f31d40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View File

@ -227,7 +227,7 @@ async fn client_update(
bearer: Option<TypedHeader<Authorization<Bearer>>>, bearer: Option<TypedHeader<Authorization<Bearer>>>,
Extension(redis_client): Extension<RedisClient>, Extension(redis_client): Extension<RedisClient>,
) -> Result<(), CustomError> { ) -> 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( async fn client_delete(

View File

@ -18,6 +18,16 @@ const RSA_PEM_KEY: &str = "RSA_PEM";
// #[global_allocator] // #[global_allocator]
// static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; // 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<CustomError> for Result<Response> { impl From<CustomError> for Result<Response> {
fn from(error: CustomError) -> Self { fn from(error: CustomError) -> Self {
match error { match error {
@ -226,7 +236,7 @@ pub async fn main(req: Request, env: Env) -> Result<Response> {
} }
}) })
.post_async(oidc::REGISTER_PATH, |mut req, ctx| async move { .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 base_url = ctx.var(BASE_URL_KEY)?.to_string().parse().unwrap();
let url = req.url()?; let url = req.url()?;
let db_client = CFClient { ctx, url }; let db_client = CFClient { ctx, url };
@ -295,7 +305,7 @@ pub async fn main(req: Request, env: Env) -> Result<Response> {
.and_then(|b| HeaderValue::from_str(b.as_ref()).ok()) .and_then(|b| HeaderValue::from_str(b.as_ref()).ok())
.as_ref() .as_ref()
.and_then(Bearer::decode); .and_then(Bearer::decode);
let payload = req.json().await?; let payload = json_bad_request!(req.json().await)?;
let url = req.url()?; let url = req.url()?;
let db_client = CFClient { ctx, url }; let db_client = CFClient { ctx, url };
match oidc::client_update(client_id, payload, bearer, &db_client).await { match oidc::client_update(client_id, payload, bearer, &db_client).await {