@@ -16,13 +16,13 @@ use axum::{
16
16
extract:: { Path , Query , Request , State } ,
17
17
handler:: Handler ,
18
18
http:: {
19
- header:: { ACCEPT , CACHE_CONTROL , CONTENT_LENGTH , CONTENT_TYPE } ,
19
+ header:: { ACCEPT , CACHE_CONTROL , CONTENT_TYPE } ,
20
20
HeaderName , HeaderValue , StatusCode ,
21
21
} ,
22
22
middleware:: Next ,
23
23
response:: { IntoResponse , Redirect , Response } ,
24
24
routing:: get,
25
- Extension , RequestExt , Router ,
25
+ Extension , Router ,
26
26
} ;
27
27
use axum_extra:: routing:: RouterExt ;
28
28
use base64:: { prelude:: BASE64_STANDARD , Engine } ;
@@ -451,22 +451,24 @@ impl<T: Serialize> IntoResponse for Json<T> {
451
451
}
452
452
}
453
453
454
- async fn error_middleware ( State ( state) : State < AppState > , mut req : Request , next : Next ) -> Response {
454
+ async fn error_middleware (
455
+ State ( state) : State < AppState > ,
456
+ CspNonce ( nonce) : CspNonce ,
457
+ req : Request ,
458
+ next : Next ,
459
+ ) -> Response {
455
460
let json = req
456
461
. headers ( )
457
462
. get ( ACCEPT )
458
463
. is_some_and ( |v| v. to_str ( ) . is_ok_and ( |v| v. contains ( "application/json" ) ) ) ;
459
- let nonce = match req. extract_parts :: < CspNonce > ( ) . await {
460
- Ok ( CspNonce ( n) ) => n,
461
- Err ( err) => return err. into_response ( ) ,
462
- } ;
463
464
let mut resp = next. run ( req) . await ;
464
465
if let Some ( failure) = resp. extensions ( ) . get :: < Arc < Failure > > ( ) . cloned ( ) {
465
466
let error = failure. to_string ( ) ;
466
- let body = if json {
467
+ let status = resp. status ( ) ;
468
+ if json {
467
469
resp. headers_mut ( ) . insert ( CONTENT_TYPE , JSON_CTYPE . clone ( ) ) ;
468
470
let error = ErrorSerialization { error } ;
469
- infallible_json_serialize ( & error)
471
+ ( status , Json ( infallible_json_serialize ( & error) ) ) . into_response ( )
470
472
} else {
471
473
resp. headers_mut ( ) . insert ( CONTENT_TYPE , HTML_CTYPE . clone ( ) ) ;
472
474
let error = ErrorTemplate {
@@ -475,16 +477,11 @@ async fn error_middleware(State(state): State<AppState>, mut req: Request, next:
475
477
root_url : state. root_url ,
476
478
nonce,
477
479
} ;
478
- error
479
- . render ( )
480
- . unwrap_or_else ( |e| format ! ( "error rendering template: {e}" ) )
481
- . into_bytes ( )
482
- } ;
483
- resp. headers_mut ( )
484
- . insert ( CONTENT_LENGTH , HeaderValue :: from ( body. len ( ) ) ) ;
485
- * resp. body_mut ( ) = Body :: from ( body) ;
480
+ ( status, error) . into_response ( )
481
+ }
482
+ } else {
483
+ resp
486
484
}
487
- resp
488
485
}
489
486
490
487
pub struct Png ( pub Vec < u8 > ) ;
0 commit comments