Skip to content

fix: report db connection errors #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions crates/pgt_typecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ mod diagnostics;
pub use diagnostics::TypecheckDiagnostic;
use diagnostics::create_type_error;
use pgt_text_size::TextRange;
use sqlx::Executor;
use sqlx::PgPool;
use sqlx::postgres::PgDatabaseError;
pub use sqlx::postgres::PgSeverity;
use sqlx::{Executor, PgPool};

#[derive(Debug)]
pub struct TypecheckParams<'a> {
Expand All @@ -29,7 +28,9 @@ pub struct TypeError {
pub constraint: Option<String>,
}

pub async fn check_sql(params: TypecheckParams<'_>) -> Option<TypecheckDiagnostic> {
pub async fn check_sql(
params: TypecheckParams<'_>,
) -> Result<Option<TypecheckDiagnostic>, sqlx::Error> {
// Check if the AST is not a supported statement type
if !matches!(
params.ast,
Expand All @@ -39,13 +40,10 @@ pub async fn check_sql(params: TypecheckParams<'_>) -> Option<TypecheckDiagnosti
| pgt_query_ext::NodeEnum::DeleteStmt(_)
| pgt_query_ext::NodeEnum::CommonTableExpr(_)
) {
return None;
return Ok(None);
}

let mut conn = match params.conn.acquire().await {
Ok(c) => c,
Err(_) => return None,
};
let mut conn = params.conn.acquire().await?;

// Postgres caches prepared statements within the current DB session (connection).
// This can cause issues if the underlying table schema changes while statements
Expand All @@ -56,11 +54,11 @@ pub async fn check_sql(params: TypecheckParams<'_>) -> Option<TypecheckDiagnosti
let res = conn.prepare(params.sql).await;

match res {
Ok(_) => None,
Ok(_) => Ok(None),
Err(sqlx::Error::Database(err)) => {
let pg_err = err.downcast_ref::<PgDatabaseError>();
Some(create_type_error(pg_err, params.tree))
Ok(Some(create_type_error(pg_err, params.tree)))
}
Err(_) => None,
Err(err) => Err(err),
}
}
2 changes: 1 addition & 1 deletion crates/pgt_typecheck/tests/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn test(name: &str, query: &str, setup: &str) {

Formatter::new(&mut writer)
.write_markup(markup! {
{PrintDiagnostic::simple(&result.unwrap())}
{PrintDiagnostic::simple(&result.unwrap().unwrap())}
})
.unwrap();

Expand Down
19 changes: 11 additions & 8 deletions crates/pgt_workspace/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,6 @@ impl Workspace for WorkspaceServer {

let mut diagnostics: Vec<SDiagnostic> = parser.document_diagnostics().to_vec();

// TODO: run this in parallel with rayon based on rayon.count()

if let Some(pool) = self
.connection
.read()
Expand All @@ -385,13 +383,15 @@ impl Workspace for WorkspaceServer {
})
.await
.map(|d| {
let r = d.location().span.map(|span| span + range.start());
d.map(|d| {
let r = d.location().span.map(|span| span + range.start());

d.with_file_path(path.as_path().display().to_string())
.with_file_span(r.unwrap_or(range))
d.with_file_path(path.as_path().display().to_string())
.with_file_span(r.unwrap_or(range))
})
})
} else {
None
Ok(None)
}
}
})
Expand All @@ -400,8 +400,11 @@ impl Workspace for WorkspaceServer {
.await
})?;

for result in async_results.into_iter().flatten() {
diagnostics.push(SDiagnostic::new(result));
for result in async_results.into_iter() {
let result = result?;
if let Some(diag) = result {
diagnostics.push(SDiagnostic::new(diag));
}
}
}

Expand Down
Loading