More helpful error messages when creating adaptors
This commit is contained in:
parent
2ba96cc9cb
commit
b586b4c88d
5 changed files with 22 additions and 20 deletions
|
|
@ -203,19 +203,22 @@ impl Adaptor for DatastoreAdaptor {
|
|||
|
||||
impl DatastoreAdaptor {
|
||||
pub async fn new() -> Self {
|
||||
let project_name = env::var("GCP_PROJECT_NAME").unwrap();
|
||||
|
||||
// Load credentials
|
||||
let credentials: ApplicationCredentials =
|
||||
serde_json::from_str(&env::var("GCP_CREDENTIALS").unwrap()).unwrap();
|
||||
let credentials: ApplicationCredentials = serde_json::from_str(
|
||||
&env::var("GCP_CREDENTIALS").expect("Expected GCP_CREDENTIALS environment variable"),
|
||||
)
|
||||
.expect("GCP_CREDENTIALS environment variable is not valid JSON");
|
||||
|
||||
// Connect to datastore
|
||||
let client = Client::from_credentials(project_name.clone(), credentials)
|
||||
let client = Client::from_credentials(credentials.project_id.clone(), credentials.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
.expect("Failed to setup datastore 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 }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -158,23 +158,28 @@ async fn get_stats_row(db: &DatabaseConnection) -> Result<stats::ActiveModel, Db
|
|||
|
||||
impl SqlAdaptor {
|
||||
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
|
||||
let db = Database::connect(&connection_string).await.unwrap();
|
||||
let db = Database::connect(&connection_string)
|
||||
.await
|
||||
.expect("Failed to connect to SQL database");
|
||||
println!(
|
||||
"{} Connected to database at {}",
|
||||
match db {
|
||||
DatabaseConnection::SqlxMySqlPoolConnection(_) => "🐬",
|
||||
DatabaseConnection::SqlxPostgresPoolConnection(_) => "🐘",
|
||||
DatabaseConnection::SqlxSqlitePoolConnection(_) => "🪶",
|
||||
DatabaseConnection::Disconnected => panic!("Failed to connect"),
|
||||
DatabaseConnection::Disconnected => panic!("Failed to connect to SQL database"),
|
||||
},
|
||||
connection_string
|
||||
);
|
||||
|
||||
// Setup tables
|
||||
Migrator::up(&db, None).await.unwrap();
|
||||
Migrator::up(&db, None)
|
||||
.await
|
||||
.expect("Failed to set up tables in the database");
|
||||
|
||||
Self { db }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue