Skip to content

Commit 8cc8f97

Browse files
committed
Improve snafu error messages
1 parent e43e373 commit 8cc8f97

7 files changed

Lines changed: 63 additions & 7 deletions

File tree

trino-lb/src/error_formatting.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
pub fn snafu_error_to_string<E: std::error::Error>(err: &E) -> String {
2+
let mut result = format!("{err}");
3+
let mut source = err.source();
4+
while let Some(err) = source {
5+
result.push_str(format!(": {err}").as_str());
6+
source = err.source();
7+
}
8+
9+
result
10+
}
11+
12+
#[test]
13+
fn test_error_formatting() {
14+
let err = trino_lb_persistence::Error::RedisError {
15+
source: trino_lb_persistence::redis::Error::CreateClient {
16+
source: redis::RedisError::from(std::io::Error::new(
17+
std::io::ErrorKind::ConnectionRefused,
18+
"Connection to Redis was refused",
19+
)),
20+
},
21+
};
22+
23+
assert_eq!(format!("{err}"), "Redis persistence error");
24+
assert_eq!(format!("{err:#}"), "Redis persistence error");
25+
assert_eq!(
26+
format!("{err:?}"),
27+
"RedisError { source: CreateClient { source: Connection to Redis was refused } }"
28+
);
29+
assert_eq!(
30+
snafu_error_to_string(&err),
31+
"Redis persistence error: Failed to create redis client: Connection to Redis was refused: Connection to Redis was refused"
32+
);
33+
assert_eq!(
34+
format!("{:#}", snafu::Report::from_error(err)),
35+
"Redis persistence error
36+
37+
Caused by these errors (recent errors listed first):
38+
1: Failed to create redis client
39+
2: Connection to Redis was refused
40+
41+
NOTE: Some redundant information has been removed. Set SNAFU_RAW_ERROR_MESSAGES=1 to disable this behavior.
42+
"
43+
);
44+
}

trino-lb/src/http_server/admin/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use trino_lb_persistence::Persistence;
2121

2222
use crate::{
2323
cluster_group_manager::{self, ClusterStats},
24+
error_formatting::snafu_error_to_string,
2425
http_server::AppState,
2526
};
2627

@@ -71,7 +72,7 @@ impl IntoResponse for Error {
7172
Error::GetQueryCounterForGroup { .. } => StatusCode::INTERNAL_SERVER_ERROR,
7273
Error::GetAllClusterStates { .. } => StatusCode::INTERNAL_SERVER_ERROR,
7374
};
74-
(status_code, format!("{self}")).into_response()
75+
(status_code, snafu_error_to_string(&self)).into_response()
7576
}
7677
}
7778

trino-lb/src/http_server/metrics.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use prometheus::{Encoder, TextEncoder};
99
use snafu::{ResultExt, Snafu};
1010
use tracing::{instrument, warn};
1111

12-
use crate::http_server::AppState;
12+
use crate::{error_formatting::snafu_error_to_string, http_server::AppState};
1313

1414
#[derive(Snafu, Debug)]
1515
pub enum Error {
@@ -23,7 +23,11 @@ pub enum Error {
2323
impl IntoResponse for Error {
2424
fn into_response(self) -> Response {
2525
warn!(error = ?self, "Error while processing metrics request");
26-
(StatusCode::INTERNAL_SERVER_ERROR, format!("{self:?}")).into_response()
26+
(
27+
StatusCode::INTERNAL_SERVER_ERROR,
28+
snafu_error_to_string(&self),
29+
)
30+
.into_response()
2731
}
2832
}
2933

trino-lb/src/http_server/ui/index.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use tracing::{instrument, warn};
1212

1313
use crate::{
1414
cluster_group_manager::{self, ClusterStats},
15+
error_formatting::snafu_error_to_string,
1516
http_server::AppState,
1617
};
1718

@@ -33,7 +34,7 @@ impl IntoResponse for Error {
3334
Error::GetAllClusterStates { .. } => StatusCode::INTERNAL_SERVER_ERROR,
3435
Error::RenderTemplate { .. } => StatusCode::INTERNAL_SERVER_ERROR,
3536
};
36-
(status_code, format!("{self}")).into_response()
37+
(status_code, snafu_error_to_string(&self)).into_response()
3738
}
3839
}
3940

trino-lb/src/http_server/ui/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tracing::{instrument, warn};
1111
use trino_lb_core::TrinoLbQueryId;
1212
use trino_lb_persistence::Persistence;
1313

14-
use crate::http_server::AppState;
14+
use crate::{error_formatting::snafu_error_to_string, http_server::AppState};
1515

1616
#[derive(Snafu, Debug)]
1717
pub enum Error {
@@ -36,7 +36,7 @@ impl IntoResponse for Error {
3636
Error::QueryIdMissing => StatusCode::BAD_REQUEST,
3737
Error::QueryIdNotFound { .. } => StatusCode::NOT_FOUND,
3838
};
39-
(status_code, format!("{self}")).into_response()
39+
(status_code, snafu_error_to_string(&self)).into_response()
4040
}
4141
}
4242

trino-lb/src/http_server/v1/statement.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use url::Url;
2828

2929
use crate::{
3030
cluster_group_manager::{self, SendToTrinoResponse},
31+
error_formatting::snafu_error_to_string,
3132
http_server::AppState,
3233
maintenance::leftover_queries::UPDATE_QUEUED_QUERY_LAST_ACCESSED_INTERVAL,
3334
};
@@ -131,7 +132,11 @@ pub enum Error {
131132
impl IntoResponse for Error {
132133
fn into_response(self) -> Response {
133134
warn!(error = ?self, "Error while processing request");
134-
(StatusCode::INTERNAL_SERVER_ERROR, format!("{self:?}")).into_response()
135+
(
136+
StatusCode::INTERNAL_SERVER_ERROR,
137+
snafu_error_to_string(&self),
138+
)
139+
.into_response()
135140
}
136141
}
137142

trino-lb/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::{args::Args, http_server::start_http_server};
2323

2424
mod args;
2525
mod cluster_group_manager;
26+
mod error_formatting;
2627
mod http_server;
2728
mod maintenance;
2829
mod metrics;

0 commit comments

Comments
 (0)