summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Boerger <helly@php.net>2003-08-28 22:56:41 +0000
committerMarcus Boerger <helly@php.net>2003-08-28 22:56:41 +0000
commitfa70708d1592783a88ea7ec1add1b6ec6d231f8a (patch)
tree8e3e4d2a6491102f5b0482cf97b1e1813f67b636
parent12376a22702ddc45aad463a1684cf84eecde77af (diff)
downloadphp-git-fa70708d1592783a88ea7ec1add1b6ec6d231f8a.tar.gz
Add zend_throw_exception_ex() which allows to format exception messages.
# # Since we don't have any portable way of printing into a dynamic buffer i # used a stack buffer of 1K (just like the error printing) and used a dynamic # buffer in case the necessary function is available. #
-rw-r--r--Zend/zend_default_classes.c47
-rw-r--r--Zend/zend_default_classes.h1
-rw-r--r--Zend/zend_exceptions.c47
-rw-r--r--Zend/zend_exceptions.h1
4 files changed, 96 insertions, 0 deletions
diff --git a/Zend/zend_default_classes.c b/Zend/zend_default_classes.c
index e99e6d3287..6de12a69a2 100644
--- a/Zend/zend_default_classes.c
+++ b/Zend/zend_default_classes.c
@@ -144,6 +144,53 @@ ZEND_API zend_class_entry *zend_exception_get_default(void)
return default_exception_ptr;
}
+ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...)
+{
+ zval *ex;
+ va_list arg;
+
+#ifdef _GNU_SOURCE
+ char *message;
+ va_start(arg, format);
+ vasprintf(message, format, arg);
+ va_end(arg);
+#else
+ char message[1024];
+ va_start(arg, format);
+ vsnprintf(message, sizeof(message), format, arg);
+ va_end(arg);
+#endif
+
+ MAKE_STD_ZVAL(ex);
+ if (exception_ce) {
+ if (!instanceof_function(exception_ce, default_exception_ptr TSRMLS_CC)) {
+ zend_error(E_NOTICE, "Exceptions must be derived from exception");
+ exception_ce = default_exception_ptr;
+ }
+ } else {
+ exception_ce = default_exception_ptr;
+ }
+ object_init_ex(ex, exception_ce);
+
+
+ if (message) {
+ zend_update_property_string(exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
+ }
+ if (code) {
+ zend_update_property_long(exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
+ }
+
+#ifdef _GNU_SOURCE
+ free(message);
+#endif
+
+ EG(exception) = ex;
+}
+
+/* at the moment we can't use zend_throw_exception_ex because we don't have a protable
+ * vsnprintf that tells us the number of characters needed nor do we have spprintf from
+ * php or asprintf from glibc always.
+ */
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC)
{
zval *ex;
diff --git a/Zend/zend_default_classes.h b/Zend/zend_default_classes.h
index 0e1d70f26a..9b6e17661c 100644
--- a/Zend/zend_default_classes.h
+++ b/Zend/zend_default_classes.h
@@ -30,6 +30,7 @@ ZEND_API void zend_register_default_classes(TSRMLS_D);
/* exception_ce NULL or zend_exception_get_default() or a derived class
* message NULL or the message of the exception */
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC);
+ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...);
/* show an exception using zend_error(E_ERROR,...) */
ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);
diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c
index e99e6d3287..6de12a69a2 100644
--- a/Zend/zend_exceptions.c
+++ b/Zend/zend_exceptions.c
@@ -144,6 +144,53 @@ ZEND_API zend_class_entry *zend_exception_get_default(void)
return default_exception_ptr;
}
+ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...)
+{
+ zval *ex;
+ va_list arg;
+
+#ifdef _GNU_SOURCE
+ char *message;
+ va_start(arg, format);
+ vasprintf(message, format, arg);
+ va_end(arg);
+#else
+ char message[1024];
+ va_start(arg, format);
+ vsnprintf(message, sizeof(message), format, arg);
+ va_end(arg);
+#endif
+
+ MAKE_STD_ZVAL(ex);
+ if (exception_ce) {
+ if (!instanceof_function(exception_ce, default_exception_ptr TSRMLS_CC)) {
+ zend_error(E_NOTICE, "Exceptions must be derived from exception");
+ exception_ce = default_exception_ptr;
+ }
+ } else {
+ exception_ce = default_exception_ptr;
+ }
+ object_init_ex(ex, exception_ce);
+
+
+ if (message) {
+ zend_update_property_string(exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
+ }
+ if (code) {
+ zend_update_property_long(exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
+ }
+
+#ifdef _GNU_SOURCE
+ free(message);
+#endif
+
+ EG(exception) = ex;
+}
+
+/* at the moment we can't use zend_throw_exception_ex because we don't have a protable
+ * vsnprintf that tells us the number of characters needed nor do we have spprintf from
+ * php or asprintf from glibc always.
+ */
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC)
{
zval *ex;
diff --git a/Zend/zend_exceptions.h b/Zend/zend_exceptions.h
index 0e1d70f26a..9b6e17661c 100644
--- a/Zend/zend_exceptions.h
+++ b/Zend/zend_exceptions.h
@@ -30,6 +30,7 @@ ZEND_API void zend_register_default_classes(TSRMLS_D);
/* exception_ce NULL or zend_exception_get_default() or a derived class
* message NULL or the message of the exception */
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC);
+ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...);
/* show an exception using zend_error(E_ERROR,...) */
ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);