Skip to content

Commit

Permalink
Cleanup and add context to UnknownError
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Jan 24, 2024
1 parent 21dd7b9 commit 954d6d8
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 20 deletions.
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
pub mod structs;
use sqlx::postgres::PgPoolOptions;
pub use structs::all_locks::AllLocks;
pub use structs::bloat::Bloat;
pub use structs::blocking::Blocking;
Expand Down Expand Up @@ -319,11 +320,10 @@ pub enum PgExtrasError {
#[error("Cannot connect to database")]
ConnectionError(),
#[error("Unknown pg-extras error")]
Unknown,
Unknown(String),
}

async fn get_rows<T: Tabular>(query: &str) -> Result<Vec<T>, PgExtrasError> {
use sqlx::postgres::PgPoolOptions;
let pool = match PgPoolOptions::new()
.max_connections(5)
.connect(db_url()?.as_str())
Expand All @@ -335,7 +335,7 @@ async fn get_rows<T: Tabular>(query: &str) -> Result<Vec<T>, PgExtrasError> {

Ok(match sqlx::query(query).fetch_all(&pool).await {
Ok(rows) => rows.iter().map(T::new).collect(),
Err(_) => return Err(PgExtrasError::Unknown),
Err(e) => return Err(PgExtrasError::Unknown(format!("An error occurred: {}", e))),
})
}

Expand All @@ -347,8 +347,6 @@ fn db_url() -> Result<String, PgExtrasError> {

#[cfg(test)]
mod tests {
use crate::structs::db_settings;

use super::*;

#[tokio::test]
Expand Down Expand Up @@ -385,7 +383,7 @@ mod tests {
render_table(buffercache_usage().await?);
render_table(ssl_used().await?);
render_table(connections().await?);
// render_table(db_settings().await?);
render_table(db_settings().await?);
Ok(())
}
}
3 changes: 1 addition & 2 deletions src/structs/all_locks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::structs::shared::{get_default_interval, Tabular};
use sqlx::postgres::types::PgInterval;
use sqlx::postgres::PgRow;
use sqlx::postgres::{types::PgInterval, PgRow};
use sqlx::Row;

#[derive(Debug, Clone)]
Expand Down
3 changes: 1 addition & 2 deletions src/structs/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::structs::shared::{get_default_interval, Tabular};
use sqlx::postgres::types::PgInterval;
use sqlx::postgres::PgRow;
use sqlx::postgres::{types::PgInterval, PgRow};
use sqlx::Row;

#[derive(Debug, Clone)]
Expand Down
3 changes: 1 addition & 2 deletions src/structs/calls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::structs::shared::{get_default_interval, Tabular};
use sqlx::postgres::types::PgInterval;
use sqlx::postgres::PgRow;
use sqlx::postgres::{types::PgInterval, PgRow};
use sqlx::Row;

#[derive(Debug, Clone)]
Expand Down
3 changes: 1 addition & 2 deletions src/structs/locks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::structs::shared::{get_default_interval, Tabular};
use sqlx::postgres::types::PgInterval;
use sqlx::postgres::PgRow;
use sqlx::postgres::{types::PgInterval, PgRow};
use sqlx::Row;

#[derive(Debug, Clone)]
Expand Down
3 changes: 1 addition & 2 deletions src/structs/long_running_queries.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::structs::shared::{get_default_interval, Tabular};
use sqlx::postgres::types::PgInterval;
use sqlx::postgres::PgRow;
use sqlx::postgres::{types::PgInterval, PgRow};
use sqlx::Row;

#[derive(Debug, Clone)]
Expand Down
3 changes: 1 addition & 2 deletions src/structs/outliers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::structs::shared::{get_default_interval, Tabular};
use sqlx::postgres::types::PgInterval;
use sqlx::postgres::PgRow;
use sqlx::postgres::{types::PgInterval, PgRow};
use sqlx::Row;

#[derive(Debug, Clone)]
Expand Down
3 changes: 1 addition & 2 deletions src/structs/shared.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use sqlx::postgres::types::PgInterval;
use sqlx::postgres::PgRow;
use sqlx::postgres::{types::PgInterval, PgRow};
use std::env;

pub trait Tabular {
Expand Down

0 comments on commit 954d6d8

Please sign in to comment.