Skip to content

Commit 036595b

Browse files
committed
Less meaningless error messages
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
1 parent 828cb70 commit 036595b

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

crates/environments/src/environment_definition.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,33 @@ use anyhow::Context;
33
pub async fn load_environment(env_id: &str) -> anyhow::Result<TargetEnvironment> {
44
use futures_util::TryStreamExt;
55

6-
let (pkg_name, pkg_ver) = env_id.split_once('@').unwrap();
6+
let (pkg_name, pkg_ver) = env_id.split_once('@').with_context(|| format!("Failed to parse target environment {env_id} as package reference - is the target correct?"))?;
77

8-
let mut client = wasm_pkg_loader::Client::with_global_defaults()?;
8+
// TODO: this requires wkg configuration which shouldn't be on users:
9+
// is there a better way to handle it?
10+
let mut client = wasm_pkg_loader::Client::with_global_defaults()
11+
.context("Failed to create a package loader from your global settings")?;
912

10-
let package = pkg_name.to_owned().try_into().context("pkg ref parse")?;
11-
let version = wasm_pkg_loader::Version::parse(pkg_ver).context("pkg ver parse")?;
13+
let package = pkg_name
14+
.to_owned()
15+
.try_into()
16+
.with_context(|| format!("Failed to parse environment name {pkg_name} as package name"))?;
17+
let version = wasm_pkg_loader::Version::parse(pkg_ver).with_context(|| {
18+
format!("Failed to parse environment version {pkg_ver} as package version")
19+
})?;
1220

1321
let release = client
1422
.get_release(&package, &version)
1523
.await
16-
.context("get release")?;
24+
.with_context(|| format!("Failed to get {env_id} release from registry"))?;
1725
let stm = client
1826
.stream_content(&package, &release)
1927
.await
20-
.context("stream content")?;
28+
.with_context(|| format!("Failed to get {env_id} package from registry"))?;
2129
let bytes = stm
2230
.try_collect::<bytes::BytesMut>()
2331
.await
24-
.context("collect stm")?
32+
.with_context(|| format!("Failed to get {env_id} package data from registry"))?
2533
.to_vec();
2634

2735
TargetEnvironment::new(env_id.to_owned(), bytes)
@@ -37,13 +45,16 @@ pub struct TargetEnvironment {
3745

3846
impl TargetEnvironment {
3947
fn new(name: String, bytes: Vec<u8>) -> anyhow::Result<Self> {
40-
let decoded = wit_component::decode(&bytes).context("decode wasm")?;
48+
let decoded = wit_component::decode(&bytes)
49+
.with_context(|| format!("Failed to decode package for environment {name}"))?;
4150
let package_id = decoded.package();
4251
let package = decoded
4352
.resolve()
4453
.packages
4554
.get(package_id)
46-
.context("should had a package")?
55+
.with_context(|| {
56+
format!("The {name} environment is invalid (no package for decoded package ID)")
57+
})?
4758
.clone();
4859

4960
Ok(Self {

crates/environments/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::anyhow;
1+
use anyhow::{anyhow, Context};
22

33
mod environment_definition;
44
mod loader;
@@ -36,7 +36,9 @@ async fn validate_application_against_environments(
3636
load_and_resolve_all(app, ts, resolution_context)
3737
.map(|css| css.map(|css| (ty.to_owned(), css)))
3838
});
39-
let components_by_trigger_type = join_all_result(components_by_trigger_type_futs).await?;
39+
let components_by_trigger_type = join_all_result(components_by_trigger_type_futs)
40+
.await
41+
.context("Failed to prepare components for target environment checking")?;
4042

4143
for (trigger_type, component) in components_by_trigger_type {
4244
for component in &component {
@@ -126,7 +128,8 @@ async fn validate_wasm_against_world(
126128
"#
127129
);
128130

129-
let doc = wac_parser::Document::parse(&wac_text)?;
131+
let doc = wac_parser::Document::parse(&wac_text)
132+
.context("Internal error constructing WAC document for target checking")?;
130133

131134
// TODO: if we end up needing the registry, we need to do this dance
132135
// for things we are providing separately, or the registry will try to

crates/environments/src/loader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::Path;
22

3-
use anyhow::anyhow;
3+
use anyhow::{anyhow, Context};
44
use spin_common::ui::quoted_path;
55

66
pub(crate) struct ComponentToValidate<'a> {
@@ -73,7 +73,7 @@ async fn load_and_resolve_one<'a>(
7373

7474
let loader = ComponentSourceLoader::new(resolution_context.wasm_loader());
7575

76-
let wasm = spin_compose::compose(&loader, &component).await?;
76+
let wasm = spin_compose::compose(&loader, &component).await.with_context(|| format!("Spin needed to compose dependencies for {id} as part of target checking, but composition failed"))?;
7777

7878
Ok(ComponentToValidate {
7979
id,

0 commit comments

Comments
 (0)