refactor(lockfile): move anyhow to thiserror (#18178)

This commit is contained in:
Yiyu Lin 2023-03-14 22:55:52 +08:00 committed by Yoshiya Hinosawa
parent 980a3d3ac7
commit 7773af5861
6 changed files with 32 additions and 30 deletions

2
Cargo.lock generated
View File

@ -1211,11 +1211,11 @@ dependencies = [
name = "deno_lockfile"
version = "0.8.0"
dependencies = [
"anyhow",
"ring",
"serde",
"serde_json",
"test_util",
"thiserror",
]
[[package]]

View File

@ -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"

View File

@ -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

View File

@ -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

15
lockfile/error.rs Normal file
View File

@ -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),
}

View File

@ -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<Lockfile, AnyError> {
pub fn new(filename: PathBuf, overwrite: bool) -> Result<Lockfile, Error> {
// 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::<LockfileContent>(value).with_context(|| {
format!(
"Unable to parse contents of the lockfile \"{}\"",
filename.display()
)
})?
serde_json::from_value::<LockfileContent>(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<String, String> = 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(());
}