More helpful error messages when creating adaptors

This commit is contained in:
Ben Grant 2023-05-15 20:11:41 +10:00
parent 2ba96cc9cb
commit b586b4c88d
5 changed files with 22 additions and 20 deletions

8
backend/Cargo.lock generated
View file

@ -617,7 +617,7 @@ dependencies = [
"chrono", "chrono",
"common", "common",
"datastore-adaptor", "datastore-adaptor",
"dotenv", "dotenvy",
"memory-adaptor", "memory-adaptor",
"punycode", "punycode",
"rand", "rand",
@ -814,12 +814,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]] [[package]]
name = "dotenvy" name = "dotenvy"
version = "0.15.7" version = "0.15.7"

View file

@ -20,7 +20,7 @@ common = { path = "common" }
sql-adaptor = { path = "adaptors/sql" } sql-adaptor = { path = "adaptors/sql" }
datastore-adaptor = { path = "adaptors/datastore" } datastore-adaptor = { path = "adaptors/datastore" }
memory-adaptor = { path = "adaptors/memory" } memory-adaptor = { path = "adaptors/memory" }
dotenv = "0.15.0" dotenvy = "0.15.7"
serde_json = "1.0.96" serde_json = "1.0.96"
rand = "0.8.5" rand = "0.8.5"
punycode = "0.4.1" punycode = "0.4.1"

View file

@ -203,19 +203,22 @@ impl Adaptor for DatastoreAdaptor {
impl DatastoreAdaptor { impl DatastoreAdaptor {
pub async fn new() -> Self { pub async fn new() -> Self {
let project_name = env::var("GCP_PROJECT_NAME").unwrap();
// Load credentials // Load credentials
let credentials: ApplicationCredentials = let credentials: ApplicationCredentials = serde_json::from_str(
serde_json::from_str(&env::var("GCP_CREDENTIALS").unwrap()).unwrap(); &env::var("GCP_CREDENTIALS").expect("Expected GCP_CREDENTIALS environment variable"),
)
.expect("GCP_CREDENTIALS environment variable is not valid JSON");
// Connect to datastore // Connect to datastore
let client = Client::from_credentials(project_name.clone(), credentials) let client = Client::from_credentials(credentials.project_id.clone(), credentials.clone())
.await .await
.unwrap(); .expect("Failed to setup datastore client");
let client = Mutex::new(client); let client = Mutex::new(client);
println!("🎛️ Connected to datastore in project {}", project_name); println!(
"🎛️ Connected to datastore in project {}",
credentials.project_id
);
Self { client } Self { client }
} }

View file

@ -158,23 +158,28 @@ async fn get_stats_row(db: &DatabaseConnection) -> Result<stats::ActiveModel, Db
impl SqlAdaptor { impl SqlAdaptor {
pub async fn new() -> Self { pub async fn new() -> Self {
let connection_string = env::var("DATABASE_URL").unwrap(); let connection_string =
env::var("DATABASE_URL").expect("Expected DATABASE_URL environment variable");
// Connect to the database // Connect to the database
let db = Database::connect(&connection_string).await.unwrap(); let db = Database::connect(&connection_string)
.await
.expect("Failed to connect to SQL database");
println!( println!(
"{} Connected to database at {}", "{} Connected to database at {}",
match db { match db {
DatabaseConnection::SqlxMySqlPoolConnection(_) => "🐬", DatabaseConnection::SqlxMySqlPoolConnection(_) => "🐬",
DatabaseConnection::SqlxPostgresPoolConnection(_) => "🐘", DatabaseConnection::SqlxPostgresPoolConnection(_) => "🐘",
DatabaseConnection::SqlxSqlitePoolConnection(_) => "🪶", DatabaseConnection::SqlxSqlitePoolConnection(_) => "🪶",
DatabaseConnection::Disconnected => panic!("Failed to connect"), DatabaseConnection::Disconnected => panic!("Failed to connect to SQL database"),
}, },
connection_string connection_string
); );
// Setup tables // Setup tables
Migrator::up(&db, None).await.unwrap(); Migrator::up(&db, None)
.await
.expect("Failed to set up tables in the database");
Self { db } Self { db }
} }

View file

@ -35,7 +35,7 @@ async fn main() {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
// Load env // Load env
dotenv::dotenv().ok(); dotenvy::dotenv().ok();
let shared_state = Arc::new(Mutex::new(ApiState { let shared_state = Arc::new(Mutex::new(ApiState {
adaptor: create_adaptor().await, adaptor: create_adaptor().await,