diff --git a/sentry.c b/sentry.c index e19a6d1..e9fdbb2 100644 --- a/sentry.c +++ b/sentry.c @@ -512,6 +512,15 @@ static void sentry_observer_end(zend_execute_data *execute_data, zval *return_va ZVAL_COPY(&metadata_zv, &state->metadata); add_assoc_zval(&event, "metadata", &metadata_zv); + zend_object *exception = EG(exception); + if (exception != NULL) { + zval exception_zv; + ZVAL_OBJ_COPY(&exception_zv, exception); + add_assoc_zval(&event, "exception", &exception_zv); + } else { + add_assoc_null(&event, "exception"); + } + ZVAL_COPY_VALUE(¶ms[0], &event); ZVAL_COPY_VALUE(¶ms[1], &state->user_state); diff --git a/tests/test_cought_exceptions_do_not_leak.phpt b/tests/test_cought_exceptions_do_not_leak.phpt new file mode 100644 index 0000000..55ec6f9 --- /dev/null +++ b/tests/test_cought_exceptions_do_not_leak.phpt @@ -0,0 +1,45 @@ +--TEST-- +Tests that re-throwing different exceptions properly populates the 'exception' key. +--EXTENSIONS-- +sentry +--FILE-- +getMessage() . PHP_EOL; + echo get_class($exception) . PHP_EOL; + } else { + echo 'No exception here' . PHP_EOL; + } +}); + +\Sentry\instrument(null, 'test_throw'); +\Sentry\instrument(null, 'test_rethrow'); +try { +test_rethrow(); +} catch (Throwable $t) { + +} + +?> +--EXPECTF-- +Oh no +CustomException +Exception caught +No exception here \ No newline at end of file diff --git a/tests/test_exception_set_in_end_callback.phpt b/tests/test_exception_set_in_end_callback.phpt new file mode 100644 index 0000000..32ed6db --- /dev/null +++ b/tests/test_exception_set_in_end_callback.phpt @@ -0,0 +1,32 @@ +--TEST-- +Tests that a thrown exception is captured and stored under the 'exception' key in the end callback +--EXTENSIONS-- +sentry +--FILE-- +getMessage() . PHP_EOL; + echo get_class($exception) . PHP_EOL; +}); + +\Sentry\instrument(null, 'test_throw'); +try { +test_throw(); +} catch (Throwable $t) { + +} + +?> +--EXPECTF-- +Oh no +CustomException \ No newline at end of file diff --git a/tests/test_fields_always_present.phpt b/tests/test_fields_always_present.phpt new file mode 100644 index 0000000..21b5ac3 --- /dev/null +++ b/tests/test_fields_always_present.phpt @@ -0,0 +1,40 @@ +--TEST-- +Tests that some fields are always present in the start and end callback and also assert their type. +--EXTENSIONS-- +sentry +--FILE-- + +--EXPECTF-- +Start callback +start_time: float +name: string +End callback +start_time: float +name: string +duration: float +end_time: float +metadata: array diff --git a/tests/test_metadata_key_always_exists.phpt b/tests/test_metadata_key_always_exists.phpt new file mode 100644 index 0000000..2812077 --- /dev/null +++ b/tests/test_metadata_key_always_exists.phpt @@ -0,0 +1,27 @@ +--TEST-- +Tests that the 'metadata' key in data in the end callback is always populated and never null (Function) +--EXTENSIONS-- +sentry +--FILE-- + +--EXPECTF-- +[] \ No newline at end of file diff --git a/tests/test_metadata_key_always_exists_attribute.phpt b/tests/test_metadata_key_always_exists_attribute.phpt new file mode 100644 index 0000000..c61c671 --- /dev/null +++ b/tests/test_metadata_key_always_exists_attribute.phpt @@ -0,0 +1,27 @@ +--TEST-- +Tests that the 'metadata' key in data in the end callback is always populated and never null (Attribute) +--EXTENSIONS-- +sentry +--FILE-- + +--EXPECTF-- +[] \ No newline at end of file diff --git a/tests/test_nested_exceptions_properly_scoped.phpt b/tests/test_nested_exceptions_properly_scoped.phpt new file mode 100644 index 0000000..08d6d76 --- /dev/null +++ b/tests/test_nested_exceptions_properly_scoped.phpt @@ -0,0 +1,43 @@ +--TEST-- +Tests that catching an exception in a function will make 'exception' be null in the end callback +--EXTENSIONS-- +sentry +--FILE-- +getMessage() . PHP_EOL; + echo get_class($exception) . PHP_EOL; +}); + +\Sentry\instrument(null, 'test_throw'); +\Sentry\instrument(null, 'test_rethrow'); +try { +test_rethrow(); +} catch (Throwable $t) { + +} + +?> +--EXPECTF-- +Oh no +CustomException +throw different exception +TestException \ No newline at end of file