Skip to content

Commit 9a52756

Browse files
committed
carify error message provenance
1 parent d3ac30e commit 9a52756

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- Better error messages for Microsoft SQL Server. SQLPage now displays the line number of the error, which is especially useful for debugging long migration scripts.
66
- Many improvements in the official website and the documentation.
77
Most notably, the documentation now has syntax highlighting on code blocks.
8+
- CLarify some ambiguous error messages:
9+
- make it clearer whether the error comes from SQLPage or from the database
10+
- specific tokenization errors are now displayed as such
811

912
## 0.13.0 (2023-10-16)
1013
- New [timeline](https://sql.ophir.dev/documentation.sql?component=timeline#component) component to display a timeline of events.

src/webserver/database/execute_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn parse_single_sql_result(
151151
}
152152

153153
fn clone_anyhow_err(err: &anyhow::Error) -> anyhow::Error {
154-
let mut e = anyhow!("An error occurred while trying to prepare this SQL statement");
154+
let mut e = anyhow!("SQLPage could not parse and prepare this SQL statement");
155155
for c in err.chain().rev() {
156156
e = e.context(c.to_string());
157157
}

src/webserver/database/sql.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::PreparedStatement;
33
use crate::file_cache::AsyncFromStrWithState;
44
use crate::utils::add_value_to_map;
55
use crate::{AppState, Database};
6+
use anyhow::Context;
67
use async_trait::async_trait;
78
use sqlparser::ast::{
89
BinaryOperator, DataType, Expr, Function, FunctionArg, FunctionArgExpr, Ident, ObjectName,
@@ -85,7 +86,9 @@ async fn prepare_query_with_params(
8586
}
8687
Err(err) => {
8788
log::warn!("Failed to prepare {query:?}: {err:#}");
88-
ParsedSQLStatement::Error(err)
89+
ParsedSQLStatement::Error(err.context(format!(
90+
"The database returned an error when preparing this SQL statement: {query}"
91+
)))
8992
}
9093
}
9194
}
@@ -118,7 +121,9 @@ fn parse_sql<'a>(
118121
dialect: &'a dyn Dialect,
119122
sql: &'a str,
120123
) -> anyhow::Result<impl Iterator<Item = ParsedStatement> + 'a> {
121-
let tokens = Tokenizer::new(dialect, sql).tokenize_with_location()?;
124+
let tokens = Tokenizer::new(dialect, sql)
125+
.tokenize_with_location()
126+
.with_context(|| "SQLPage's SQL parser could not tokenize the sql file")?;
122127
let mut parser = Parser::new(dialect).with_tokens_with_locations(tokens);
123128
let db_kind = kind_of_dialect(dialect);
124129
Ok(std::iter::from_fn(move || {
@@ -160,7 +165,7 @@ fn syntax_error(err: ParserError, parser: &mut Parser) -> ParsedStatement {
160165
if i == 0 {
161166
writeln!(
162167
&mut err_msg,
163-
"SQL syntax error on line {}, character {}:",
168+
"SQLPage found a syntax error on line {}, character {}:",
164169
next_token.location.line, next_token.location.column
165170
)
166171
.unwrap();

0 commit comments

Comments
 (0)