Skip to content

Commit

Permalink
upgrade http-error to 0.3.0-alpha.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rkusa committed Feb 8, 2024
1 parent a90a48d commit e38f6e2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
10 changes: 8 additions & 2 deletions postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ edition = "2021"

[features]
default = ["json", "time", "uuid"]
json = ["sqlm-postgres-macros/json", "tokio-postgres/with-serde_json-1", "dep:serde_json"]
json = [
"sqlm-postgres-macros/json",
"tokio-postgres/with-serde_json-1",
"dep:serde_json",
]
time = ["sqlm-postgres-macros/time", "tokio-postgres/with-time-0_3", "dep:time"]
uuid = ["sqlm-postgres-macros/uuid", "tokio-postgres/with-uuid-1", "dep:uuid"]

[dependencies]
deadpool-postgres = "0.11"
dotenvy = "0.15"
http-error = { version = "0.2" } # git = "https://github.com/rkusa/http-error.git", rev = "1f0630c" } # path = "../../http-error" }
http-error = { version = "0.3.0-alpha.1", features = [
"tracing",
] } # git = "https://github.com/rkusa/http-error.git", rev = "1f0630c" } # path = "../../http-error" }
once_cell = "1.17"
rustls = { version = "0.21", features = ["dangerous_configuration"] }
serde_json = { version = "1.0", optional = true }
Expand Down
54 changes: 40 additions & 14 deletions postgres/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
use std::{error, fmt};

use http_error::{HttpError, StatusCode};
use tracing::Span;

#[derive(Debug)]
pub enum Error {
pub struct Error {
pub kind: ErrorKind,
span: Span,
}

#[derive(Debug)]
pub enum ErrorKind {
MissingDatabaseUrlEnv,
Postgres(tokio_postgres::Error),
Build(deadpool_postgres::BuildError),
Pool(deadpool_postgres::PoolError),
}

impl Error {
fn new(kind: ErrorKind) -> Self {
Self {
kind,
span: Span::current(),
}
}
}

impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::MissingDatabaseUrlEnv => None,
Error::Postgres(err) => Some(err),
Error::Build(err) => Some(err),
Error::Pool(err) => Some(err),
match &self.kind {
ErrorKind::MissingDatabaseUrlEnv => None,
ErrorKind::Postgres(err) => Some(err),
ErrorKind::Build(err) => Some(err),
ErrorKind::Pool(err) => Some(err),
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::MissingDatabaseUrlEnv => f.write_str("env DATABASE_URL not set"),
Error::Postgres(err) => err.fmt(f),
Error::Build(_) => write!(f, "failed to build postgres connection pool"),
Error::Pool(_) => write!(f, "failed to acquire postgress connection from pool"),
match &self.kind {
ErrorKind::MissingDatabaseUrlEnv => f.write_str("env DATABASE_URL not set"),
ErrorKind::Postgres(err) => err.fmt(f),
ErrorKind::Build(_) => write!(f, "failed to build postgres connection pool"),
ErrorKind::Pool(_) => write!(f, "failed to acquire postgress connection from pool"),
}
}
}
Expand All @@ -36,22 +52,32 @@ impl HttpError for Error {
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}

fn span(&self) -> Option<&tracing::Span> {
Some(&self.span)
}
}

impl From<tokio_postgres::Error> for Error {
fn from(err: tokio_postgres::Error) -> Self {
Self::Postgres(err)
Self::new(ErrorKind::Postgres(err))
}
}

impl From<deadpool_postgres::BuildError> for Error {
fn from(err: deadpool_postgres::BuildError) -> Self {
Self::Build(err)
Self::new(ErrorKind::Build(err))
}
}

impl From<deadpool_postgres::PoolError> for Error {
fn from(err: deadpool_postgres::PoolError) -> Self {
Self::Pool(err)
Self::new(ErrorKind::Pool(err))
}
}

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Self::new(kind)
}
}
10 changes: 8 additions & 2 deletions postgres/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::time::Instant;

use tracing::Instrument;

use crate::error::ErrorKind;
use crate::query::Query;
use crate::{Error, Sql};

Expand Down Expand Up @@ -33,14 +34,19 @@ where
tracing::trace!(?elapsed, "sql query finished");
return Ok(r);
}
Err(Error::Postgres(err)) if err.is_closed() && i <= 5 => {
Err(Error {
kind: ErrorKind::Postgres(err),
..
}) if err.is_closed() && i <= 5 => {
// retry pool size + 1 times if connection is closed (might have
// received a closed one from the connection pool)
i += 1;
tracing::trace!("retry due to connection closed error");
continue;
}
Err(err) => return Err(err),
Err(err) => {
return Err(err);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::sync::Arc;
pub use deadpool_postgres::Transaction;
use deadpool_postgres::{ClientWrapper, Manager, ManagerConfig, Object, Pool, RecyclingMethod};
pub use error::Error;
use error::ErrorKind;
pub use future::SqlFuture;
use once_cell::sync::OnceCell;
pub use query::Query;
Expand All @@ -42,7 +43,7 @@ pub async fn connect() -> Result<Connection, Error> {
let pool = POOL.get_or_try_init(|| {
let mut config = tokio_postgres::Config::from_str(
dotenvy::var("DATABASE_URL")
.map_err(|_| Error::MissingDatabaseUrlEnv)?
.map_err(|_| ErrorKind::MissingDatabaseUrlEnv)?
.as_str(),
)?;
config.application_name(env!("CARGO_PKG_NAME"));
Expand Down

0 comments on commit e38f6e2

Please sign in to comment.