我正在用 Rust 编写一个跨平台(Linux/iOS/Android)库。万一我的库发生恐慌,我希望应用程序继续正常工作而不是崩溃。为了做到这一点,我正在使用catch_unwind; 其结果包含恐慌信息,并且不包含有关问题根源的足够信息,这里有一段代码解释了我正在做的事情:
fn calculate(json: &str) -> String {
unimplemented!()
}
#[no_mangle]
pub fn rust_calculation(json: &str) -> String {
let r = std::panic::catch_unwind(||{
// rust calculation
let calc: String = calculate(json).into();
calc
});
let r_str = match r {
Ok(v) => v,
Err(e) => {
let panic_information = match e.downcast::<String>() {
Ok(v) => *v,
_ => "Unknown Source of Error".to_owned()
};
panic_information
}
};
return r_str;
}
fn main() {
println!("{}", rust_calculation("test"));
}
Run Code Online (Sandbox Code Playgroud)
如果发生错误,返回的消息是不够的,有时,它只包含消息
错误来源未知
我想将回溯发送到调用源,以便它可以记录它,然后我们可以调试 …
我正在尝试用 php 实现 firebase,这是我找到的一个链接:
https://github.com/ktamas77/firebase-php
这是一个例子。
const DEFAULT_URL = 'https://kidsplace.firebaseio.com/';
const DEFAULT_TOKEN = 'MqL0c8tKCtheLSYcygYNtGhU8Z2hULOFs9OKPdEp';
const DEFAULT_PATH = '/firebase/example';
$firebase = new \Firebase\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN);
// --- storing an array ---
$test = array(
"foo" => "bar",
"i_love" => "lamp",
"id" => 42
);
$dateTime = new DateTime();
$firebase->set(DEFAULT_PATH . '/' . $dateTime->format('c'), $test);
// --- storing a string ---
$firebase->set(DEFAULT_PATH . '/name/contact001', "John Doe");
// --- reading the stored string ---
$name = $firebase->get(DEFAULT_PATH . '/name/contact001');
Run Code Online (Sandbox Code Playgroud)
这是我的相同代码的版本:
<?php
include 'firebaseLib.php';
const …Run Code Online (Sandbox Code Playgroud)