Skip to content

Commit

Permalink
Use inspect_err for tracing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Feb 8, 2024
1 parent 2907c18 commit dd03af5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Raised Minimum Supported Rust Version (MSRV) to `1.76`. ([#000](https://github.com/heroku/libcnb.rs/pull/000))
- `libcnb`:
- Changed `Layer` interface from `&self` to `&mut self`. ([#669](https://github.com/heroku/libcnb.rs/pull/669))

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ members = [

[workspace.package]
version = "0.17.0"
rust-version = "1.75"
rust-version = "1.76"
edition = "2021"
license = "BSD-3-Clause"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[docs.rs]: https://docs.rs/libcnb/latest/libcnb/
[Latest Version]: https://img.shields.io/crates/v/libcnb.svg
[crates.io]: https://crates.io/crates/libcnb
[MSRV]: https://img.shields.io/badge/MSRV-rustc_1.74+-lightgray.svg
[MSRV]: https://img.shields.io/badge/MSRV-rustc_1.76+-lightgray.svg
[install-rust]: https://www.rust-lang.org/tools/install

`libcnb.rs` is a framework for writing [Cloud Native Buildpacks](https://buildpacks.io) in Rust.
Expand Down
91 changes: 31 additions & 60 deletions libcnb/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,11 @@ pub fn libcnb_runtime_detect<B: Buildpack>(
let stack_id: StackId = env::var("CNB_STACK_ID")
.map_err(Error::CannotDetermineStackId)
.and_then(|stack_id_string| stack_id_string.parse().map_err(Error::StackIdError))
.map_err(|err| {
trace_error(&err);
err
})?;
.inspect_err(|err| trace_error(err))?;

let platform = B::Platform::from_path(&args.platform_dir_path).map_err(|inner_err| {
let err = Error::CannotCreatePlatformFromPath(inner_err);
trace_error(&err);
err
})?;
let platform = B::Platform::from_path(&args.platform_dir_path)
.map_err(Error::CannotCreatePlatformFromPath)
.inspect_err(|err| trace_error(err))?;

let build_plan_path = args.build_plan_path;

Expand All @@ -164,10 +159,9 @@ pub fn libcnb_runtime_detect<B: Buildpack>(
buildpack_descriptor,
};

let detect_result = buildpack.detect(detect_context).map_err(|err| {
trace_error(&err);
err
})?;
let detect_result = buildpack
.detect(detect_context)
.inspect_err(|err| trace_error(err))?;

match detect_result.0 {
InnerDetectResult::Fail => {
Expand All @@ -177,11 +171,9 @@ pub fn libcnb_runtime_detect<B: Buildpack>(
}
InnerDetectResult::Pass { build_plan } => {
if let Some(build_plan) = build_plan {
write_toml_file(&build_plan, build_plan_path).map_err(|inner_err| {
let err = Error::CannotWriteBuildPlan(inner_err);
trace_error(&err);
err
})?;
write_toml_file(&build_plan, build_plan_path)
.map_err(Error::CannotWriteBuildPlan)
.inspect_err(|err| trace_error(err))?;
}
#[cfg(feature = "trace")]
trace.add_event("detect-passed");
Expand Down Expand Up @@ -222,32 +214,22 @@ pub fn libcnb_runtime_build<B: Buildpack>(
let stack_id: StackId = env::var("CNB_STACK_ID")
.map_err(Error::CannotDetermineStackId)
.and_then(|stack_id_string| stack_id_string.parse().map_err(Error::StackIdError))
.map_err(|err| {
trace_error(&err);
err
})?;

let platform = Platform::from_path(&args.platform_dir_path).map_err(|inner_err| {
let err = Error::CannotCreatePlatformFromPath(inner_err);
trace_error(&err);
err
})?;

let buildpack_plan = read_toml_file(&args.buildpack_plan_path).map_err(|inner_err| {
let err = Error::CannotReadBuildpackPlan(inner_err);
trace_error(&err);
err
})?;
.inspect_err(|err| trace_error(err))?;

let platform = Platform::from_path(&args.platform_dir_path)
.map_err(Error::CannotCreatePlatformFromPath)
.inspect_err(|err| trace_error(err))?;

let buildpack_plan = read_toml_file(&args.buildpack_plan_path)
.map_err(Error::CannotReadBuildpackPlan)
.inspect_err(|err| trace_error(err))?;

let store = match read_toml_file::<Store>(layers_dir.join("store.toml")) {
Err(TomlFileError::IoError(io_error)) if is_not_found_error_kind(&io_error) => Ok(None),
other => other.map(Some),
}
.map_err(Error::CannotReadStore)
.map_err(|err| {
trace_error(&err);
err
})?;
.inspect_err(|err| trace_error(err))?;

let build_context = BuildContext {
layers_dir: layers_dir.clone(),
Expand All @@ -260,10 +242,9 @@ pub fn libcnb_runtime_build<B: Buildpack>(
store,
};

let build_result = buildpack.build(build_context).map_err(|err| {
trace_error(&err);
err
})?;
let build_result = buildpack
.build(build_context)
.inspect_err(|err| trace_error(err))?;

match build_result.0 {
InnerBuildResult::Pass {
Expand All @@ -273,19 +254,15 @@ pub fn libcnb_runtime_build<B: Buildpack>(
launch_sboms,
} => {
if let Some(launch) = launch {
write_toml_file(&launch, layers_dir.join("launch.toml")).map_err(|inner_err| {
let err = Error::CannotWriteLaunch(inner_err);
trace_error(&err);
err
})?;
write_toml_file(&launch, layers_dir.join("launch.toml"))
.map_err(Error::CannotWriteLaunch)
.inspect_err(|err| trace_error(err))?;
};

if let Some(store) = store {
write_toml_file(&store, layers_dir.join("store.toml")).map_err(|inner_err| {
let err = Error::CannotWriteStore(inner_err);
trace_error(&err);
err
})?;
write_toml_file(&store, layers_dir.join("store.toml"))
.map_err(Error::CannotWriteStore)
.inspect_err(|err| trace_error(err))?;
};

for build_sbom in build_sboms {
Expand All @@ -294,10 +271,7 @@ pub fn libcnb_runtime_build<B: Buildpack>(
&build_sbom.data,
)
.map_err(Error::CannotWriteBuildSbom)
.map_err(|err| {
trace_error(&err);
err
})?;
.inspect_err(|err| trace_error(err))?;
}

for launch_sbom in launch_sboms {
Expand All @@ -306,10 +280,7 @@ pub fn libcnb_runtime_build<B: Buildpack>(
&launch_sbom.data,
)
.map_err(Error::CannotWriteLaunchSbom)
.map_err(|err| {
trace_error(&err);
err
})?;
.inspect_err(|err| trace_error(err))?;
}

#[cfg(feature = "trace")]
Expand Down

0 comments on commit dd03af5

Please sign in to comment.