Skip to content

Commit d613f71

Browse files
Document error handling
1 parent 6465766 commit d613f71

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ serve(listener, router).await.unwrap();
166166
# }
167167
```
168168

169+
### Error Handling
170+
171+
Both the `RequiredMAuthValidationLayer` and the `OptionalMAuthValidationLayer` layers will
172+
log errors encountered via `tracing` under the `mauth_client::validate_incoming` target.
173+
174+
The Required layer returns the 401 response immediately, so there is no convenient way to
175+
retrieve the error in order to do anything more sophisticated with it.
176+
177+
The Optional layer, in addition to loging the error, will also add the `MAuthValidationError`
178+
to the request extensions. If desired, any request handlers or middlewares can retrieve it
179+
from there in order to take further actions based on the error type. This error type also
180+
implements Axum's `OptionalFromRequestParts`, so you can more easily retrieve it using
181+
`Option<MAuthValidationError>` anywhere that supports extractors.
182+
169183
### OpenTelemetry Integration
170184

171185
There are also optional features `tracing-otel-26` and `tracing-otel-27` that pair with

src/axum_service.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::task::{Context, Poll};
1313
use tower::{Layer, Service};
1414
use tracing::error;
1515

16-
use crate::validate_incoming::ValidatedRequestDetails;
16+
use crate::validate_incoming::{MAuthValidationError, ValidatedRequestDetails};
1717
use crate::{
1818
config::{ConfigFileSection, ConfigReadError},
1919
MAuthInfo,
@@ -228,3 +228,17 @@ where
228228
Ok(parts.extensions.get::<ValidatedRequestDetails>().cloned())
229229
}
230230
}
231+
232+
impl<S> OptionalFromRequestParts<S> for MAuthValidationError
233+
where
234+
S: Send + Sync,
235+
{
236+
type Rejection = Infallible;
237+
238+
async fn from_request_parts(
239+
parts: &mut Parts,
240+
_state: &S,
241+
) -> Result<Option<Self>, Self::Rejection> {
242+
Ok(parts.extensions.get::<MAuthValidationError>().cloned())
243+
}
244+
}

0 commit comments

Comments
 (0)