fix: add proper targets to db logging, panic in debug mode

feat/type-attributes
Tomáš Mládek 2022-10-23 16:07:08 +02:00
parent 6394a70030
commit 3292a5b346
2 changed files with 14 additions and 4 deletions

View File

@ -30,6 +30,7 @@ use diesel::r2d2::{self, ConnectionManager};
use diesel::result::{DatabaseErrorKind, Error};
use diesel::sqlite::SqliteConnection;
use hierarchies::initialize_hier;
use shadow_rs::is_debug;
use std::convert::TryFrom;
use std::fs;
use std::path::{Path, PathBuf};
@ -73,11 +74,16 @@ impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error>
type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
#[derive(Debug)]
pub struct LoggingHandler {}
pub struct LoggingHandler {
pub target: &'static str,
}
impl diesel::r2d2::HandleError<diesel::r2d2::Error> for LoggingHandler {
fn handle_error(&self, error: diesel::r2d2::Error) {
error!("{}", error);
error!(target = self.target, "{}", error);
if is_debug() {
panic!("This should not happen! {}", error);
}
}
}
@ -121,7 +127,9 @@ impl UpEndDatabase {
enable_foreign_keys: true,
busy_timeout: Some(Duration::from_secs(30)),
}))
.error_handler(Box::new(LoggingHandler {}))
.error_handler(Box::new(LoggingHandler {
target: "upend::db::main",
}))
.build(manager)?;
trace!("Pool created.");

View File

@ -122,7 +122,9 @@ impl FsStore {
let pool = r2d2::Pool::builder()
.connection_customizer(Box::new(ConnectionOptions {}))
.error_handler(Box::new(LoggingHandler {}))
.error_handler(Box::new(LoggingHandler {
target: "upend::db::fs",
}))
.build(manager)?;
let lock = Arc::new(RwLock::new(()));