Skip to content

Commit 132a79a

Browse files
authored
Don't panic on json-body failure (#1244)
Currently CLI clients will panic if the `json-body` file cannot be read or cannot be deserialized. Update these to return errors and add additional context.
1 parent 9d0d23f commit 132a79a

File tree

8 files changed

+298
-160
lines changed

8 files changed

+298
-160
lines changed

progenitor-impl/src/cli.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl Generator {
8888

8989
let code = quote! {
9090
use #crate_path::*;
91+
use anyhow::Context as _;
9192

9293
pub struct Cli<T: CliConfig> {
9394
client: Client,
@@ -510,12 +511,12 @@ impl Generator {
510511
if let Some(value) =
511512
matches.get_one::<std::path::PathBuf>("json-body")
512513
{
513-
let body_txt = std::fs::read_to_string(value).unwrap();
514+
let body_txt = std::fs::read_to_string(value).with_context(|| format!("failed to read {}", value.display()))?;
514515
let body_value =
515516
serde_json::from_str::<#body_type_ident>(
516517
&body_txt,
517518
)
518-
.unwrap();
519+
.with_context(|| format!("failed to parse {}", value.display()))?;
519520
request = request.body(body_value);
520521
}
521522
}

progenitor-impl/tests/output/src/buildomat_cli.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::buildomat_builder::*;
2+
use anyhow::Context as _;
23
pub struct Cli<T: CliConfig> {
34
client: Client,
45
config: T,
@@ -465,8 +466,10 @@ impl<T: CliConfig> Cli<T> {
465466
}
466467

467468
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
468-
let body_txt = std::fs::read_to_string(value).unwrap();
469-
let body_value = serde_json::from_str::<types::TaskSubmit>(&body_txt).unwrap();
469+
let body_txt = std::fs::read_to_string(value)
470+
.with_context(|| format!("failed to read {}", value.display()))?;
471+
let body_value = serde_json::from_str::<types::TaskSubmit>(&body_txt)
472+
.with_context(|| format!("failed to parse {}", value.display()))?;
470473
request = request.body(body_value);
471474
}
472475

@@ -569,8 +572,10 @@ impl<T: CliConfig> Cli<T> {
569572
}
570573

571574
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
572-
let body_txt = std::fs::read_to_string(value).unwrap();
573-
let body_value = serde_json::from_str::<types::UserCreate>(&body_txt).unwrap();
575+
let body_txt = std::fs::read_to_string(value)
576+
.with_context(|| format!("failed to read {}", value.display()))?;
577+
let body_value = serde_json::from_str::<types::UserCreate>(&body_txt)
578+
.with_context(|| format!("failed to parse {}", value.display()))?;
574579
request = request.body(body_value);
575580
}
576581

@@ -637,8 +642,10 @@ impl<T: CliConfig> Cli<T> {
637642
}
638643

639644
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
640-
let body_txt = std::fs::read_to_string(value).unwrap();
641-
let body_value = serde_json::from_str::<types::WorkerBootstrap>(&body_txt).unwrap();
645+
let body_txt = std::fs::read_to_string(value)
646+
.with_context(|| format!("failed to read {}", value.display()))?;
647+
let body_value = serde_json::from_str::<types::WorkerBootstrap>(&body_txt)
648+
.with_context(|| format!("failed to parse {}", value.display()))?;
642649
request = request.body(body_value);
643650
}
644651

@@ -695,8 +702,10 @@ impl<T: CliConfig> Cli<T> {
695702
}
696703

697704
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
698-
let body_txt = std::fs::read_to_string(value).unwrap();
699-
let body_value = serde_json::from_str::<types::WorkerAppendTask>(&body_txt).unwrap();
705+
let body_txt = std::fs::read_to_string(value)
706+
.with_context(|| format!("failed to read {}", value.display()))?;
707+
let body_value = serde_json::from_str::<types::WorkerAppendTask>(&body_txt)
708+
.with_context(|| format!("failed to parse {}", value.display()))?;
700709
request = request.body(body_value);
701710
}
702711

@@ -753,8 +762,10 @@ impl<T: CliConfig> Cli<T> {
753762
}
754763

755764
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
756-
let body_txt = std::fs::read_to_string(value).unwrap();
757-
let body_value = serde_json::from_str::<types::WorkerCompleteTask>(&body_txt).unwrap();
765+
let body_txt = std::fs::read_to_string(value)
766+
.with_context(|| format!("failed to read {}", value.display()))?;
767+
let body_value = serde_json::from_str::<types::WorkerCompleteTask>(&body_txt)
768+
.with_context(|| format!("failed to parse {}", value.display()))?;
758769
request = request.body(body_value);
759770
}
760771

@@ -791,8 +802,10 @@ impl<T: CliConfig> Cli<T> {
791802
}
792803

793804
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
794-
let body_txt = std::fs::read_to_string(value).unwrap();
795-
let body_value = serde_json::from_str::<types::WorkerAddOutput>(&body_txt).unwrap();
805+
let body_txt = std::fs::read_to_string(value)
806+
.with_context(|| format!("failed to read {}", value.display()))?;
807+
let body_value = serde_json::from_str::<types::WorkerAddOutput>(&body_txt)
808+
.with_context(|| format!("failed to parse {}", value.display()))?;
796809
request = request.body(body_value);
797810
}
798811

progenitor-impl/tests/output/src/cli_gen_cli.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::cli_gen_builder::*;
2+
use anyhow::Context as _;
23
pub struct Cli<T: CliConfig> {
34
client: Client,
45
config: T,
@@ -56,8 +57,10 @@ impl<T: CliConfig> Cli<T> {
5657
}
5758

5859
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
59-
let body_txt = std::fs::read_to_string(value).unwrap();
60-
let body_value = serde_json::from_str::<types::UnoBody>(&body_txt).unwrap();
60+
let body_txt = std::fs::read_to_string(value)
61+
.with_context(|| format!("failed to read {}", value.display()))?;
62+
let body_value = serde_json::from_str::<types::UnoBody>(&body_txt)
63+
.with_context(|| format!("failed to parse {}", value.display()))?;
6164
request = request.body(body_value);
6265
}
6366

progenitor-impl/tests/output/src/keeper_cli.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::keeper_builder::*;
2+
use anyhow::Context as _;
23
pub struct Cli<T: CliConfig> {
34
client: Client,
45
config: T,
@@ -216,8 +217,10 @@ impl<T: CliConfig> Cli<T> {
216217
}
217218

218219
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
219-
let body_txt = std::fs::read_to_string(value).unwrap();
220-
let body_value = serde_json::from_str::<types::EnrolBody>(&body_txt).unwrap();
220+
let body_txt = std::fs::read_to_string(value)
221+
.with_context(|| format!("failed to read {}", value.display()))?;
222+
let body_value = serde_json::from_str::<types::EnrolBody>(&body_txt)
223+
.with_context(|| format!("failed to parse {}", value.display()))?;
221224
request = request.body(body_value);
222225
}
223226

@@ -296,8 +299,10 @@ impl<T: CliConfig> Cli<T> {
296299
}
297300

298301
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
299-
let body_txt = std::fs::read_to_string(value).unwrap();
300-
let body_value = serde_json::from_str::<types::ReportFinishBody>(&body_txt).unwrap();
302+
let body_txt = std::fs::read_to_string(value)
303+
.with_context(|| format!("failed to read {}", value.display()))?;
304+
let body_value = serde_json::from_str::<types::ReportFinishBody>(&body_txt)
305+
.with_context(|| format!("failed to parse {}", value.display()))?;
301306
request = request.body(body_value);
302307
}
303308

@@ -322,8 +327,10 @@ impl<T: CliConfig> Cli<T> {
322327
}
323328

324329
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
325-
let body_txt = std::fs::read_to_string(value).unwrap();
326-
let body_value = serde_json::from_str::<types::ReportOutputBody>(&body_txt).unwrap();
330+
let body_txt = std::fs::read_to_string(value)
331+
.with_context(|| format!("failed to read {}", value.display()))?;
332+
let body_value = serde_json::from_str::<types::ReportOutputBody>(&body_txt)
333+
.with_context(|| format!("failed to parse {}", value.display()))?;
327334
request = request.body(body_value);
328335
}
329336

@@ -358,8 +365,10 @@ impl<T: CliConfig> Cli<T> {
358365
}
359366

360367
if let Some(value) = matches.get_one::<std::path::PathBuf>("json-body") {
361-
let body_txt = std::fs::read_to_string(value).unwrap();
362-
let body_value = serde_json::from_str::<types::ReportStartBody>(&body_txt).unwrap();
368+
let body_txt = std::fs::read_to_string(value)
369+
.with_context(|| format!("failed to read {}", value.display()))?;
370+
let body_value = serde_json::from_str::<types::ReportStartBody>(&body_txt)
371+
.with_context(|| format!("failed to parse {}", value.display()))?;
363372
request = request.body(body_value);
364373
}
365374

0 commit comments

Comments
 (0)