From 96b1ede254cfca3bbb1741adb653975789605290 Mon Sep 17 00:00:00 2001
From: Yiyu Lin <linyiyu1992@gmail.com>
Date: Tue, 14 Mar 2023 22:55:52 +0800
Subject: [PATCH] refactor(lockfile): move `anyhow` to `thiserror` (#18178)

---
 Cargo.lock          |  2 +-
 Cargo.toml          |  1 +
 cli/Cargo.toml      |  2 +-
 lockfile/Cargo.toml |  2 +-
 lockfile/error.rs   | 15 +++++++++++++++
 lockfile/lib.rs     | 40 +++++++++++++---------------------------
 6 files changed, 32 insertions(+), 30 deletions(-)
 create mode 100644 lockfile/error.rs

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<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(());
     }