Skip to content

Commit

Permalink
Validate PgConfig to make sure pool size is non-zero and connection s…
Browse files Browse the repository at this point in the history
…tr is set (#1546)

fixes #1542
  • Loading branch information
Saru2003 authored Oct 23, 2024
1 parent 0ba3a16 commit 1326157
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions martin/src/pg/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::pg::builder::PgBuilder;
use crate::pg::config_function::FuncInfoSources;
use crate::pg::config_table::TableInfoSources;
use crate::pg::utils::on_slow;
use crate::pg::PgError;
use crate::pg::PgResult;
use crate::source::TileInfoSources;
use crate::utils::{IdResolver, OptBoolObj, OptOneMany};
Expand Down Expand Up @@ -94,6 +95,23 @@ pub struct PgCfgPublishFuncs {

impl PgConfig {
/// Apply defaults to the config, and validate if there is a connection string
pub fn validate(&self) -> PgResult<()> {
if let Some(pool_size) = self.pool_size {
if pool_size < 1 {
return Err(PgError::ConfigError(
"pool_size must be greater than or equal to 1.",
));
}
}
if self.connection_string.is_none() {
return Err(PgError::ConfigError(
"A connection string must be provided.",
));
}

Ok(())
}

pub fn finalize(&mut self) -> PgResult<UnrecognizedValues> {
let mut res = UnrecognizedValues::new();
if let Some(ref ts) = self.tables {
Expand All @@ -110,6 +128,7 @@ impl PgConfig {
self.auto_publish = OptBoolObj::Bool(true);
}

self.validate()?;
Ok(res)
}

Expand Down
3 changes: 3 additions & 0 deletions martin/src/pg/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ 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("Configuration error: {0}")]
ConfigError(&'static str),
}

0 comments on commit 1326157

Please sign in to comment.