Skip to content

Commit 9320b16

Browse files
committed
better errors for invalid character encoding in SQL files
1 parent 60ab81c commit 9320b16

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
select username as title, avatar_blob as image_url
2121
from users;
2222
```
23+
- When a sql file is saved with the wrong character encoding (not UTF8), SQLPage now displays a helpful error messages that points to exactly where in the file the problem is.
2324

2425
## v0.36.1
2526
- Fix regression introduced in v0.36.0: PostgreSQL money values showed as 0.0

src/filesystem.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use chrono::{DateTime, Utc};
66
use sqlx::any::{AnyKind, AnyStatement, AnyTypeInfo};
77
use sqlx::postgres::types::PgTimeTz;
88
use sqlx::{Postgres, Statement, Type};
9+
use std::fmt::Write;
910
use std::io::ErrorKind;
1011
use std::path::{Component, Path, PathBuf};
1112

@@ -64,10 +65,22 @@ impl FileSystem {
6465
priviledged: bool,
6566
) -> anyhow::Result<String> {
6667
let bytes = self.read_file(app_state, path, priviledged).await?;
67-
String::from_utf8(bytes).with_context(|| {
68-
format!(
69-
"The file at {} contains invalid UTF8 characters",
70-
path.display()
68+
String::from_utf8(bytes).map_err(|utf8_err| {
69+
let invalid_idx = utf8_err.utf8_error().valid_up_to();
70+
let bytes = utf8_err.into_bytes();
71+
let valid_prefix = String::from_utf8_lossy(&bytes[..invalid_idx]);
72+
let line_num = valid_prefix.lines().count();
73+
let mut bad_seq = valid_prefix.lines().last().unwrap_or_default().to_string();
74+
let bad_char_idx = bad_seq.len() + 1;
75+
for b in bytes[invalid_idx..].iter().take(8) {
76+
write!(&mut bad_seq, "\\x{b:02X}").unwrap();
77+
}
78+
79+
let display_path = path.display();
80+
anyhow::format_err!(
81+
"SQLPage expects all sql files to be encoded in UTF-8. \
82+
The file \"{display_path}\" contains the following invalid UTF-8 byte sequence around line {line_num} character {bad_char_idx}: \"{bad_seq}\". \
83+
Please convert the file to UTF-8.",
7184
)
7285
})
7386
}

0 commit comments

Comments
 (0)