Skip to content

Commit 6a97f16

Browse files
committed
Clean up use of utility wrapper to log file/line numbers for tracing
This was just added to tracing (tokio-rs/tracing#1773) and the wrapper can be updated to leverage the same.
1 parent 36e15ad commit 6a97f16

File tree

5 files changed

+51
-20
lines changed

5 files changed

+51
-20
lines changed

src/errors.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use async_session;
2-
use axum::{http::StatusCode, Json};
2+
use axum::{
3+
body::{boxed, Full},
4+
http::StatusCode,
5+
response::{IntoResponse, Response},
6+
Json,
7+
};
38
use serde_json::{error, json, Value};
49
use std::io;
510
use thiserror::Error;
@@ -22,6 +27,14 @@ pub enum CustomError {
2227
pub type ApiError = (StatusCode, Json<Value>);
2328
pub type ApiResult<T> = std::result::Result<T, ApiError>;
2429

30+
impl IntoResponse for CustomError {
31+
fn into_response(self) -> Response {
32+
let mut res = Response::new(boxed(Full::from(self.to_string())));
33+
*res.status_mut() = StatusCode::BAD_REQUEST;
34+
res
35+
}
36+
}
37+
2538
impl From<hyper::Error> for CustomError {
2639
fn from(err: hyper::Error) -> CustomError {
2740
CustomError::HyperError

src/extractors/user_extractor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ where
7272
std::panic::Location::caller(),
7373
format!("Unable to fetch user from session!"),
7474
)
75-
.await;
75+
.await
76+
.into_response();
7677

7778
return Err(StatusCode::INTERNAL_SERVER_ERROR);
7879
}

src/handlers.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ pub async fn handle_form(req: Request<Body>) -> impl IntoResponse {
9191
std::panic::Location::caller(),
9292
format!("handle_form: Body {:?}", body_deserialized),
9393
)
94-
.await;
94+
.await
95+
.into_response();
9596

9697
let store = &mut req_parts
9798
.extensions()

src/session.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ pub async fn session_uuid_middleware<B>(mut req: Request<B>, next: Next<B>) -> i
7777
std::panic::Location::caller(),
7878
format!("Error: Unable to update session with user {:?}", err),
7979
)
80-
.await;
80+
.await
81+
.into_response();
8182

8283
return Err(StatusCode::INTERNAL_SERVER_ERROR);
8384
}
@@ -95,7 +96,8 @@ pub async fn session_uuid_middleware<B>(mut req: Request<B>, next: Next<B>) -> i
9596
std::panic::Location::caller(),
9697
format!("Unable to fetch cookie value from new session!"),
9798
)
98-
.await;
99+
.await
100+
.into_response();
99101

100102
return Err(StatusCode::INTERNAL_SERVER_ERROR);
101103
}
@@ -105,7 +107,8 @@ pub async fn session_uuid_middleware<B>(mut req: Request<B>, next: Next<B>) -> i
105107
std::panic::Location::caller(),
106108
format!("Error whilst attempting to update store {:?}", err),
107109
)
108-
.await;
110+
.await
111+
.into_response();
109112

110113
return Err(StatusCode::INTERNAL_SERVER_ERROR);
111114
}
@@ -115,7 +118,8 @@ pub async fn session_uuid_middleware<B>(mut req: Request<B>, next: Next<B>) -> i
115118
std::panic::Location::caller(),
116119
format!("Updated Session: {:?}", &session_clone.id()),
117120
)
118-
.await;
121+
.await
122+
.into_response();
119123

120124
tracing::debug!(
121125
"Created cookie {:?}={:?} for UUID {} / for Session: {:?}",
@@ -194,7 +198,8 @@ pub async fn session_uuid_middleware<B>(mut req: Request<B>, next: Next<B>) -> i
194198
session_cookie_clone
195199
),
196200
)
197-
.await;
201+
.await
202+
.into_response();
198203

199204
return Err(StatusCode::BAD_REQUEST);
200205
}
@@ -265,7 +270,7 @@ where
265270
std::panic::Location::caller(),
266271
format!("Error: Unable to deserialize request body {:?}", err),
267272
)
268-
.await;
273+
.await?;
269274

270275
return Err(CustomError::NotImplementedError);
271276
}
@@ -298,7 +303,7 @@ pub async fn update(
298303
&session_cookie
299304
),
300305
)
301-
.await;
306+
.await?;
302307

303308
// Use `session_cookie` to load the session
304309
let mut session: Session = match store.load_session(session_cookie.to_string()).await {
@@ -309,7 +314,7 @@ pub async fn update(
309314
std::panic::Location::caller(),
310315
format!("Error: Unable to load session!"),
311316
)
312-
.await;
317+
.await?;
313318
return Err(CustomError::NotImplementedError);
314319
}
315320
},
@@ -327,7 +332,7 @@ pub async fn update(
327332
std::panic::Location::caller(),
328333
format!("Error: Unable to update session with user {:?}", err),
329334
)
330-
.await;
335+
.await?;
331336

332337
return Err(CustomError::NotImplementedError);
333338
}
@@ -343,7 +348,7 @@ pub async fn update(
343348
cookie_value
344349
),
345350
)
346-
.await;
351+
.await?;
347352

348353
cookie_value
349354
}
@@ -352,7 +357,7 @@ pub async fn update(
352357
std::panic::Location::caller(),
353358
format!("Store updated OK / No cookie value returned"),
354359
)
355-
.await;
360+
.await?;
356361

357362
String::from("")
358363
}
@@ -362,7 +367,7 @@ pub async fn update(
362367
std::panic::Location::caller(),
363368
format!("Error whilst attempting to update store {:?}", err),
364369
)
365-
.await;
370+
.await?;
366371

367372
return Err(CustomError::NotImplementedError);
368373
}
@@ -398,7 +403,7 @@ where
398403
&session_cookie
399404
),
400405
)
401-
.await;
406+
.await?;
402407

403408
// Use `session_cookie` to load the session
404409
let session: Session = match store.load_session(session_cookie.to_string()).await {
@@ -409,7 +414,7 @@ where
409414
std::panic::Location::caller(),
410415
format!("Error: Unable to load session!"),
411416
)
412-
.await;
417+
.await?;
413418
return Err(CustomError::NotImplementedError);
414419
}
415420
},
@@ -425,7 +430,7 @@ where
425430
std::panic::Location::caller(),
426431
format!("Unable to fetch user from session!"),
427432
)
428-
.await;
433+
.await?;
429434

430435
return Err(CustomError::NotImplementedError);
431436
}

src/utils.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1+
use crate::errors::CustomError;
12
use anyhow::Error;
23
use std::panic::Location;
34

4-
pub async fn tracing_debug(location: &'static Location<'static>, msg: String) {
5+
pub async fn tracing_debug(
6+
location: &'static Location<'static>,
7+
msg: String,
8+
) -> Result<(), CustomError> {
59
let custom_format = format!("[{}:{}]", location.file(), location.line());
610

711
tracing::debug!("{}: {}", custom_format, msg);
12+
13+
Ok(())
814
}
915

10-
pub async fn tracing_error(location: &'static Location<'static>, msg: String) {
16+
pub async fn tracing_error(
17+
location: &'static Location<'static>,
18+
msg: String,
19+
) -> Result<(), CustomError> {
1120
let custom_format = format!("[{}:{}]", location.file(), location.line());
1221

1322
tracing::error!("{}: {}", custom_format, msg);
23+
24+
Ok(())
1425
}

0 commit comments

Comments
 (0)