Skip to content

Commit 1adfd23

Browse files
authored
fix: report db connection errors (#348)
* fix: report db connection errors * Update lib.rs
1 parent 4792d06 commit 1adfd23

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

crates/pgt_typecheck/src/lib.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ mod diagnostics;
33
pub use diagnostics::TypecheckDiagnostic;
44
use diagnostics::create_type_error;
55
use pgt_text_size::TextRange;
6-
use sqlx::Executor;
7-
use sqlx::PgPool;
86
use sqlx::postgres::PgDatabaseError;
97
pub use sqlx::postgres::PgSeverity;
8+
use sqlx::{Executor, PgPool};
109

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

32-
pub async fn check_sql(params: TypecheckParams<'_>) -> Option<TypecheckDiagnostic> {
31+
pub async fn check_sql(
32+
params: TypecheckParams<'_>,
33+
) -> Result<Option<TypecheckDiagnostic>, sqlx::Error> {
3334
// Check if the AST is not a supported statement type
3435
if !matches!(
3536
params.ast,
@@ -39,13 +40,10 @@ pub async fn check_sql(params: TypecheckParams<'_>) -> Option<TypecheckDiagnosti
3940
| pgt_query_ext::NodeEnum::DeleteStmt(_)
4041
| pgt_query_ext::NodeEnum::CommonTableExpr(_)
4142
) {
42-
return None;
43+
return Ok(None);
4344
}
4445

45-
let mut conn = match params.conn.acquire().await {
46-
Ok(c) => c,
47-
Err(_) => return None,
48-
};
46+
let mut conn = params.conn.acquire().await?;
4947

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

5856
match res {
59-
Ok(_) => None,
57+
Ok(_) => Ok(None),
6058
Err(sqlx::Error::Database(err)) => {
6159
let pg_err = err.downcast_ref::<PgDatabaseError>();
62-
Some(create_type_error(pg_err, params.tree))
60+
Ok(Some(create_type_error(pg_err, params.tree)))
6361
}
64-
Err(_) => None,
62+
Err(err) => Err(err),
6563
}
6664
}

crates/pgt_typecheck/tests/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async fn test(name: &str, query: &str, setup: &str) {
3737

3838
Formatter::new(&mut writer)
3939
.write_markup(markup! {
40-
{PrintDiagnostic::simple(&result.unwrap())}
40+
{PrintDiagnostic::simple(&result.unwrap().unwrap())}
4141
})
4242
.unwrap();
4343

crates/pgt_workspace/src/workspace/server.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,6 @@ impl Workspace for WorkspaceServer {
360360

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

363-
// TODO: run this in parallel with rayon based on rayon.count()
364-
365363
if let Some(pool) = self
366364
.connection
367365
.read()
@@ -385,13 +383,15 @@ impl Workspace for WorkspaceServer {
385383
})
386384
.await
387385
.map(|d| {
388-
let r = d.location().span.map(|span| span + range.start());
386+
d.map(|d| {
387+
let r = d.location().span.map(|span| span + range.start());
389388

390-
d.with_file_path(path.as_path().display().to_string())
391-
.with_file_span(r.unwrap_or(range))
389+
d.with_file_path(path.as_path().display().to_string())
390+
.with_file_span(r.unwrap_or(range))
391+
})
392392
})
393393
} else {
394-
None
394+
Ok(None)
395395
}
396396
}
397397
})
@@ -400,8 +400,11 @@ impl Workspace for WorkspaceServer {
400400
.await
401401
})?;
402402

403-
for result in async_results.into_iter().flatten() {
404-
diagnostics.push(SDiagnostic::new(result));
403+
for result in async_results.into_iter() {
404+
let result = result?;
405+
if let Some(diag) = result {
406+
diagnostics.push(SDiagnostic::new(diag));
407+
}
405408
}
406409
}
407410

0 commit comments

Comments
 (0)