Skip to content

Commit

Permalink
Refactor config validation and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Saru2003 committed Oct 23, 2024
1 parent d847bee commit 5f69d95
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 11 deletions.
9 changes: 2 additions & 7 deletions martin/src/pg/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,11 @@ impl PgConfig {
pub fn validate(&self) -> PgResult<()> {
if let Some(pool_size) = self.pool_size {
if pool_size < 1 {
return Err(PgError::ValidationError(
"pool_size must be greater than or equal to 1.".to_string(),
));
return Err(PgError::ConfigError("pool_size must be greater than or equal to 1."));
}
}
if self.connection_string.is_none() {
return Err(PgError::ValidationError(
"A connection string must be provided.".to_string(),
));
return Err(PgError::ConfigError("A connection string must be provided."));
}

Ok(())
Expand All @@ -133,7 +129,6 @@ impl PgConfig {
}

pub async fn resolve(&mut self, id_resolver: IdResolver) -> MartinResult<TileInfoSources> {
self.validate()?;
let pg = PgBuilder::new(self, id_resolver).await?;
let inst_tables = on_slow(
pg.instantiate_tables(),
Expand Down
4 changes: 2 additions & 2 deletions martin/src/pg/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ pub enum PgError {
#[error(r#"Unable to get tile {2:#} with {:?} params from {1}: {0}"#, query_to_json(.3.as_ref()))]
GetTileWithQueryError(#[source] TokioPgError, String, TileCoord, Option<UrlQuery>),

#[error("Validation error: {0}")]
ValidationError(String),
#[error("Configuration error: {0}")]
ConfigError(&'static str),
}
2 changes: 0 additions & 2 deletions martin/src/pg/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ pub struct PgPool {

impl PgPool {
pub async fn new(config: &PgConfig) -> PgResult<Self> {
config.validate()?;

let (id, mgr) = Self::parse_config(config)?;

let pool = Pool::builder(mgr)
Expand Down

0 comments on commit 5f69d95

Please sign in to comment.