Skip to content

Commit d8506fa

Browse files
committed
PHPC-154: WriteError can have info field
1 parent 6ca5cdb commit d8506fa

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

php_phongo.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,22 @@ zend_bool phongo_writeerror_init(zval *return_value, bson_t *bson TSRMLS_DC) /*
309309
if (bson_iter_init_find(&iter, bson, "errmsg") && BSON_ITER_HOLDS_UTF8(&iter)) {
310310
writeerror->message = bson_iter_dup_utf8(&iter, NULL);
311311
}
312+
if (bson_iter_init_find(&iter, bson, "errInfo")) {
313+
bson_t info;
314+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
315+
316+
MAKE_STD_ZVAL(writeerror->info);
317+
state.zchild = writeerror->info;
318+
319+
bson_init(&info);
320+
bson_append_iter(&info, NULL, 0, &iter);
321+
322+
if (!bson_to_zval(bson_get_data(&info), info.len, &state)) {
323+
zval_ptr_dtor(&writeerror->info);
324+
writeerror->info = NULL;
325+
return false;
326+
}
327+
}
312328
if (bson_iter_init_find(&iter, bson, "index") && BSON_ITER_HOLDS_INT32(&iter)) {
313329
writeerror->index = bson_iter_int32(&iter);
314330
}

php_phongo_classes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ typedef struct {
128128
zend_object std;
129129
int code;
130130
char *message;
131+
zval *info;
131132
uint32_t index;
132133
} php_phongo_writeerror_t;
133134

src/MongoDB/WriteError.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ PHP_METHOD(WriteError, getMessage)
109109
RETURN_STRING(intern->message, 1);
110110
}
111111
/* }}} */
112+
/* {{{ proto mixed WriteError::getInfo()
113+
Returns additional metadata for the error */
114+
PHP_METHOD(WriteError, getInfo)
115+
{
116+
php_phongo_writeerror_t *intern;
117+
zend_error_handling error_handling;
118+
119+
120+
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
121+
intern = (php_phongo_writeerror_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
122+
123+
if (zend_parse_parameters_none() == FAILURE) {
124+
zend_restore_error_handling(&error_handling TSRMLS_CC);
125+
return;
126+
}
127+
zend_restore_error_handling(&error_handling TSRMLS_CC);
128+
129+
130+
if (intern->info) {
131+
RETURN_ZVAL(intern->info, 1, 0);
132+
}
133+
}
134+
/* }}} */
112135

113136
/**
114137
* Value object for a write error (e.g. duplicate key).
@@ -121,6 +144,9 @@ ZEND_END_ARG_INFO();
121144
ZEND_BEGIN_ARG_INFO_EX(ai_WriteError_getIndex, 0, 0, 0)
122145
ZEND_END_ARG_INFO();
123146

147+
ZEND_BEGIN_ARG_INFO_EX(ai_WriteError_getInfo, 0, 0, 0)
148+
ZEND_END_ARG_INFO();
149+
124150
ZEND_BEGIN_ARG_INFO_EX(ai_WriteError_getMessage, 0, 0, 0)
125151
ZEND_END_ARG_INFO();
126152

@@ -129,6 +155,7 @@ static zend_function_entry php_phongo_writeerror_me[] = {
129155
PHP_ME(WriteError, getCode, ai_WriteError_getCode, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
130156
PHP_ME(WriteError, getIndex, ai_WriteError_getIndex, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
131157
PHP_ME(WriteError, getMessage, ai_WriteError_getMessage, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
158+
PHP_ME(WriteError, getInfo, ai_WriteError_getInfo, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
132159
PHP_FE_END
133160
};
134161

@@ -177,6 +204,12 @@ HashTable *php_phongo_writeerror_get_debug_info(zval *object, int *is_temp TSRML
177204
add_assoc_string_ex(&retval, ZEND_STRS("message"), intern->message, 1);
178205
add_assoc_long_ex(&retval, ZEND_STRS("code"), intern->code);
179206
add_assoc_long_ex(&retval, ZEND_STRS("index"), intern->index);
207+
if (intern->info) {
208+
Z_ADDREF_P(intern->info);
209+
add_assoc_zval_ex(&retval, ZEND_STRS("info"), intern->info);
210+
} else {
211+
add_assoc_null_ex(&retval, ZEND_STRS("info"));
212+
}
180213

181214
return Z_ARRVAL(retval);
182215
} /* }}} */

tests/standalone/manager-executeWriteBatch-002.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ object(MongoDB\Driver\WriteError)#%d (%d) {
5050
int(11000)
5151
["index"]=>
5252
int(1)
53+
["info"]=>
54+
NULL
5355
}
5456
writeError[1].message: %s
5557
writeError[1].code: 11000

tests/standalone/manager-executeWriteBatch-003.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ object(MongoDB\Driver\WriteError)#%d (%d) {
5050
int(11000)
5151
["index"]=>
5252
int(1)
53+
["info"]=>
54+
NULL
5355
}
5456
writeError[1].message: %s
5557
writeError[1].code: 11000
@@ -60,6 +62,8 @@ object(MongoDB\Driver\WriteError)#%d (%d) {
6062
int(11000)
6163
["index"]=>
6264
int(3)
65+
["info"]=>
66+
NULL
6367
}
6468
writeError[3].message: %s
6569
writeError[3].code: 11000

0 commit comments

Comments
 (0)