Skip to content

Commit 2bb3eb8

Browse files
authored
Merge pull request #26 from fermyon/improve-service-errors
Improve errors related to services
2 parents e846bce + bd38f9e commit 2bb3eb8

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

crates/test-environment/src/services.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn get_builtin_service_definitions(
126126
return Ok(Vec::new());
127127
}
128128

129-
std::fs::read_dir(service_definitions_path)
129+
let result = std::fs::read_dir(service_definitions_path)
130130
.with_context(|| {
131131
format!(
132132
"no service definitions found at '{}'",
@@ -147,7 +147,7 @@ fn get_builtin_service_definitions(
147147
Ok((file_name.to_owned(), file_extension.to_owned()))
148148
})
149149
.filter(|r| !matches!(r, Ok((_, extension)) if extension == "lock"))
150-
.filter(move |r| match r {
150+
.filter(|r| match r {
151151
Ok((service, _)) => builtins.remove(service.as_str()),
152152
_ => false,
153153
})
@@ -157,18 +157,22 @@ fn get_builtin_service_definitions(
157157
name: name.clone(),
158158
kind: match extension.as_str() {
159159
"py" => ServiceKind::Python {
160-
script: service_definitions_path.join(format!("{}.py", name)),
160+
script: service_definitions_path.join(format!("{name}.py")),
161161
},
162162
"Dockerfile" => ServiceKind::Docker {
163163
image: docker::DockerImage::FromDockerfile(
164-
service_definitions_path.join(format!("{}.Dockerfile", name)),
164+
service_definitions_path.join(format!("{name}.Dockerfile")),
165165
),
166166
},
167-
_ => bail!("unsupported service definition extension '{}'", extension),
167+
_ => bail!("unsupported service definition extension '{extension}'"),
168168
},
169169
})
170170
})
171-
.collect()
171+
.collect();
172+
if !builtins.is_empty() {
173+
bail!("no service definitions found for: {builtins:?}",);
174+
}
175+
result
172176
}
173177

174178
/// A service definition.

crates/test-environment/src/services/docker.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ fn build_image(dockerfile_path: &Path, image_name: &String) -> anyhow::Result<()
164164
.stdout(Stdio::null())
165165
.stderr(Stdio::piped())
166166
.output()
167-
.context("service failed to spawn")?;
167+
.with_context(|| {
168+
format!(
169+
"docker build failed to spawn for Dockerfile '{}'",
170+
dockerfile_path.display()
171+
)
172+
})?;
168173

169174
if !output.status.success() {
170175
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("<non-utf8>");
@@ -196,7 +201,7 @@ fn run_container(image_name: &str) -> anyhow::Result<Container> {
196201
.arg("--health-start-period=1s")
197202
.arg(image_name)
198203
.output()
199-
.context("service failed to spawn")?;
204+
.with_context(|| format!("docker run failed to spawn for image '{image_name}'"))?;
200205
if !output.status.success() {
201206
bail!("failed to run docker image");
202207
}

crates/test-environment/src/services/python.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl PythonService {
3434
.arg(script_path.display().to_string())
3535
.stdout(Stdio::piped())
3636
.spawn()
37-
.context("service failed to spawn")?;
37+
.with_context(|| format!("python failed to spawn for '{}'", script_path.display()))?;
3838
std::thread::sleep(std::time::Duration::from_millis(1000));
3939
Ok(Self {
4040
stdout: OutputStream::new(

0 commit comments

Comments
 (0)