Skip to content

Commit 6c8289a

Browse files
authored
add error context to autorust (#1497)
1 parent da2e429 commit 6c8289a

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

services/autorust/azure-autorust/src/main.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use autorust_codegen::{
66
crates::{list_crate_names, list_dirs},
77
gen, get_mgmt_readmes, get_svc_readmes,
88
jinja::{CheckAllServicesYml, PublishSdksYml, PublishServicesYml, WorkspaceCargoToml},
9-
ErrorKind, Result, ResultExt, RunConfig, SpecReadme,
9+
Error, ErrorKind, Result, ResultExt, RunConfig, SpecReadme,
1010
};
1111
use clap::Parser;
1212
use rayon::prelude::*;
@@ -68,14 +68,37 @@ fn gen_crates(only_packages: &[&str]) -> Result<()> {
6868
let mgmt = get_mgmt_readmes()?.into_iter().map(|x| ("mgmt".to_string(), x));
6969
let crate_iters = svc.chain(mgmt).collect::<Vec<_>>();
7070

71-
let results: Result<Vec<_>> = crate_iters
71+
let results = crate_iters
7272
.par_iter()
7373
.map(|(crate_type, spec)| gen_crate(only_packages, crate_type, spec))
7474
.collect::<Vec<_>>()
7575
.into_iter()
76-
.collect();
76+
.collect::<Vec<_>>();
7777

78-
(results?).into_iter().flatten().for_each(|(package_name, tags)| {
78+
let mut errors = vec![];
79+
let mut completed = vec![];
80+
81+
for result in results {
82+
match result {
83+
Ok(result) => {
84+
if let Some(result) = result {
85+
completed.push(result);
86+
}
87+
}
88+
Err(err) => {
89+
errors.push(err);
90+
}
91+
}
92+
}
93+
94+
if !errors.is_empty() {
95+
for error in &errors {
96+
eprintln!("{error:#?}");
97+
}
98+
return Err(Error::new(ErrorKind::CodeGen, "Failed to generate some crates"));
99+
}
100+
101+
for (package_name, tags) in completed {
79102
println!("{package_name}");
80103
if tags.is_empty() {
81104
println!(" No tags");
@@ -84,7 +107,7 @@ fn gen_crates(only_packages: &[&str]) -> Result<()> {
84107
println!("- {tag}");
85108
}
86109
}
87-
});
110+
}
88111

89112
Ok(())
90113
}

services/autorust/codegen/src/gen.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
autorust_toml, cargo_toml, io, lib_rs,
33
readme_md::{self, ReadmeMd},
4-
run, CrateConfig, Error, Result, RunConfig, SpecReadme,
4+
run, CrateConfig, Error, ErrorKind, Result, ResultExt, RunConfig, SpecReadme,
55
};
66
use std::{collections::HashMap, fs};
77

@@ -54,21 +54,26 @@ pub fn gen_crate(package_name: &str, spec: &SpecReadme, run_config: &RunConfig,
5454
let mut api_versions = HashMap::new();
5555
let mut has_xml = false;
5656
for tag in tags {
57-
generated_tags.push(tag.name().to_owned());
57+
let name = tag.name();
58+
generated_tags.push(name.to_owned());
5859
let output_folder = io::join(&src_folder, tag.rust_mod_name())?;
5960
let input_files: Result<Vec<_>> = tag
6061
.input_files()
6162
.iter()
6263
.map(|input_file| io::join(spec.readme(), input_file).map_err(Error::from))
6364
.collect();
64-
let input_files = input_files?;
65+
let input_files = input_files.with_context(ErrorKind::CodeGen, || format!("collecting input files for tag {name}"))?;
66+
6567
let crate_config = &CrateConfig {
6668
run_config,
6769
output_folder,
6870
input_files,
6971
};
70-
let cg = run(crate_config, &package_config)?;
71-
let operations = cg.spec.operations()?;
72+
let cg = run(crate_config, &package_config).with_context(ErrorKind::CodeGen, || format!("gen_crate run for tag {name}"))?;
73+
let operations = cg
74+
.spec
75+
.operations()
76+
.with_context(ErrorKind::CodeGen, || format!("gen_crate operations for tag {name}"))?;
7277
operation_totals.insert(tag.name(), operations.len());
7378
let mut versions = cg.spec.api_versions();
7479
versions.sort_unstable();
@@ -84,8 +89,8 @@ pub fn gen_crate(package_name: &str, spec: &SpecReadme, run_config: &RunConfig,
8489
};
8590
let default_tag = cargo_toml::get_default_tag(tags, default_tag_name);
8691

87-
cargo_toml::create(package_name, tags, default_tag, has_xml, &cargo_toml_path)?;
88-
lib_rs::create(tags, lib_rs_path, false)?;
92+
cargo_toml::create(package_name, tags, default_tag, has_xml, &cargo_toml_path).context(ErrorKind::CodeGen, "cargo_toml::create")?;
93+
lib_rs::create(tags, lib_rs_path, false).context(ErrorKind::CodeGen, "lib_rs::create")?;
8994
let readme = ReadmeMd {
9095
package_name,
9196
readme_url: readme_md::url(spec.readme().as_str()),
@@ -95,7 +100,7 @@ pub fn gen_crate(package_name: &str, spec: &SpecReadme, run_config: &RunConfig,
95100
api_version_totals,
96101
api_versions,
97102
};
98-
readme.create(&readme_path)?;
103+
readme.create(&readme_path).context(ErrorKind::CodeGen, "readme::create")?;
99104

100105
Ok(generated_tags)
101106
}

0 commit comments

Comments
 (0)