Hello all.
I'd like to propose addition of a new method of standard Exception class.
This method is somewhat identical to Java's fillInStackTrace() [0], but fills not only the "trace",
but also "line" and "file" properties.
See #33407 [1], this report explains how this method can be used.
Short example:
Exception::fillException() (the name is still under question) returns new
exception object that can be used this way:
<?php
try {
throw new Exception("message", 1);
} catch(Exception $e) {
throw $e->fillException();
}
?>
In the code above "line", "file" and "trace" properties will point at the
file/line where fillException() is called, not the file/line
where $e was constructed.
All the other properties of $e are copied, so they remain untouched.
See diff against HEAD in attachment.
[0]
http://java.sun.com/j2se/1.3/docs/api/java/lang/Throwable.html#fillInStackTrace()
[1] http://bugs.php.net/bug.php?id=33407
--
Wbr,
Antony Dovgal
Index: Zend/zend_exceptions.c
===================================================================
RCS file: /repository/ZendEngine2/zend_exceptions.c,v
retrieving revision 1.94
diff -u -p -d -r1.94 zend_exceptions.c
--- Zend/zend_exceptions.c 4 Jan 2006 23:52:06 -0000 1.94
+++ Zend/zend_exceptions.c 10 Jan 2006 13:24:57 -0000
@@ -294,6 +294,37 @@ ZEND_METHOD(error_exception, getSeverity
}
/* }}} */
+/* {{{ proto int ErrorException::fillException()
+ Fill in exception properties */
+ZEND_METHOD(exception, fillException)
+{
+ zval *exception = getThis();
+ zval *trace, tmp;
+ zend_object *object;
+
+ DEFAULT_0_PARAMS;
+
+ INIT_PZVAL(return_value);
+ Z_TYPE_P(return_value) = IS_OBJECT;
+ Z_OBJVAL_P(return_value) = zend_objects_new(&object,
Z_OBJCE_P(exception) TSRMLS_CC);
+ Z_OBJ_HT_P(return_value) = Z_OBJ_HT_P(exception);
+
+ ALLOC_HASHTABLE(object->properties);
+ zend_u_hash_init(object->properties, 0, NULL, ZVAL_PTR_DTOR, 0,
UG(unicode));
+ zend_hash_copy(object->properties, Z_OBJPROP_P(exception),
(copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
+
+ ALLOC_ZVAL(trace);
+ trace->is_ref = 0;
+ trace->refcount = 0;
+
+ zend_fetch_debug_backtrace(trace, 0, 0 TSRMLS_CC);
+
+ zend_update_property(Z_OBJCE_P(exception), return_value, "trace",
sizeof("trace")-1, trace TSRMLS_CC);
+ zend_update_property_rt_string(Z_OBJCE_P(exception), return_value,
"file", sizeof("file")-1, zend_get_executed_filename(TSRMLS_C) TSRMLS_CC);
+ zend_update_property_long(Z_OBJCE_P(exception), return_value, "line",
sizeof("line")-1, zend_get_executed_lineno(TSRMLS_C) TSRMLS_CC);
+}
+/* }}} */
+
/* {{{ ZEND_METHOD(exception, gettraceasstring) */
#define TRACE_APPEND_CHR(chr) \
*str = (char*)erealloc(*str, *len + 1 + 1); \
@@ -604,6 +635,7 @@ static zend_function_entry default_excep
ZEND_ME(exception, getTrace, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(exception, getTraceAsString, NULL,
ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(exception, __toString, NULL, 0)
+ ZEND_ME(exception, fillException, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
{NULL, NULL, NULL}
};
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php