Function libherokubuildpack::error::on_error
source · pub fn on_error<F, E>(f: F, error: Error<E>)
Expand description
Handles a given libcnb::Error
in a consistent style.
This function is intended to be used inside libcnb::Buildpack::on_error
.
It outputs generic libcnb errors in a consistent style using the logging functions from this crate. Buildpack specific errors are handled by the passed custom handler.
§Example:
use libcnb::build::{BuildContext, BuildResult};
use libcnb::Buildpack;
use libcnb::detect::{DetectContext, DetectResult};
use libcnb::generic::{GenericMetadata, GenericPlatform};
use libherokubuildpack::log::log_error;
use libherokubuildpack::error::on_error;
#[derive(Debug)]
enum FooBuildpackError {
CannotExecuteFooBuildTool(std::io::Error),
InvalidFooDescriptorToml
}
fn on_foo_buildpack_error(e: FooBuildpackError) {
match e {
FooBuildpackError::InvalidFooDescriptorToml => {
log_error("Invalid foo.toml", "Your app's foo.toml is invalid!");
}
FooBuildpackError::CannotExecuteFooBuildTool(inner) => {
log_error("Couldn't execute foo build tool", format!("Cause: {}", &inner));
}
}
}
struct FooBuildpack;
impl Buildpack for FooBuildpack {
type Platform = GenericPlatform;
type Metadata = GenericMetadata;
type Error = FooBuildpackError;
// Omitted detect and build implementations...
fn on_error(&self, error: libcnb::Error<Self::Error>) {
on_error(on_foo_buildpack_error, error)
}
}