Skip to content

Commit 3059c9e

Browse files
authored
PCBC-938 Override exception constructor in PHP Extension (#117)
* Added CBException constructor with context array * run clang-format
1 parent 4d2f26f commit 3059c9e

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/php_couchbase.cxx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ PHP_RSHUTDOWN_FUNCTION(couchbase)
5757
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(ai_Exception_getContext, IS_ARRAY, 0)
5858
ZEND_END_ARG_INFO()
5959

60+
ZEND_BEGIN_ARG_INFO_EX(ai_Exception___construct, 0, 0, 0)
61+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 0, "\"\"")
62+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, code, IS_LONG, 0, "0")
63+
ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, previous, Throwable, 1, "null")
64+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, context, IS_ARRAY, 1, "null")
65+
ZEND_END_ARG_INFO()
66+
6067
PHP_METHOD(CouchbaseException, getContext)
6168
{
6269
if (zend_parse_parameters_none_throw() == FAILURE) {
@@ -68,6 +75,40 @@ PHP_METHOD(CouchbaseException, getContext)
6875
ZVAL_COPY_DEREF(return_value, prop);
6976
}
7077

78+
PHP_METHOD(CouchbaseException, __construct)
79+
{
80+
zend_string* message = NULL;
81+
zend_long code = 0;
82+
zval tmp, *object, *previous = NULL, *context = NULL;
83+
84+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!a", &message, &code, &previous, zend_ce_throwable, &context) == FAILURE) {
85+
RETURN_THROWS();
86+
}
87+
88+
object = ZEND_THIS;
89+
90+
if (message) {
91+
ZVAL_STR_COPY(&tmp, message);
92+
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
93+
zval_ptr_dtor(&tmp);
94+
}
95+
96+
if (code) {
97+
ZVAL_LONG(&tmp, code);
98+
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
99+
}
100+
101+
if (previous) {
102+
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
103+
}
104+
105+
if (context) {
106+
zend_string* property_context_name = zend_string_init(ZEND_STRL("context"), 1);
107+
zend_update_property_ex(couchbase::php::couchbase_exception(), Z_OBJ_P(object), property_context_name, context);
108+
zend_string_release(property_context_name);
109+
}
110+
}
111+
71112
PHP_RINIT_FUNCTION(couchbase)
72113
{
73114
if (!COUCHBASE_G(initialized)) {
@@ -80,6 +121,7 @@ PHP_RINIT_FUNCTION(couchbase)
80121
// clang-format off
81122
static const zend_function_entry exception_functions[] = {
82123
PHP_ME(CouchbaseException, getContext, ai_Exception_getContext, ZEND_ACC_PUBLIC)
124+
PHP_ME(CouchbaseException, __construct, ai_Exception___construct, ZEND_ACC_PUBLIC)
83125
PHP_FE_END
84126
};
85127

@@ -1756,7 +1798,7 @@ PHP_FUNCTION(collectionDrop)
17561798
if (auto e = handle->collection_drop(return_value, bucket_name, collection_spec, options); e.ec) {
17571799
couchbase_throw_exception(e);
17581800
RETURN_THROWS();
1759-
}
1801+
}
17601802
}
17611803

17621804
static inline couchbase::php::transactions_resource*

0 commit comments

Comments
 (0)