use std::io;
use std::path::PathBuf;
use deadpool_postgres::tokio_postgres::Error as TokioPostgresError;
use deadpool_postgres::tokio_postgres::config::SslMode;
use deadpool_postgres::tokio_postgres::error::DbError;
use deadpool_postgres::{BuildError, PoolError};
use martin_tile_utils::TileCoord;
use semver::Version;
use crate::tiles::UrlQuery;
use crate::tiles::postgres::utils::query_to_json;
use crate::tiles::postgres::{RedactedConnectionString, TileWkbError};
pub type PostgresResult<T> = Result<T, PostgresError>;
#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
pub enum PostgresError {
#[error("Filter '{0}' is not valid CQL2: {1}")]
InvalidFilter(String, String),
#[error("Cannot load platform root certificates: {0:?}")]
CannotLoadRoots(Vec<rustls_native_certs::Error>),
#[error("Cannot open certificate file {1}: {0}")]
CannotOpenCert(#[source] io::Error, PathBuf),
#[error("Cannot parse certificate file {1}: {0}")]
CannotParseCert(#[source] io::Error, PathBuf),
#[error("Unable to parse PEM RSA key file {0}")]
InvalidPrivateKey(PathBuf),
#[error("Unable to use client certificate pair {cert} / {key}: {source}")]
CannotUseClientKey {
#[source]
source: rustls::Error,
cert: PathBuf,
key: PathBuf,
},
#[error(transparent)]
RustlsError(#[from] rustls::Error),
#[error(transparent)]
CannotBuildTlsVerifier(#[from] rustls::client::VerifierBuilderError),
#[error("Unknown SSL mode: {0:?}")]
UnknownSslMode(SslMode),
#[error("Postgres error while {1}: {0}")]
PostgresError(#[source] TokioPostgresError, &'static str),
#[error("PostGIS cannot transform SRID {1} into SRID {2}: {0}")]
CannotTransform(#[source] Box<DbError>, i32, i32),
#[error("Unable to build a Postgres connection pool {1}: {0}")]
PostgresPoolBuildError(#[source] BuildError, String),
#[error("Unable to get a Postgres connection from the pool {1}: {0}")]
PostgresPoolConnError(#[source] PoolError, String),
#[error("Unable to parse connection string {1}: {0}")]
BadConnectionString(#[source] TokioPostgresError, RedactedConnectionString),
#[error("Unable to parse PostGIS version {1}: {0}")]
BadPostgisVersion(#[source] semver::Error, String),
#[error("Unable to parse PostgreSQL version {version_num}")]
BadPostgresVersion {
version_num: i32,
},
#[error("PostGIS version {current} is too old, minimum required is {minimum}")]
PostgisTooOld {
current: Version,
minimum: Version,
},
#[error("PostgreSQL version {current} is too old, minimum required is {minimum}")]
PostgresqlTooOld {
current: Version,
minimum: Version,
},
#[error("Error preparing a query for the tile '{source_id}' ({signature}): {query} {source}")]
PrepareQueryError {
#[source]
source: TokioPostgresError,
source_id: String,
signature: String,
query: String,
},
#[error(r"Unable to get tile {2:#} from {1}: {0}")]
GetTileError(#[source] TokioPostgresError, String, TileCoord),
#[error(r"Unable to get tile {2:#} with {json_query:?} params from {1}: {0}", json_query=query_to_json(.3.as_ref()))]
GetTileWithQueryError(#[source] TokioPostgresError, String, TileCoord, Option<UrlQuery>),
#[error("Unable to read a tile feature's geometry: {0}")]
BadTileGeometry(#[from] TileWkbError),
#[error("Column {column} has the PostgreSQL type {pg_type}, which cannot be encoded as a tile property")]
UnsupportedPropertyType {
column: String,
pg_type: String,
},
#[error("MLT encoding failed: {0}")]
MltEncoding(#[source] Box<mlt_core::MltError>),
#[cfg(feature = "unstable-generate")]
#[error("Cannot serve the layers of table source {0}: {1}")]
EngineTable(String, #[source] Box<dyn std::error::Error + Send + Sync>),
#[cfg(feature = "unstable-generate")]
#[error(r"Unable to render tile {2:#} of {1}: {0}")]
EngineError(#[source] Box<martin_tilegen::TileGenError>, String, TileCoord),
}
impl crate::Classify for PostgresError {
fn kind(&self) -> crate::ErrorKind {
use crate::ErrorKind::{Internal, Unavailable};
match self {
Self::PostgresPoolConnError(..) => Unavailable,
Self::InvalidFilter(..)
| Self::CannotLoadRoots(_)
| Self::CannotOpenCert(..)
| Self::CannotParseCert(..)
| Self::InvalidPrivateKey(_)
| Self::CannotUseClientKey { .. }
| Self::RustlsError(_)
| Self::CannotBuildTlsVerifier(_)
| Self::UnknownSslMode(_)
| Self::PostgresError(..)
| Self::CannotTransform(..)
| Self::PostgresPoolBuildError(..)
| Self::BadConnectionString(..)
| Self::BadPostgisVersion(..)
| Self::BadPostgresVersion { .. }
| Self::PostgisTooOld { .. }
| Self::PostgresqlTooOld { .. }
| Self::PrepareQueryError { .. }
| Self::GetTileError(..)
| Self::GetTileWithQueryError(..)
| Self::BadTileGeometry(_)
| Self::UnsupportedPropertyType { .. }
| Self::MltEncoding(_) => Internal,
#[cfg(feature = "unstable-generate")]
Self::EngineTable(..) | Self::EngineError(..) => Internal,
}
}
}