From 2510acd14e5588f7a620c4c26f89acb92a2abf5d Mon Sep 17 00:00:00 2001 From: lovasoa Date: Wed, 28 Feb 2024 09:28:40 +0100 Subject: [PATCH] better errors on invalid database connection strings --- CHANGELOG.md | 1 + src/webserver/database/connect.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 558b4bd8..6c9e5b64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.19.1 (2024-02-28) - **SECURITY**: fixes users being able to re-run migrations by visiting `/sqlpage/migrations/NNNN_name.sql` pages. If you are using sqlpage migrations, your migrations are not idempotent, and you use the default SQLPAGE_WEB_ROOT (`./`) and `SQLPAGE_CONFIGURATION_DIRECTORY` (`./sqlpage/`), you should upgrade to this version as soon as possible. If you are using a custom `SQLPAGE_WEB_ROOT` or `SQLPAGE_CONFIGURATION_DIRECTORY` or your migrations are idempotent, you can upgrade at your convenience. + - Better error messages on invalid database connection strings. SQLPage now displays a more precise and useful message when an error occurs instead of a "panic" message. ## 0.19.0 (2024-02-25) diff --git a/src/webserver/database/connect.rs b/src/webserver/database/connect.rs index ab4654c2..7776d165 100644 --- a/src/webserver/database/connect.rs +++ b/src/webserver/database/connect.rs @@ -2,6 +2,7 @@ use std::time::Duration; use super::Database; use crate::{app_config::AppConfig, ON_CONNECT_FILE}; +use anyhow::Context; use sqlx::{ any::{Any, AnyConnectOptions, AnyKind}, pool::PoolOptions, @@ -13,7 +14,9 @@ impl Database { pub async fn init(config: &AppConfig) -> anyhow::Result { let database_url = &config.database_url; let mut connect_options: AnyConnectOptions = - database_url.parse().expect("Invalid database URL"); + database_url + .parse() + .with_context(|| format!("{database_url:?} is not a valid database URL"))?; connect_options.log_statements(log::LevelFilter::Trace); connect_options.log_slow_statements( log::LevelFilter::Warn,