comparison rust/hg-cpython/src/exceptions.rs @ 42328:94f3a73b6672

rust-filepatterns: add `rust-cpython` bindings for `filepatterns` This change adds the `rust-cpython` interface for top-level functions and exceptions in the filepatterns module. Contrary to the Python implementation, this tries to have finer-grained exceptions to allow for better readability and flow control down the line. Differential Revision: https://phab.mercurial-scm.org/D6272
author Rapha?l Gom?s <rgomes@octobus.net>
date Fri, 17 May 2019 09:36:29 -0400
parents ee943a920606
children d26e4a434fe5
comparison
equal deleted inserted replaced
42327:e8f3740cc067 42328:94f3a73b6672
10 //! [`GraphError`] exposes `hg::GraphError` as a subclass of `ValueError` 10 //! [`GraphError`] exposes `hg::GraphError` as a subclass of `ValueError`
11 //! but some variants of `hg::GraphError` can be converted directly to other 11 //! but some variants of `hg::GraphError` can be converted directly to other
12 //! existing Python exceptions if appropriate. 12 //! existing Python exceptions if appropriate.
13 //! 13 //!
14 //! [`GraphError`]: struct.GraphError.html 14 //! [`GraphError`]: struct.GraphError.html
15 use cpython::exc::ValueError; 15 use cpython::exc::{ValueError, RuntimeError};
16 use cpython::{PyErr, Python}; 16 use cpython::{PyErr, Python, exc};
17 use hg; 17 use hg;
18 18
19 py_exception!(rustext, GraphError, ValueError); 19 py_exception!(rustext, GraphError, ValueError);
20 20
21 impl GraphError { 21 impl GraphError {
26 } 26 }
27 hg::GraphError::WorkingDirectoryUnsupported => { 27 hg::GraphError::WorkingDirectoryUnsupported => {
28 match py 28 match py
29 .import("mercurial.error") 29 .import("mercurial.error")
30 .and_then(|m| m.get(py, "WdirUnsupported")) 30 .and_then(|m| m.get(py, "WdirUnsupported"))
31 { 31 {
32 Err(e) => e, 32 Err(e) => e,
33 Ok(cls) => PyErr::from_instance(py, cls), 33 Ok(cls) => PyErr::from_instance(py, cls),
34 }
35 }
36 }
37 }
38 }
39
40 py_exception!(rustext, PatternError, RuntimeError);
41 py_exception!(rustext, PatternFileError, RuntimeError);
42
43 impl PatternError {
44 pub fn pynew(py: Python, inner: hg::PatternError) -> PyErr {
45 match inner {
46 hg::PatternError::UnsupportedSyntax(m) => {
47 PatternError::new(py, ("PatternError", m))
48 }
49 }
50 }
51 }
52
53
54 impl PatternFileError {
55 pub fn pynew(py: Python, inner: hg::PatternFileError) -> PyErr {
56 match inner {
57 hg::PatternFileError::IO(e) => {
58 let value = (
59 e.raw_os_error().unwrap_or(2),
60 e.to_string()
61 );
62 PyErr::new::<exc::IOError, _>(py, value)
63 }
64 hg::PatternFileError::Pattern(e, l) => {
65 match e {
66 hg::PatternError::UnsupportedSyntax(m) =>
67 PatternFileError::new(py, ("PatternFileError", m, l))
34 } 68 }
35 } 69 }
36 } 70 }
37 } 71 }
38 } 72 }