Skip to content

Commit 6437c37

Browse files
committed
Add context line before and after and use up arrow
1 parent 16b984a commit 6437c37

File tree

1 file changed

+19
-5
lines changed
  • tokio-postgres/src/error

1 file changed

+19
-5
lines changed

tokio-postgres/src/error/mod.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,24 @@ impl DbError {
250250
ErrorPosition::Original(idx) => (self.statement.as_deref()?, *idx),
251251
ErrorPosition::Internal { position, query } => (query.as_str(), *position),
252252
};
253-
let (first, last) = sql.split_at_checked(pos.saturating_sub(1) as usize)?;
254-
let first = first.lines().last().unwrap_or_default();
255-
let last = last.lines().next().unwrap_or_default();
256-
Some(format!("{first}/*ERROR =>*/{last}"))
253+
let (before, after) = sql.split_at_checked(pos.saturating_sub(1) as usize)?;
254+
let before: Vec<&str> = before.lines().collect();
255+
let after: Vec<&str> = after.lines().collect();
256+
257+
let mut before_str = before[before.len().saturating_sub(2)..].join("\n");
258+
before_str.push_str(after.first().copied().unwrap_or_default());
259+
260+
let after_str = after.get(1).copied().unwrap_or_default();
261+
262+
let indent = before.last().copied().unwrap_or_default().len();
263+
264+
Some(format!(
265+
"{before_str}\n
266+
{:width$}^
267+
{after_str}",
268+
"",
269+
width = indent
270+
))
257271
}
258272

259273
/// An indication of the context in which the error occurred.
@@ -331,7 +345,7 @@ impl fmt::Display for DbError {
331345
write!(fmt, "\nHINT: {}", hint)?;
332346
}
333347
if let Some(sql) = self.format_position() {
334-
write!(fmt, "\nSQL: {}", sql)?;
348+
write!(fmt, "\n{}", sql)?;
335349
}
336350
Ok(())
337351
}

0 commit comments

Comments
 (0)