Improve error handling
This commit is contained in:
parent
fa7dc5a3f9
commit
360f3fcbbf
2 changed files with 28 additions and 19 deletions
21
src/db.rs
21
src/db.rs
|
@ -43,24 +43,20 @@ pub struct SqliteDatabase {
|
||||||
|
|
||||||
impl SqliteDatabase {
|
impl SqliteDatabase {
|
||||||
#[instrument]
|
#[instrument]
|
||||||
pub async fn open(connection_string: &str) -> Self {
|
pub async fn open(connection_string: &str) -> Result<Self, sqlx::Error> {
|
||||||
let options = SqliteConnectOptions::from_str(connection_string)
|
let options = SqliteConnectOptions::from_str(connection_string)?.create_if_missing(true);
|
||||||
.expect("Invalid database URI")
|
|
||||||
.create_if_missing(true);
|
|
||||||
|
|
||||||
let mut db = Self {
|
let mut db = Self {
|
||||||
conn: SqliteConnection::connect_with(&options)
|
conn: SqliteConnection::connect_with(&options).await?,
|
||||||
.await
|
|
||||||
.expect("Failed to open SQLite database"),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
db.init().await;
|
db.init().await?;
|
||||||
|
|
||||||
db
|
Ok(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
pub async fn init(&mut self) {
|
async fn init(&mut self) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"CREATE TABLE IF NOT EXISTS user_tokens (
|
"CREATE TABLE IF NOT EXISTS user_tokens (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
@ -72,8 +68,9 @@ impl SqliteDatabase {
|
||||||
)"
|
)"
|
||||||
)
|
)
|
||||||
.execute(&mut self.conn)
|
.execute(&mut self.conn)
|
||||||
.await
|
.await?;
|
||||||
.expect("Failed to initialize table user_tokens");
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
26
src/main.rs
26
src/main.rs
|
@ -25,13 +25,13 @@ mod db;
|
||||||
mod secrets;
|
mod secrets;
|
||||||
mod server;
|
mod server;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::{path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
use auth::{
|
use auth::{
|
||||||
AuthenticationBackend, ServerPadlockGenerator, UserAuthenticator, UserServerKeyGenerator,
|
AuthenticationBackend, ServerPadlockGenerator, UserAuthenticator, UserServerKeyGenerator,
|
||||||
};
|
};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use color_eyre::Result;
|
use color_eyre::{eyre::Context, Result};
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use db::{Database, SqliteDatabase};
|
use db::{Database, SqliteDatabase};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
@ -78,17 +78,29 @@ struct Args {
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
init()?;
|
init().context("Failed to initialize tracing")?;
|
||||||
|
|
||||||
let config = load_config(&args.config).await?;
|
let config = load_config(&args.config).await.with_context(|| {
|
||||||
|
if let Ok(path) = PathBuf::from(&args.config).canonicalize() {
|
||||||
|
format!("Failed to load config from {path:?}")
|
||||||
|
} else {
|
||||||
|
format!("Failed to load config from invalid path {}", &args.config)
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
|
||||||
let database: Arc<Mutex<Box<dyn Database + Send>>> = Arc::new(Mutex::new(Box::new(
|
let database: Arc<Mutex<Box<dyn Database + Send>>> = Arc::new(Mutex::new(Box::new(
|
||||||
SqliteDatabase::open(&config.database.connection_string).await,
|
SqliteDatabase::open(&config.database.connection_string)
|
||||||
|
.await
|
||||||
|
.context("Failed to open database")?,
|
||||||
)));
|
)));
|
||||||
|
|
||||||
let mut auth_backends = vec![];
|
let mut auth_backends = vec![];
|
||||||
for c in config.auth_backends {
|
for (i, c) in config.auth_backends.into_iter().enumerate() {
|
||||||
auth_backends.push(AuthenticationBackend::new(c).await?);
|
auth_backends.push(
|
||||||
|
AuthenticationBackend::new(c)
|
||||||
|
.await
|
||||||
|
.with_context(|| format!("Failed to initialize backend {i}"))?,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let user_authenticator = Arc::new(UserAuthenticator::new(database, auth_backends));
|
let user_authenticator = Arc::new(UserAuthenticator::new(database, auth_backends));
|
||||||
|
|
Loading…
Reference in a new issue