Skip to content

Commit

Permalink
Upgrade error_stack
Browse files Browse the repository at this point in the history
  • Loading branch information
jssblck committed Nov 1, 2023
1 parent 6e43718 commit 30de2cb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
10 changes: 5 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use error_stack::fmt::HookContext;
use error_stack::{IntoReport, Report, Result, ResultExt};
use error_stack::{Report, Result, ResultExt};
use std::env;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -49,15 +49,15 @@ fn main() -> Result<(), Error> {

fn env_var(var: &str) -> Result<String, Error> {
env::var(var)
.into_report()
.map_err(Report::from)
.change_context_lazy(|| Error::CargoEnv(var.to_owned()))
.attach(Help("ensure that this program is running in a Cargo build"))
}

fn write_file<F: FnOnce() -> String>(root: &Path, name: &str, generator: F) -> Result<(), Error> {
let target = root.join(name);
fs::write(&target, generator())
.into_report()
.map_err(Report::from)
.change_context_lazy(|| Error::WriteFile(target))
.attach(Help(
"ensure that the build is not running on a read-only file system",
Expand Down Expand Up @@ -97,11 +97,11 @@ fn generate_cargo_build_vars() -> Result<(), Error> {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.into_report()
.map_err(Report::from)
.change_context(Error::CargoBuildVars)?;

let git_hash = String::from_utf8(output.stdout)
.into_report()
.map_err(Report::from)
.change_context(Error::CargoBuildVars)?;

println!("cargo:rustc-env=GIT_HASH={}", git_hash);
Expand Down
16 changes: 9 additions & 7 deletions src/ext/error_stack.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Extensions to `error_stack`.
use colored::Colorize;
use error_stack::{Context, IntoReport, Report, ResultExt};
use error_stack::{Context, Report, ResultExt};

use crate::doc;

Expand Down Expand Up @@ -40,7 +40,7 @@ pub trait ErrorHelper {
fn help_lazy<S: AsRef<str>, F: FnOnce() -> S>(self, helper: F) -> Self;
}

impl<T, C> ErrorHelper for error_stack::Result<T, C> {
impl<T, C: Context> ErrorHelper for error_stack::Result<T, C> {
fn help<S: AsRef<str>>(self, help_text: S) -> Self {
let help = help_literal();
let help_text = help_text.as_ref();
Expand Down Expand Up @@ -84,7 +84,7 @@ pub trait ErrorDocReference {
fn documentation_lazy<S: AsRef<str>, F: FnOnce() -> S>(self, url_generator: F) -> Self;
}

impl<T, C> ErrorDocReference for error_stack::Result<T, C> {
impl<T, C: Context> ErrorDocReference for error_stack::Result<T, C> {
fn documentation<S: AsRef<str>>(self, url: S) -> Self {
let doc = documentation_literal();
let doc_url = url.as_ref();
Expand Down Expand Up @@ -125,7 +125,7 @@ pub trait DescribeContext {
fn describe_lazy<S: AsRef<str>, F: FnOnce() -> S>(self, describer: F) -> Self;
}

impl<T, C> DescribeContext for error_stack::Result<T, C> {
impl<T, C: Context> DescribeContext for error_stack::Result<T, C> {
fn describe<S: AsRef<str>>(self, description: S) -> Self {
let context = describe_literal();
let description = description.as_ref();
Expand Down Expand Up @@ -160,7 +160,7 @@ pub trait FatalErrorReport {
fn request_support(self) -> Self;
}

impl<T, C> FatalErrorReport for error_stack::Result<T, C> {
impl<T, C: Context> FatalErrorReport for error_stack::Result<T, C> {
fn request_support(self) -> Self {
let support = support_literal();
let support_url = doc::link::fossa_support();
Expand Down Expand Up @@ -201,11 +201,13 @@ where

#[track_caller]
fn context(self, context: C) -> Result<T, Report<C>> {
self.into_report().change_context(context)
self.map_err(Report::from)
.map_err(|err| err.change_context(context))
}

#[track_caller]
fn context_lazy<F: Fn() -> C>(self, context: F) -> Result<T, Report<C>> {
self.into_report().change_context_lazy(context)
self.map_err(Report::from)
.map_err(|err| err.change_context(context()))
}
}
4 changes: 2 additions & 2 deletions src/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::{
path::{Path, PathBuf},
};

use error_stack::{report, IntoReport, Report, ResultExt};
use error_stack::{report, Report, ResultExt};
use tokio::{fs::File, task};

use crate::{
Expand Down Expand Up @@ -195,7 +195,7 @@ where
Report<E>: From<E>,
F: FnOnce() -> Result<T, E> + Send + 'static,
{
spawn_blocking(|| work().into_report()).await
spawn_blocking(|| work().map_err(Report::from)).await
}

/// Run the provided blocking closure in the background.
Expand Down
6 changes: 3 additions & 3 deletions src/ext/io/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{
path::{Path, PathBuf},
};

use error_stack::{IntoReport, Report, ResultExt};
use error_stack::{Report, ResultExt};
use itertools::Itertools;
use libflate::gzip;
use once_cell::sync::OnceCell;
Expand Down Expand Up @@ -208,7 +208,7 @@ pub fn validate_file(path: PathBuf) -> Result<PathBuf, Report<Error>> {
} else {
Error::NotRegularFile
.wrap_err()
.into_report()
.map_err(Report::from)
.attach_printable_lazy(|| format!("validate file: '{}'", path.display()))
}
}
Expand Down Expand Up @@ -243,7 +243,7 @@ pub fn home_dir() -> Result<&'static PathBuf, Report<Error>> {
static LAZY: OnceCell<PathBuf> = OnceCell::new();
LAZY.get_or_try_init(|| {
debug!("Performing uncached lookup of home directory");
dirs::home_dir().ok_or(Error::LocateUserHome).into_report()
dirs::home_dir().ok_or(Error::LocateUserHome).map_err(Report::from)
.describe("on macOS and Linux, this uses the $HOME environment variable or the system call 'getpwuid_r'")
.describe("on Windows, this uses the Windows API call 'SHGetKnownFolderPath'")
.describe("this is a very rare condition, and it's not likely that Broker will be able to resolve this issue")
Expand Down
14 changes: 5 additions & 9 deletions src/fossa_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use bytes::Bytes;
use cached::proc_macro::cached;
use error_stack::{bail, report, IntoReport};
use error_stack::{bail, report};
use error_stack::{Result, ResultExt};
use futures::future::try_join3;
use indoc::formatdoc;
Expand Down Expand Up @@ -546,8 +546,7 @@ async fn download_from_github(version: &str) -> Result<Cursor<Bytes>, Error> {
.get(&download_url)
.send()
.await
.into_report()
.change_context(Error::Download)
.context(Error::Download)
.help_lazy(|| formatdoc!{"
Try downloading FOSSA CLI from '{download_url}' to determine if this is an issue with the local network.
You also may be able to work around this issue by using the installation script for FOSSA CLI,
Expand All @@ -558,8 +557,7 @@ async fn download_from_github(version: &str) -> Result<Cursor<Bytes>, Error> {
let content = response
.bytes()
.await
.into_report()
.change_context(Error::Download)
.context(Error::Download)
.help_lazy(|| formatdoc!{"
Try downloading FOSSA CLI from '{download_url}' to determine if this is an issue with the local network.
You also may be able to work around this issue by using the installation script for FOSSA CLI,
Expand Down Expand Up @@ -604,8 +602,7 @@ where
.write(true)
.mode(0o770)
.open(final_path)
.into_report()
.change_context_lazy(|| Error::FinalCopy(final_path_string.clone()))?;
.context_lazy(|| Error::FinalCopy(final_path_string.clone()))?;

std::io::copy(&mut zip_file, &mut final_file)
.context_lazy(|| Error::FinalCopy(final_path_string.clone()))
Expand All @@ -623,8 +620,7 @@ where
.create(true)
.write(true)
.open(final_path)
.into_report()
.change_context_lazy(|| Error::FinalCopy(final_path_string.clone()))?;
.context_lazy(|| Error::FinalCopy(final_path_string.clone()))?;

std::io::copy(&mut zip_file, &mut final_file)
.context_lazy(|| Error::FinalCopy(final_path_string.clone()))
Expand Down

0 comments on commit 30de2cb

Please sign in to comment.