Skip to content

Commit

Permalink
Merge pull request #26 from fermyon/improve-service-errors
Browse files Browse the repository at this point in the history
Improve errors related to services
  • Loading branch information
rylev authored Jul 19, 2024
2 parents e846bce + bd38f9e commit 2bb3eb8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
16 changes: 10 additions & 6 deletions crates/test-environment/src/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn get_builtin_service_definitions(
return Ok(Vec::new());
}

std::fs::read_dir(service_definitions_path)
let result = std::fs::read_dir(service_definitions_path)
.with_context(|| {
format!(
"no service definitions found at '{}'",
Expand All @@ -147,7 +147,7 @@ fn get_builtin_service_definitions(
Ok((file_name.to_owned(), file_extension.to_owned()))
})
.filter(|r| !matches!(r, Ok((_, extension)) if extension == "lock"))
.filter(move |r| match r {
.filter(|r| match r {
Ok((service, _)) => builtins.remove(service.as_str()),
_ => false,
})
Expand All @@ -157,18 +157,22 @@ fn get_builtin_service_definitions(
name: name.clone(),
kind: match extension.as_str() {
"py" => ServiceKind::Python {
script: service_definitions_path.join(format!("{}.py", name)),
script: service_definitions_path.join(format!("{name}.py")),
},
"Dockerfile" => ServiceKind::Docker {
image: docker::DockerImage::FromDockerfile(
service_definitions_path.join(format!("{}.Dockerfile", name)),
service_definitions_path.join(format!("{name}.Dockerfile")),
),
},
_ => bail!("unsupported service definition extension '{}'", extension),
_ => bail!("unsupported service definition extension '{extension}'"),
},
})
})
.collect()
.collect();
if !builtins.is_empty() {
bail!("no service definitions found for: {builtins:?}",);
}
result
}

/// A service definition.
Expand Down
9 changes: 7 additions & 2 deletions crates/test-environment/src/services/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ fn build_image(dockerfile_path: &Path, image_name: &String) -> anyhow::Result<()
.stdout(Stdio::null())
.stderr(Stdio::piped())
.output()
.context("service failed to spawn")?;
.with_context(|| {
format!(
"docker build failed to spawn for Dockerfile '{}'",
dockerfile_path.display()
)
})?;

if !output.status.success() {
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("<non-utf8>");
Expand Down Expand Up @@ -196,7 +201,7 @@ fn run_container(image_name: &str) -> anyhow::Result<Container> {
.arg("--health-start-period=1s")
.arg(image_name)
.output()
.context("service failed to spawn")?;
.with_context(|| format!("docker run failed to spawn for image '{image_name}'"))?;
if !output.status.success() {
bail!("failed to run docker image");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/test-environment/src/services/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl PythonService {
.arg(script_path.display().to_string())
.stdout(Stdio::piped())
.spawn()
.context("service failed to spawn")?;
.with_context(|| format!("python failed to spawn for '{}'", script_path.display()))?;
std::thread::sleep(std::time::Duration::from_millis(1000));
Ok(Self {
stdout: OutputStream::new(
Expand Down

0 comments on commit 2bb3eb8

Please sign in to comment.