diff --git a/Cargo.lock b/Cargo.lock index 83681a9f4b..90ec21ac79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1211,11 +1211,11 @@ dependencies = [ name = "deno_lockfile" version = "0.8.0" dependencies = [ - "anyhow", "ring", "serde", "serde_json", "test_util", + "thiserror", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 62051cf239..739f9421dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -122,6 +122,7 @@ sha2 = { version = "0.10.6", features = ["oid"] } smallvec = "1.8" socket2 = "0.4.7" tar = "=0.4.38" +thiserror = "=1.0.38" tokio = { version = "=1.25.0", features = ["full"] } tokio-rustls = "0.23.3" tokio-tungstenite = "0.16.1" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 10ebc89ea9..e99596bcf1 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -101,7 +101,7 @@ shell-escape = "=0.1.5" tar.workspace = true text-size = "=1.1.0" text_lines = "=0.6.0" -thiserror = "=1.0.38" +thiserror.workspace = true tokio.workspace = true tokio-util.workspace = true tower-lsp.workspace = true diff --git a/lockfile/Cargo.toml b/lockfile/Cargo.toml index 036df881ce..b509fe31f8 100644 --- a/lockfile/Cargo.toml +++ b/lockfile/Cargo.toml @@ -11,10 +11,10 @@ description = "An implementation of a lockfile used in Deno" path = "lib.rs" [dependencies] -anyhow.workspace = true ring.workspace = true serde.workspace = true serde_json.workspace = true +thiserror.workspace = true [dev-dependencies] test_util.workspace = true diff --git a/lockfile/error.rs b/lockfile/error.rs new file mode 100644 index 0000000000..3613033163 --- /dev/null +++ b/lockfile/error.rs @@ -0,0 +1,15 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum LockfileError { + #[error(transparent)] + Io(#[from] std::io::Error), + + #[error("Unable to read lockfile: \"{0}\"")] + ReadError(String), + + #[error("Unable to parse contents of lockfile: \"{0}\"")] + ParseError(String), +} diff --git a/lockfile/lib.rs b/lockfile/lib.rs index 12ebb7441f..fa2a3a6398 100644 --- a/lockfile/lib.rs +++ b/lockfile/lib.rs @@ -1,10 +1,11 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +mod error; +pub use error::LockfileError as Error; + use std::collections::BTreeMap; use std::io::Write; -use anyhow::Context; -use anyhow::Error as AnyError; use ring::digest; use serde::Deserialize; use serde::Serialize; @@ -109,7 +110,7 @@ pub struct Lockfile { } impl Lockfile { - pub fn new(filename: PathBuf, overwrite: bool) -> Result { + pub fn new(filename: PathBuf, overwrite: bool) -> Result { // Writing a lock file always uses the new format. if overwrite { return Ok(Lockfile { @@ -136,35 +137,20 @@ impl Lockfile { } }; - let s = result.with_context(|| { - format!("Unable to read lockfile: \"{}\"", filename.display()) - })?; - let value: serde_json::Value = - serde_json::from_str(&s).with_context(|| { - format!( - "Unable to parse contents of the lockfile \"{}\"", - filename.display() - ) - })?; + let s = + result.map_err(|_| Error::ReadError(filename.display().to_string()))?; + let value: serde_json::Value = serde_json::from_str(&s) + .map_err(|_| Error::ParseError(filename.display().to_string()))?; let version = value.get("version").and_then(|v| v.as_str()); let content = if version == Some("2") { - serde_json::from_value::(value).with_context(|| { - format!( - "Unable to parse contents of the lockfile \"{}\"", - filename.display() - ) - })? + serde_json::from_value::(value) + .map_err(|_| Error::ParseError(filename.display().to_string()))? } else { // If there's no version field, we assume that user is using the old // version of the lockfile. We'll migrate it in-place into v2 and it - // will be writte in v2 if user uses `--lock-write` flag. + // will be written in v2 if user uses `--lock-write` flag. let remote: BTreeMap = serde_json::from_value(value) - .with_context(|| { - format!( - "Unable to parse contents of the lockfile \"{}\"", - filename.display() - ) - })?; + .map_err(|_| Error::ParseError(filename.display().to_string()))?; LockfileContent { version: "2".to_string(), remote, @@ -181,7 +167,7 @@ impl Lockfile { } // Synchronize lock file to disk - noop if --lock-write file is not specified. - pub fn write(&self) -> Result<(), AnyError> { + pub fn write(&self) -> Result<(), Error> { if !self.has_content_changed && !self.overwrite { return Ok(()); }