Skip to content

Commit c7cc0f5

Browse files
committed
Refactor: Simplify error handling in queries and implement conversion from anyhow::Error to QueryError
1 parent 189edfb commit c7cc0f5

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

common/src/queries/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ impl QueryError {
5656
}
5757
}
5858
}
59+
60+
impl From<anyhow::Error> for QueryError {
61+
fn from(err: anyhow::Error) -> Self {
62+
Self::Internal {
63+
message: err.to_string(),
64+
}
65+
}
66+
}

common/src/queries/utils.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ pub async fn query_state<T, F>(
1515
where
1616
F: FnOnce(Message) -> Result<T, QueryError>,
1717
{
18-
let raw_msg = context
19-
.message_bus
20-
.request(topic, request_msg)
21-
.await
22-
.map_err(|e| QueryError::internal_error(format!("Failed to query '{topic}': {e:#}")))?;
23-
18+
let raw_msg = context.message_bus.request(topic, request_msg).await?;
2419
let message = Arc::try_unwrap(raw_msg).unwrap_or_else(|arc| (*arc).clone());
2520

2621
extractor(message)
@@ -38,7 +33,7 @@ where
3833
T: Serialize,
3934
{
4035
let data = query_state(context, topic, request_msg, |response| {
41-
extractor(response).unwrap_or_else(|| {
36+
extractor(response).unwrap_or_else(|e| {
4237
Err(QueryError::internal_error(format!(
4338
"Unexpected response message type while calling {topic}"
4439
)))

0 commit comments

Comments
 (0)