Skip to content

Commit 1bc9e65

Browse files
committed
Refactor: Migrate to thiserror for RESTError and QueryError
1 parent 6aa66de commit 1bc9e65

File tree

3 files changed

+24
-45
lines changed

3 files changed

+24
-45
lines changed

common/src/queries/errors.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
use serde::{Deserialize, Serialize};
2-
use std::fmt;
2+
use thiserror::Error;
33

44
/// Common error type for all state query responses
5-
#[derive(Debug, Clone, Serialize, Deserialize)]
5+
#[derive(Debug, Clone, Error, Serialize, Deserialize)]
66
pub enum QueryError {
77
/// The requested resource was not found
8+
#[error("Not found: {resource}")]
89
NotFound { resource: String },
910

1011
/// An error occurred while processing the query
12+
#[error("Internal error: {message}")]
1113
Internal { message: String },
1214

1315
/// Storage backend is disabled in configuration
16+
#[error("{storage_type} storage is not enabled")]
1417
StorageDisabled { storage_type: String },
1518

1619
/// Invalid request parameters
20+
#[error("Invalid request: {message}")]
1721
InvalidRequest { message: String },
1822

1923
/// Query variant is not implemented yet
24+
#[error("Query not implemented: {query}")]
2025
NotImplemented { query: String },
2126
}
2227

@@ -51,19 +56,3 @@ impl QueryError {
5156
}
5257
}
5358
}
54-
55-
impl fmt::Display for QueryError {
56-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57-
match self {
58-
Self::NotFound { resource } => write!(f, "Not found: {}", resource),
59-
Self::Internal { message } => write!(f, "Query failed: {}", message),
60-
Self::StorageDisabled { storage_type } => {
61-
write!(f, "{} storage is not enabled", storage_type)
62-
}
63-
Self::InvalidRequest { message } => write!(f, "Invalid request: {}", message),
64-
Self::NotImplemented { query } => write!(f, "Query not implemented: {}", query),
65-
}
66-
}
67-
}
68-
69-
impl std::error::Error for QueryError {}

common/src/queries/utils.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::messages::{Message, RESTResponse};
66
use crate::queries::errors::QueryError;
77
use crate::rest_error::RESTError;
88

9-
/// Query state and get typed result or QueryError
10-
/// This is the low-level building block for handlers that need to do more processing
119
pub async fn query_state<T, F>(
1210
context: &Arc<Context<Message>>,
1311
topic: &str,
@@ -21,7 +19,7 @@ where
2119
.message_bus
2220
.request(topic, request_msg)
2321
.await
24-
.map_err(|e| QueryError::internal_error(e.to_string()))?;
22+
.map_err(|e| QueryError::internal_error(format!("Failed to query '{topic}': {e:#}")))?;
2523

2624
let message = Arc::try_unwrap(raw_msg).unwrap_or_else(|arc| (*arc).clone());
2725

common/src/rest_error.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
use crate::queries::errors::QueryError;
22
use anyhow::Error as AnyhowError;
33
use caryatid_module_rest_server::messages::RESTResponse;
4-
use std::fmt;
4+
use thiserror::Error;
55

6-
/// Standard error types for the application
7-
#[derive(Debug)]
6+
/// Standard REST error types
7+
#[derive(Debug, Error)]
88
pub enum RESTError {
9+
#[error("{0}")]
910
BadRequest(String),
11+
12+
#[error("{0}")]
1013
NotFound(String),
14+
15+
#[error("{0}")]
1116
InternalServerError(String),
17+
18+
#[error("{0}")]
1219
NotImplemented(String),
1320
}
1421

@@ -24,12 +31,12 @@ impl RESTError {
2431
}
2532

2633
/// Get the error message
27-
pub fn message(&self) -> String {
34+
pub fn message(&self) -> &str {
2835
match self {
29-
RESTError::BadRequest(msg) => msg.clone(),
30-
RESTError::NotFound(msg) => msg.clone(),
31-
RESTError::InternalServerError(msg) => msg.clone(),
32-
RESTError::NotImplemented(msg) => msg.clone(),
36+
RESTError::BadRequest(msg) => msg,
37+
RESTError::NotFound(msg) => msg,
38+
RESTError::InternalServerError(msg) => msg,
39+
RESTError::NotImplemented(msg) => msg,
3340
}
3441
}
3542

@@ -79,18 +86,10 @@ impl RESTError {
7986
}
8087
}
8188

82-
impl fmt::Display for RESTError {
83-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84-
write!(f, "{}", self.message())
85-
}
86-
}
87-
88-
impl std::error::Error for RESTError {}
89-
9089
/// Convert RESTError to RESTResponse
9190
impl From<RESTError> for RESTResponse {
9291
fn from(error: RESTError) -> Self {
93-
RESTResponse::with_text(error.status_code(), &error.message())
92+
RESTResponse::with_text(error.status_code(), error.message())
9493
}
9594
}
9695

@@ -101,13 +100,6 @@ impl From<AnyhowError> for RESTError {
101100
}
102101
}
103102

104-
// /// Convert ParseIntError to RESTError (400 Bad Request)
105-
// impl From<ParseIntError> for RESTError {
106-
// fn from(error: ParseIntError) -> Self {
107-
// RESTError::BadRequest(error.to_string())
108-
// }
109-
// }
110-
111103
/// Convert hex decode errors to RESTError (400 Bad Request)
112104
impl From<hex::FromHexError> for RESTError {
113105
fn from(error: hex::FromHexError) -> Self {

0 commit comments

Comments
 (0)