-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy patherror_highlighting.rs
More file actions
192 lines (176 loc) · 5.76 KB
/
Copy patherror_highlighting.rs
File metadata and controls
192 lines (176 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::{
fmt::Write,
path::{Path, PathBuf},
};
use super::sql::{SourceSpan, StmtWithParams};
#[derive(Debug)]
struct NiceDatabaseError {
/// The source file that contains the query.
source_file: PathBuf,
/// The error that occurred.
db_err: sqlx::error::Error,
/// The query that was executed.
query: String,
/// The start location of the query in the source file, if the query was extracted from a larger file.
query_position: Option<SourceSpan>,
}
fn write_source_position_info(
f: &mut std::fmt::Formatter<'_>,
source_file: &Path,
query_position: Option<SourceSpan>,
) -> Result<(), std::fmt::Error> {
write!(f, "\n{}", source_file.display())?;
if let Some(query_position) = query_position {
let start_line = query_position.start.line;
let end_line = query_position.end.line;
if start_line == end_line {
write!(f, ": line {start_line}")?;
} else {
write!(f, ": lines {start_line} to {end_line}")?;
}
}
Ok(())
}
impl std::fmt::Display for NiceDatabaseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"In \"{}\": The following error occurred while executing an SQL statement:\n{}\n\nThe SQL statement sent by SQLPage was:\n",
self.source_file.display(),
self.db_err
)?;
if let sqlx::error::Error::Database(db_err) = &self.db_err {
let Some(mut offset) = db_err.offset() else {
write!(f, "{}", self.query)?;
self.show_position_info(f)?;
return Ok(());
};
for line in self.query.lines() {
if offset > line.len() {
offset -= line.len() + 1;
} else {
highlight_line_offset(f, line, offset);
self.show_position_info(f)?;
break;
}
}
Ok(())
} else {
write!(f, "{}", self.query)?;
self.show_position_info(f)?;
Ok(())
}
}
}
impl NiceDatabaseError {
fn show_position_info(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write_source_position_info(f, &self.source_file, self.query_position)
}
}
impl std::error::Error for NiceDatabaseError {}
#[derive(Debug)]
struct NicePositionedError {
source_file: PathBuf,
query_position: SourceSpan,
error: anyhow::Error,
}
impl std::fmt::Display for NicePositionedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "In \"{}\": {}", self.source_file.display(), self.error)?;
write_source_position_info(f, &self.source_file, Some(self.query_position))
}
}
impl std::error::Error for NicePositionedError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.error.as_ref())
}
}
/// Display a database error without any position information
#[must_use]
pub fn display_db_error(
source_file: &Path,
query: &str,
db_err: sqlx::error::Error,
) -> anyhow::Error {
anyhow::Error::new(NiceDatabaseError {
source_file: source_file.to_path_buf(),
db_err,
query: query.to_string(),
query_position: None,
})
}
/// Display a database error with a highlighted line and character offset.
#[must_use]
pub fn display_stmt_db_error(
source_file: &Path,
stmt: &StmtWithParams,
db_err: sqlx::error::Error,
) -> anyhow::Error {
anyhow::Error::new(NiceDatabaseError {
source_file: source_file.to_path_buf(),
db_err,
query: stmt.query.clone(),
query_position: Some(stmt.query_position),
})
}
#[must_use]
pub fn display_stmt_error(
source_file: &Path,
query_position: SourceSpan,
error: anyhow::Error,
) -> anyhow::Error {
anyhow::Error::new(NicePositionedError {
source_file: source_file.to_path_buf(),
query_position,
error,
})
}
/// Highlight a line with a character offset.
pub fn highlight_line_offset<W: std::fmt::Write>(msg: &mut W, line: &str, offset: usize) {
writeln!(msg, "{line}").unwrap();
writeln!(msg, "{}⬆️", " ".repeat(offset)).unwrap();
}
/// Highlight an error given a line and a character offset
/// line and `col_num` are 1-based
pub fn quote_source_with_highlight(source: &str, line_num: u64, col_num: u64) -> String {
let mut msg = String::new();
let col_num_usize = usize::try_from(col_num)
.unwrap_or_default()
.saturating_sub(1);
for (current_line_num, line) in (1_u64..).zip(source.lines()) {
if current_line_num + 1 == line_num || current_line_num == line_num + 1 {
writeln!(msg, "{line}").unwrap();
} else if current_line_num == line_num {
highlight_line_offset(&mut msg, line, col_num_usize);
} else if current_line_num > line_num + 1 {
break;
}
}
msg
}
#[test]
fn test_display_stmt_error_includes_file_and_line() {
let err = display_stmt_error(
Path::new("example.sql"),
SourceSpan {
start: super::sql::SourceLocation {
line: 12,
column: 3,
},
end: super::sql::SourceLocation {
line: 12,
column: 17,
},
},
anyhow::anyhow!("boom"),
);
let message = err.to_string();
assert!(message.contains("In \"example.sql\": boom"));
assert!(message.contains("example.sql: line 12"));
}
#[test]
fn test_quote_source_with_highlight() {
let source = "SELECT *\nFROM table\nWHERE <syntax error>";
let expected = "FROM table\nWHERE <syntax error>\n ⬆️\n";
assert_eq!(quote_source_with_highlight(source, 3, 6), expected);
}