Debian Codegraph @

rust/hg-pyo3/src/exceptions.rs

raw
1 use std::fmt::Display;
2
3 use hg::dirstate::on_disk::DirstateV2ParseError;
4 use hg::dirstate::DirstateError;
5 use hg::revlog::nodemap::NodeMapError;
6 use hg::revlog::RevlogError;
7 use hg::UncheckedRevision;
8 use pyo3::create_exception;
9 use pyo3::exceptions::PyOSError;
10 use pyo3::exceptions::PyRuntimeError;
11 use pyo3::exceptions::PyValueError;
12 use pyo3::import_exception;
13 use pyo3::PyErr;
14
15 use crate::revision::PyRevision;
16
17 create_exception!(pyo3_rustext, GraphError, PyValueError);
18 create_exception!(pyo3_rustext, FallbackError, PyRuntimeError);
19 import_exception!(mercurial.error, WdirUnsupported);
20
21 impl GraphError {
22 pub fn from_hg(inner: hg::GraphError) -> PyErr {
23 match inner {
24 hg::GraphError::ParentOutOfRange(r) => {
25 GraphError::new_err(("ParentOutOfRange", PyRevision(r.0)))
26 }
27 hg::GraphError::ParentOutOfOrder(r) => {
28 GraphError::new_err(("ParentOutOfOrder", PyRevision(r.0)))
29 }
30 }
31 }
32 pub fn from_vcsgraph(inner: vcsgraph::graph::GraphReadError) -> PyErr {
33 match inner {
34 vcsgraph::graph::GraphReadError::InconsistentGraphData => {
35 GraphError::new_err("InconsistentGraphData")
36 }
37 vcsgraph::graph::GraphReadError::InvalidKey => {
38 GraphError::new_err("ParentOutOfRange")
39 }
40 vcsgraph::graph::GraphReadError::KeyedInvalidKey(r) => {
41 GraphError::new_err(("ParentOutOfRange", r))
42 }
43 vcsgraph::graph::GraphReadError::WorkingDirectoryUnsupported => {
44 WdirUnsupported::new_err(())
45 }
46 }
47 }
48 }
49
50 pub fn map_lock_error<T>(e: std::sync::PoisonError<T>) -> PyErr {
51 PyRuntimeError::new_err(format!("In Rust PyO3 bindings: {e}"))
52 }
53
54 pub fn map_try_lock_error<T>(e: std::sync::TryLockError<T>) -> PyErr {
55 PyRuntimeError::new_err(format!("In Rust PyO3 bindings: {e}"))
56 }
57
58 pub fn to_string_value_error<T: Display>(e: T) -> PyErr {
59 PyValueError::new_err(e.to_string())
60 }
61
62 pub mod mercurial_py_errors {
63 pyo3::import_exception!(mercurial.error, RevlogError);
64 }
65
66 pub fn revlog_error_from_msg(e: impl ToString) -> PyErr {
67 mercurial_py_errors::RevlogError::new_err(e.to_string().into_bytes())
68 }
69
70 pub fn revlog_error_bare() -> PyErr {
71 mercurial_py_errors::RevlogError::new_err(("None",))
72 }
73
74 pub fn revlog_error(revlog_error: RevlogError) -> PyErr {
75 mercurial_py_errors::RevlogError::new_err(revlog_error.to_string())
76 }
77
78 pub fn rev_not_in_index(rev: UncheckedRevision) -> PyErr {
79 PyValueError::new_err(format!("revlog index out of range: {}", rev))
80 }
81
82 pub fn nodemap_error(err: NodeMapError) -> PyErr {
83 match err {
84 NodeMapError::MultipleResults => {
85 mercurial_py_errors::RevlogError::new_err("")
86 }
87
88 NodeMapError::RevisionNotInIndex(rev) => {
89 PyValueError::new_err(format!(
90 "Inconsistency: Revision {} found in nodemap \
91 is not in revlog index",
92 rev
93 ))
94 }
95 }
96 }
97
98 pub fn graph_error(_err: hg::GraphError) -> PyErr {
99 // ParentOutOfRange is currently the only alternative
100 // in `hg::GraphError`. The C index always raises this simple ValueError.
101 PyValueError::new_err("parent out of range")
102 }
103
104 pub fn dirstate_error(err: DirstateError) -> PyErr {
105 PyOSError::new_err(format!("Dirstate error: {:?}", err))
106 }
107
108 pub fn dirstate_v2_error(_err: DirstateV2ParseError) -> PyErr {
109 PyValueError::new_err("corrupted dirstate-v2")
110 }