Skip to content

Commit 638500d

Browse files
fix(shield-axum): use JSON errors for extractors (#30)
1 parent c5d3052 commit 638500d

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

packages/integrations/shield-axum/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl ErrorBody {
3232
}
3333
}
3434

35+
#[derive(Debug)]
3536
pub struct RouteError(ShieldError);
3637

3738
impl IntoResponse for RouteError {
Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,82 @@
11
use async_trait::async_trait;
2-
use axum::{
3-
extract::FromRequestParts,
4-
http::{request::Parts, StatusCode},
5-
};
6-
use shield::{Session, Shield, User};
2+
use axum::{extract::FromRequestParts, http::request::Parts};
3+
use shield::{ConfigurationError, Session, Shield, ShieldError, User};
4+
5+
use crate::error::RouteError;
76

87
pub struct ExtractShield<U: User>(pub Shield<U>);
98

109
#[async_trait]
1110
impl<S: Send + Sync, U: User + Clone + 'static> FromRequestParts<S> for ExtractShield<U> {
12-
type Rejection = (StatusCode, &'static str);
11+
type Rejection = RouteError;
1312

1413
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
1514
parts
1615
.extensions
1716
.get::<Shield<U>>()
1817
.cloned()
1918
.map(ExtractShield)
20-
.ok_or((
21-
StatusCode::INTERNAL_SERVER_ERROR,
22-
"Can't extract Shield. Is `ShieldLayer` enabled?",
23-
))
19+
.ok_or(ShieldError::Configuration(ConfigurationError::Invalid(
20+
"Can't extract Shield. Is `ShieldLayer` enabled?".to_owned(),
21+
)))
22+
.map_err(RouteError::from)
2423
}
2524
}
2625

2726
pub struct ExtractSession(pub Session);
2827

2928
#[async_trait]
3029
impl<S: Send + Sync> FromRequestParts<S> for ExtractSession {
31-
type Rejection = (StatusCode, &'static str);
30+
type Rejection = RouteError;
3231

3332
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
3433
parts
3534
.extensions
3635
.get::<Session>()
3736
.cloned()
3837
.map(ExtractSession)
39-
.ok_or((
40-
StatusCode::INTERNAL_SERVER_ERROR,
41-
"Can't extract Shield session. Is `ShieldLayer` enabled?",
42-
))
38+
.ok_or(ShieldError::Configuration(ConfigurationError::Invalid(
39+
"Can't extract Shield. Is `ShieldLayer` enabled?".to_owned(),
40+
)))
41+
.map_err(RouteError::from)
4342
}
4443
}
4544

4645
pub struct ExtractUser<U: User>(pub Option<U>);
4746

4847
#[async_trait]
4948
impl<S: Send + Sync, U: User + Clone + 'static> FromRequestParts<S> for ExtractUser<U> {
50-
type Rejection = (StatusCode, &'static str);
49+
type Rejection = RouteError;
5150

5251
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
5352
parts
5453
.extensions
5554
.get::<Option<U>>()
5655
.cloned()
5756
.map(ExtractUser)
58-
.ok_or((
59-
StatusCode::INTERNAL_SERVER_ERROR,
60-
"Can't extract Shield user. Is `ShieldLayer` enabled?",
61-
))
57+
.ok_or(ShieldError::Configuration(ConfigurationError::Invalid(
58+
"Can't extract Shield. Is `ShieldLayer` enabled?".to_owned(),
59+
)))
60+
.map_err(RouteError::from)
6261
}
6362
}
6463

6564
pub struct UserRequired<U: User>(pub U);
6665

6766
#[async_trait]
6867
impl<S: Send + Sync, U: User + Clone + 'static> FromRequestParts<S> for UserRequired<U> {
69-
type Rejection = (StatusCode, &'static str);
68+
type Rejection = RouteError;
7069

7170
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
7271
parts
7372
.extensions
7473
.get::<Option<U>>()
7574
.cloned()
76-
.ok_or((
77-
StatusCode::INTERNAL_SERVER_ERROR,
78-
"Can't extract Shield user. Is `ShieldLayer` enabled?",
79-
))
80-
.and_then(|user| user.ok_or((StatusCode::UNAUTHORIZED, "Unauthorized")))
75+
.ok_or(ShieldError::Configuration(ConfigurationError::Invalid(
76+
"Can't extract Shield. Is `ShieldLayer` enabled?".to_owned(),
77+
)))
78+
.and_then(|user| user.ok_or(ShieldError::Unauthorized))
8179
.map(UserRequired)
80+
.map_err(RouteError::from)
8281
}
8382
}

0 commit comments

Comments
 (0)