Skip to content

Commit

Permalink
feat: better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteregrets committed Nov 20, 2024
1 parent e260538 commit 3646e60
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
46 changes: 25 additions & 21 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,17 @@ use streamstore::{
},
};

use crate::error::s2_status;
use crate::error::S2ServiceError;

pub struct AccountService {
client: Client,
}

#[derive(Debug, thiserror::Error)]
pub enum AccountServiceError {
#[error("Failed to list basins: {0}")]
ListBasins(String),

#[error("Failed to create basin: {0}")]
CreateBasin(String),

#[error("Failed to delete basin: {0}")]
DeleteBasin(String),

#[error("Failed to get basin config: {0}")]
GetBasinConfig(String),

#[error("Failed to reconfigure basin: {0}")]
ReconfigureBasin(String),
#[error("Failed to {operation} basin(s): \n{error}")]
pub struct AccountServiceError {
operation: String,
error: S2ServiceError,
}

impl AccountService {
Expand All @@ -49,7 +38,10 @@ impl AccountService {
self.client
.list_basins(list_basins_req)
.await
.map_err(|e| AccountServiceError::ListBasins(s2_status(&e)))
.map_err(|e| AccountServiceError {
operation: "list".to_string(),
error: S2ServiceError::from(e),
})
}

pub async fn create_basin(
Expand All @@ -74,15 +66,21 @@ impl AccountService {
self.client
.create_basin(create_basin_req)
.await
.map_err(|e| AccountServiceError::CreateBasin(s2_status(&e)))
.map_err(|e| AccountServiceError {
operation: "create".to_string(),
error: S2ServiceError::from(e),
})
}

pub async fn delete_basin(&self, basin: BasinName) -> Result<(), AccountServiceError> {
let delete_basin_req = DeleteBasinRequest::new(basin);
self.client
.delete_basin(delete_basin_req)
.await
.map_err(|e| AccountServiceError::DeleteBasin(s2_status(&e)))?;
.map_err(|e| AccountServiceError {
operation: "delete".to_string(),
error: S2ServiceError::from(e),
})?;
Ok(())
}

Expand All @@ -93,7 +91,10 @@ impl AccountService {
self.client
.get_basin_config(basin)
.await
.map_err(|e| AccountServiceError::GetBasinConfig(s2_status(&e)))
.map_err(|e| AccountServiceError {
operation: "get".to_string(),
error: S2ServiceError::from(e),
})
}

pub async fn reconfigure_basin(
Expand All @@ -108,7 +109,10 @@ impl AccountService {
self.client
.reconfigure_basin(reconfigure_basin_req)
.await
.map_err(|e| AccountServiceError::ReconfigureBasin(s2_status(&e)))?;
.map_err(|e| AccountServiceError {
operation: "reconfigure".to_string(),
error: S2ServiceError::from(e),
})?;
Ok(())
}
}
24 changes: 23 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,31 @@ pub enum S2CliError {
RecordWrite(String),
}

#[derive(Error, Debug, Default)]
#[error("{status}: \n{message}")]
pub struct S2ServiceError {
pub message: String,
pub status: String,
}

impl From<ClientError> for S2ServiceError {
fn from(error: ClientError) -> Self {
match error {
ClientError::Service(status) => Self {
message: status.message().to_string(),
status: status.code().to_string(),
},
_ => Self {
message: error.to_string(),
..Default::default()
},
}
}
}

pub fn s2_status(error: &ClientError) -> String {
match error {
ClientError::Service(status) => status.code().to_string(),
_ => error.to_string(),
}
}
}

0 comments on commit 3646e60

Please sign in to comment.