Skip to content

Commit ba62ee6

Browse files
committed
fix(api): use custom path extractor
1 parent 7b00dfc commit ba62ee6

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

packages/common/api-builder/src/extract.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::anyhow;
22
use axum::{
33
extract::{
44
Request,
5-
rejection::{ExtensionRejection, JsonRejection},
5+
rejection::{ExtensionRejection, JsonRejection, PathRejection},
66
{FromRequest, FromRequestParts},
77
},
88
response::IntoResponse,
@@ -102,7 +102,36 @@ where
102102
.map(|ext| Extension(ext.0))
103103
.map_err(|err| {
104104
ExtractorError(
105-
anyhow!("developer error: extension error: {}", err.body_text()).into(),
105+
ApiBadRequest {
106+
reason: err.body_text(),
107+
}
108+
.build()
109+
.into(),
110+
)
111+
})
112+
}
113+
}
114+
115+
pub struct Path<T>(pub T);
116+
117+
impl<S, T> FromRequestParts<S> for Path<T>
118+
where
119+
axum::extract::Path<T>: FromRequestParts<S, Rejection = PathRejection>,
120+
S: Send + Sync,
121+
{
122+
type Rejection = ExtractorError;
123+
124+
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
125+
axum::extract::Path::<T>::from_request_parts(parts, state)
126+
.await
127+
.map(|ext| Path(ext.0))
128+
.map_err(|err| {
129+
ExtractorError(
130+
ApiBadRequest {
131+
reason: err.body_text(),
132+
}
133+
.build()
134+
.into(),
106135
)
107136
})
108137
}

packages/common/api-builder/src/wrappers.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use anyhow::Result;
22
use axum::{
33
body::Bytes,
4-
extract::Path,
54
response::IntoResponse,
65
routing::{
76
delete as axum_delete, get as axum_get, patch as axum_patch, post as axum_post,
@@ -14,7 +13,7 @@ use std::future::Future;
1413
use crate::{
1514
context::ApiCtx,
1615
error_response::ApiError,
17-
extract::{Extension, Json, Query},
16+
extract::{Extension, Json, Path, Query},
1817
};
1918

2019
/// Macro to generate wrapper functions for HTTP methods

packages/core/api-public/src/actors/delete.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use anyhow::Result;
22
use axum::{
3-
extract::Path,
43
http::HeaderMap,
54
response::{IntoResponse, Response},
65
};
76
use rivet_api_builder::{
87
ApiError,
9-
extract::{Extension, Json, Query},
8+
extract::{Extension, Json, Path, Query},
109
};
1110
use rivet_api_util::request_remote_datacenter_raw;
1211
use rivet_util::Id;

packages/core/api-public/src/runner_configs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use anyhow::Result;
22
use axum::{
3-
extract::Path,
43
http::HeaderMap,
54
response::{IntoResponse, Response},
65
};
76
use rivet_api_builder::{
87
ApiError,
9-
extract::{Extension, Json, Query},
8+
extract::{Extension, Json, Path, Query},
109
};
1110

1211
use rivet_api_peer::runner_configs::*;

packages/core/api-public/src/ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use axum::{
2-
extract::Path,
32
http::{StatusCode, header},
43
response::{IntoResponse, Response},
54
};
65
use include_dir::{Dir, include_dir};
6+
use rivet_api_builder::extract::Path;
77

88
static UI_DIR: Dir<'_> = include_dir!("$OUT_DIR/ui");
99

0 commit comments

Comments
 (0)