Skip to content

Commit 5d8d4a2

Browse files
committed
feature:exception handler
1 parent b4fbbec commit 5d8d4a2

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ pub fn get_module() -> Module {
3434
Argument::by_val("errline"),
3535
]);
3636

37+
module
38+
.add_function("minitrace_exception_handler", minitrace_exception_handler)
39+
.arguments(vec![Argument::by_val("ex")]);
40+
3741
module.on_module_init(module::init);
3842
module.on_request_init(request::init);
3943
module.on_request_shutdown(request::shutdown);
@@ -63,3 +67,40 @@ fn minitrace_error_handler(arguments: &mut [ZVal]) -> phper::Result<()> {
6367
ctx.end_span();
6468
Ok(())
6569
}
70+
71+
fn minitrace_exception_handler(arguments: &mut [ZVal]) -> phper::Result<()> {
72+
let ex = arguments[0].expect_mut_z_obj()?;
73+
let binding = ex.call("getMessage", &mut [])?;
74+
let message = binding
75+
.as_z_str()
76+
.ok_or(phper::errors::NotImplementThrowableError)?
77+
.to_str()?;
78+
let binding = ex.call("getFile", &mut [])?;
79+
let file = binding
80+
.as_z_str()
81+
.ok_or(phper::errors::NotImplementThrowableError)?
82+
.to_str()?;
83+
let line = ex
84+
.call("getLine", &mut [])?
85+
.as_long()
86+
.ok_or(phper::errors::NotImplementThrowableError)?;
87+
let binding = ex.call("getTraceAsString", &mut [])?;
88+
let trace = binding
89+
.as_z_str()
90+
.ok_or(phper::errors::NotImplementThrowableError)?
91+
.to_str()?;
92+
93+
let class_name = ex.get_class().get_name().to_str()?;
94+
95+
let mut payload = HashMap::new();
96+
payload.insert("trace".to_string(), trace.to_string());
97+
98+
let ctx = context::get_context();
99+
ctx.start_span(
100+
crate::span::SPAN_KIND_EXCEPTION,
101+
format!("{}: {} in {} on line {}", class_name, message, file, line).as_str(),
102+
payload,
103+
);
104+
ctx.end_span();
105+
Ok(())
106+
}

src/request.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ pub fn init() {
1111
&mut [ZVal::from("minitrace_error_handler")],
1212
);
1313

14+
let _ = phper::functions::call(
15+
"set_exception_handler",
16+
&mut [ZVal::from("minitrace_exception_handler")],
17+
);
18+
1419
jit_initialization();
1520

1621
let get = get_page_request_get().unwrap();

src/span.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub const SPAN_KIND_PDO: &str = "PDO";
88
pub const SPAN_KIND_PDO_STATEMENT: &str = "PDO_STATEMENT";
99
pub const SPAN_KIND_CURL: &str = "CURL";
1010
pub const SPAN_KIND_ERROR: &str = "ERROR";
11+
pub const SPAN_KIND_EXCEPTION: &str = "EXCEPTION";
1112

1213
#[derive(Debug)]
1314
pub struct Span {

test_curl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
66
$response = curl_exec($ch);
77
curl_close($ch);
8-
var_dump($xxxxxx);
8+
var_dump($xxxxxx);
9+
10+
throw new Exception('test exception');

0 commit comments

Comments
 (0)