Skip to content

Commit 88fef26

Browse files
committed
better error messages for invalid partition subtype
1 parent 68d86ef commit 88fef26

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

espflash/src/error.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ pub enum PartitionTableError {
286286
#[error("Malformed partition table")]
287287
#[diagnostic(
288288
code(espflash::partition_table::mallformed),
289-
help("See the espressif documentation for information on the partition table format:
289+
help("{}See the espressif documentation for information on the partition table format:
290290
291-
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/partition-tables.html#creating-custom-tables")
291+
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/partition-tables.html#creating-custom-tables", self.help)
292292
)]
293293
pub struct CSVError {
294294
#[source_code]
@@ -298,6 +298,7 @@ pub struct CSVError {
298298
hint: String,
299299
#[source]
300300
error: csv::Error,
301+
help: String,
301302
}
302303

303304
impl CSVError {
@@ -307,7 +308,7 @@ impl CSVError {
307308
csv::ErrorKind::UnequalLengths { pos: Some(pos), .. } => pos.line(),
308309
_ => 0,
309310
};
310-
let hint = match error.kind() {
311+
let mut hint = match error.kind() {
311312
csv::ErrorKind::Deserialize { err, .. } => err.to_string(),
312313
csv::ErrorKind::UnequalLengths {
313314
expected_len, len, ..
@@ -317,6 +318,20 @@ impl CSVError {
317318
),
318319
_ => String::new(),
319320
};
321+
let mut help = String::new();
322+
323+
// string matching is fragile but afaik there is no better way in this case
324+
// and if it does break the error is still not bad
325+
if hint == "data did not match any variant of untagged enum SubType" {
326+
hint = "Unknown sub-type".into();
327+
help = format!(
328+
"the following sub-types are supported:
329+
{} for data partitions
330+
{} for app partitions\n\n",
331+
Type::Data.subtype_hint(),
332+
Type::App.subtype_hint()
333+
)
334+
}
320335

321336
let err_span = line_to_span(&source, err_line as usize);
322337

@@ -325,6 +340,7 @@ impl CSVError {
325340
err_span,
326341
hint,
327342
error,
343+
help,
328344
}
329345
}
330346
}

0 commit comments

Comments
 (0)