Macro python_comm::raise_error [−][src]
macro_rules! raise_error {
($python : expr, $func : ident, $text : expr, "\n", $err : expr) => { ... };
($func : ident, $text : expr, "\n", $err : expr) => { ... };
($func : ident, "\n", $err : expr) => { ... };
($func : ident, $text : expr) => { ... };
}
Expand description
生成带文件名、行号、函数的 cpython::PyErr 或 anyhow::Error
用法
use cpython::Python;
use python_comm::prelude::*;
use python_comm_macros::auto_func_name;
let gil = Python::acquire_gil();
let py = gil.python();
#[auto_func_name]
fn test(py:Python) -> Result<(), anyhow::Error> {
let v = Ok(2).and(Err("e"));
// 用法1:生成 cpython::PyErr
let _a = v.or_else(|err| raise_error!(py, __func__, "some text", "\n", err));
// 用法2:生成 anyhow::Error, 包含补充说明及另一个 error
let b = v.or_else(|err| raise_error!(__func__, "some text", "\n", err));
// 用法3:生成 anyhow::Error, 包含另一个 error
let _c = v.or_else(|err| raise_error!(__func__, "\n", err));
// 用法3:生成 anyhow::Error, 补充说明
let _d = v.or_else(|_err| Err(raise_error!(__func__, "some text")));
b
}
let err = format!("{:?}", test(py).err().unwrap());
let msg1 = "Error: src\\macros.rs:19 test() some text\n\"e\"";
let msg2 = "Error: src/macros.rs:19 test() some text\n\"e\"";
assert!(err == msg1 || err == msg2, "\n left: {:?}\n msg1:{:?}\n msg2:{:?}", err, msg1, msg2);