changeset 51252:5807e3a8865e

rust-python-index: don't panic on a corrupted index when calling from Python This makes `test-verify.t` pass again. In an ideal world, we would find the exact commit where this test breaks and amend part of this change there, but this is a long enough series.
author Raphaël Gomès <rgomes@octobus.net>
date Tue, 31 Oct 2023 17:36:59 +0100
parents f9a52a9603f9
children 3551f2a1c963
files rust/hg-cpython/src/revlog.rs
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-cpython/src/revlog.rs	Tue Oct 31 17:34:31 2023 +0100
+++ b/rust/hg-cpython/src/revlog.rs	Tue Oct 31 17:36:59 2023 +0100
@@ -675,7 +675,9 @@
                         .expect("default header is broken")
                         .unwrap(),
                 )
-                .unwrap(),
+                .map_err(|e| {
+                    revlog_error_with_msg(py, e.to_string().as_bytes())
+                })?,
             ),
             RefCell::new(None),
             RefCell::new(None),
@@ -1052,6 +1054,21 @@
     }
 }
 
+fn revlog_error_with_msg(py: Python, msg: &[u8]) -> PyErr {
+    match py
+        .import("mercurial.error")
+        .and_then(|m| m.get(py, "RevlogError"))
+    {
+        Err(e) => e,
+        Ok(cls) => PyErr::from_instance(
+            py,
+            cls.call(py, (PyBytes::new(py, msg),), None)
+                .ok()
+                .into_py_object(py),
+        ),
+    }
+}
+
 fn graph_error(py: Python, _err: hg::GraphError) -> PyErr {
     // ParentOutOfRange is currently the only alternative
     // in `hg::GraphError`. The C index always raises this simple ValueError.