changeset 52045:652149ed64f0

rust: improve `InvalidRevision` error message I encountered this when debugging earlier and felt like we were losing some information along the way, which we were!
author Raphaël Gomès <rgomes@octobus.net>
date Tue, 01 Oct 2024 13:20:40 +0200
parents 0cd16b1d613f
children 28a0eb21ff04
files rust/hg-core/src/revlog/index.rs rust/hg-core/src/revlog/mod.rs rust/hg-core/src/revset.rs rust/rhg/src/error.rs tests/test-rhg-sparse-narrow.t
diffstat 5 files changed, 30 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/index.rs	Mon Sep 30 17:19:35 2024 +0200
+++ b/rust/hg-core/src/revlog/index.rs	Tue Oct 01 13:20:40 2024 +0200
@@ -829,7 +829,7 @@
             }
             let [mut p1, mut p2] = self
                 .parents(rev)
-                .map_err(|_| RevlogError::InvalidRevision)?;
+                .map_err(|e| RevlogError::InvalidRevision(e.to_string()))?;
             while let Some(p1_entry) = self.get_entry(p1) {
                 if p1_entry.compressed_len() != 0 || p1.0 == 0 {
                     break;
@@ -839,9 +839,9 @@
                 if parent_base.0 == p1.0 {
                     break;
                 }
-                p1 = self
-                    .check_revision(parent_base)
-                    .ok_or(RevlogError::InvalidRevision)?;
+                p1 = self.check_revision(parent_base).ok_or(
+                    RevlogError::InvalidRevision(parent_base.to_string()),
+                )?;
             }
             while let Some(p2_entry) = self.get_entry(p2) {
                 if p2_entry.compressed_len() != 0 || p2.0 == 0 {
@@ -852,16 +852,16 @@
                 if parent_base.0 == p2.0 {
                     break;
                 }
-                p2 = self
-                    .check_revision(parent_base)
-                    .ok_or(RevlogError::InvalidRevision)?;
+                p2 = self.check_revision(parent_base).ok_or(
+                    RevlogError::InvalidRevision(parent_base.to_string()),
+                )?;
             }
             if base == p1.0 || base == p2.0 {
                 return Ok(false);
             }
             rev = self
                 .check_revision(base.into())
-                .ok_or(RevlogError::InvalidRevision)?;
+                .ok_or(RevlogError::InvalidRevision(base.to_string()))?;
         }
         Ok(rev == NULL_REVISION)
     }
--- a/rust/hg-core/src/revlog/mod.rs	Mon Sep 30 17:19:35 2024 +0200
+++ b/rust/hg-core/src/revlog/mod.rs	Tue Oct 01 13:20:40 2024 +0200
@@ -139,6 +139,16 @@
     ParentOutOfRange(Revision),
 }
 
+impl std::fmt::Display for GraphError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        match self {
+            GraphError::ParentOutOfRange(revision) => {
+                write!(f, "parent out of range ({})", revision)
+            }
+        }
+    }
+}
+
 impl<T: Graph> Graph for &T {
     fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
         (*self).parents(rev)
@@ -193,7 +203,8 @@
 
 #[derive(Debug, derive_more::From, derive_more::Display)]
 pub enum RevlogError {
-    InvalidRevision,
+    #[display(fmt = "invalid revision identifier: {}", "_0")]
+    InvalidRevision(String),
     /// Working directory is not supported
     WDirUnsupported,
     /// Found more than one entry whose ID match the requested prefix
@@ -809,7 +820,7 @@
         if let Some(nodemap) = &self.nodemap {
             nodemap
                 .find_bin(&self.index, node)?
-                .ok_or(RevlogError::InvalidRevision)
+                .ok_or(RevlogError::InvalidRevision(format!("{:x}", node)))
         } else {
             self.rev_from_node_no_persistent_nodemap(node)
         }
@@ -851,7 +862,8 @@
                 found_by_prefix = Some(rev)
             }
         }
-        found_by_prefix.ok_or(RevlogError::InvalidRevision)
+        found_by_prefix
+            .ok_or(RevlogError::InvalidRevision(format!("{:x}", node)))
     }
 
     /// Returns whether the given revision exists in this revlog.
@@ -960,7 +972,7 @@
         let index_entry = self
             .index
             .get_entry(rev)
-            .ok_or(RevlogError::InvalidRevision)?;
+            .ok_or(RevlogError::InvalidRevision(rev.to_string()))?;
         let offset = index_entry.offset();
         let start = if self.index.is_inline() {
             offset + ((rev.0 as usize + 1) * INDEX_ENTRY_SIZE)
--- a/rust/hg-core/src/revset.rs	Mon Sep 30 17:19:35 2024 +0200
+++ b/rust/hg-core/src/revset.rs	Tue Oct 01 13:20:40 2024 +0200
@@ -28,9 +28,9 @@
     }
 
     match resolve_rev_number_or_hex_prefix(input, &changelog.revlog) {
-        Err(RevlogError::InvalidRevision) => {
+        Err(RevlogError::InvalidRevision(revision)) => {
             // TODO: support for the rest of the language here.
-            let msg = format!("cannot parse revset '{}'", input);
+            let msg = format!("cannot parse revset '{}'", revision);
             Err(HgError::unsupported(msg).into())
         }
         result => result,
@@ -67,5 +67,5 @@
         }
         return revlog.rev_from_node(prefix);
     }
-    Err(RevlogError::InvalidRevision)
+    Err(RevlogError::InvalidRevision(input.to_string()))
 }
--- a/rust/rhg/src/error.rs	Mon Sep 30 17:19:35 2024 +0200
+++ b/rust/rhg/src/error.rs	Tue Oct 01 13:20:40 2024 +0200
@@ -207,9 +207,9 @@
             RevlogError::WDirUnsupported => CommandError::abort(
                 "abort: working directory revision cannot be specified",
             ),
-            RevlogError::InvalidRevision => CommandError::abort(format!(
+            RevlogError::InvalidRevision(r) => CommandError::abort(format!(
                 "abort: invalid revision identifier: {}",
-                rev
+                r
             )),
             RevlogError::AmbiguousPrefix => CommandError::abort(format!(
                 "abort: ambiguous revision identifier: {}",
--- a/tests/test-rhg-sparse-narrow.t	Mon Sep 30 17:19:35 2024 +0200
+++ b/tests/test-rhg-sparse-narrow.t	Tue Oct 01 13:20:40 2024 +0200
@@ -70,7 +70,7 @@
 TODO: bad error message
 
   $ $NO_FALLBACK rhg cat -r "$tip" hide
-  abort: invalid revision identifier: 6d714a4a2998cbfd0620db44da58b749f6565d63
+  abort: invalid revision identifier: 1406e74118627694268417491f018a4a883152f0
   [255]
   $ "$real_hg" cat -r "$tip" hide
   [1]