Skip to content

Commit 1252a6e

Browse files
committed
PHPC-1026: Log warnings instead of throwing for unsupported BSON types
1 parent 3314b93 commit 1252a6e

File tree

3 files changed

+103
-52
lines changed

3 files changed

+103
-52
lines changed

src/bson.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static bool php_phongo_bson_visit_array(const bson_iter_t *iter ARG_UNUSED, cons
4747

4848
static void php_phongo_bson_visit_corrupt(const bson_iter_t *iter ARG_UNUSED, void *data ARG_UNUSED) /* {{{ */
4949
{
50-
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "Corrupt BSON data detected!");
50+
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Corrupt BSON data detected!");
5151
} /* }}} */
5252

5353
static void php_phongo_bson_visit_unsupported_type(const bson_iter_t *iter ARG_UNUSED, const char *key, uint32_t v_type_code, void *data ARG_UNUSED) /* {{{ */
@@ -131,8 +131,15 @@ static bool php_phongo_bson_visit_binary(const bson_iter_t *iter ARG_UNUSED, con
131131

132132
static bool php_phongo_bson_visit_undefined(const bson_iter_t *iter, const char *key, void *data) /* {{{ */
133133
{
134-
TSRMLS_FETCH();
135-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Detected unsupported BSON type 0x06 (undefined) for fieldname \"%s\"", key);
134+
zval *retval = PHONGO_BSON_STATE_ZCHILD(data);
135+
136+
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Detected unsupported BSON type 0x06 (undefined) for fieldname \"%s\"", key);
137+
138+
if (((php_phongo_bson_state *)data)->is_visiting_array) {
139+
add_next_index_null(retval);
140+
} else {
141+
add_assoc_null(retval, key);
142+
}
136143

137144
return false;
138145
} /* }}} */
@@ -285,8 +292,15 @@ static bool php_phongo_bson_visit_regex(const bson_iter_t *iter ARG_UNUSED, cons
285292

286293
static bool php_phongo_bson_visit_symbol(const bson_iter_t *iter, const char *key, size_t symbol_len, const char *symbol, void *data) /* {{{ */
287294
{
288-
TSRMLS_FETCH();
289-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Detected unsupported BSON type 0x0E (symbol) for fieldname \"%s\"", key);
295+
zval *retval = PHONGO_BSON_STATE_ZCHILD(data);
296+
297+
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Detected unsupported BSON type 0x0E (symbol) for fieldname \"%s\"", key);
298+
299+
if (((php_phongo_bson_state *)data)->is_visiting_array) {
300+
add_next_index_null(retval);
301+
} else {
302+
add_assoc_null(retval, key);
303+
}
290304

291305
return false;
292306
} /* }}} */
@@ -323,8 +337,15 @@ static bool php_phongo_bson_visit_code(const bson_iter_t *iter ARG_UNUSED, const
323337

324338
static bool php_phongo_bson_visit_dbpointer(const bson_iter_t *iter, const char *key, size_t collection_len, const char *collection, const bson_oid_t *oid, void *data) /* {{{ */
325339
{
326-
TSRMLS_FETCH();
327-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Detected unsupported BSON type 0x0C (DBPointer) for fieldname \"%s\"", key);
340+
zval *retval = PHONGO_BSON_STATE_ZCHILD(data);
341+
342+
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Detected unsupported BSON type 0x0C (DBPointer) for fieldname \"%s\"", key);
343+
344+
if (((php_phongo_bson_state *)data)->is_visiting_array) {
345+
add_next_index_null(retval);
346+
} else {
347+
add_assoc_null(retval, key);
348+
}
328349

329350
return false;
330351
} /* }}} */

tests/bson/bson-toPHP_error-005.phpt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,29 @@ $tests = [
1111
pack('VCa*xVa*xx', 18, 0x0E, 'foo', 4, 'bar'), // symbol
1212
];
1313

14+
ini_set("mongodb.debug", "stdout");
1415
foreach ($tests as $bson) {
15-
echo throws(function() use ($bson) {
16-
var_dump(toPHP($bson));
17-
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
16+
var_dump(toPHP($bson));
1817
}
18+
ini_set("mongodb.debug", "off");
1919

2020
?>
2121
===DONE===
2222
<?php exit(0); ?>
23-
--EXPECT--
24-
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
25-
Detected unsupported BSON type 0x06 (undefined) for fieldname "foo"
26-
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
27-
Detected unsupported BSON type 0x0C (DBPointer) for fieldname "foo"
28-
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
29-
Detected unsupported BSON type 0x0E (symbol) for fieldname "foo"
23+
--EXPECTF--
24+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "foo"
25+
object(stdClass)#%d (%d) {
26+
["foo"]=>
27+
NULL
28+
}
29+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x0C (DBPointer) for fieldname "foo"
30+
object(stdClass)#%d (%d) {
31+
["foo"]=>
32+
NULL
33+
}
34+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x0E (symbol) for fieldname "foo"
35+
object(stdClass)#%d (%d) {
36+
["foo"]=>
37+
NULL
38+
}
3039
===DONE===

tests/bson/bson-toPHP_error-006.phpt

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,66 @@ $tests = [
1818
pack('VCa*xVCa*xxCa*xVCa*xxx', 31, 0x03, 'e1', 9, 0x06, 'u1', 0x03, 'e2', 9, 0x06, 'u2'),
1919
];
2020

21+
ini_set("mongodb.debug", "stdout");
2122
foreach ($tests as $bson) {
22-
try {
23-
var_dump(toPHP($bson));
24-
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
25-
do {
26-
printf("OK: %s\n%s\n", get_class($e), $e->getMessage());
27-
} while ($e = $e->getPrevious());
28-
}
29-
30-
echo "\n";
23+
var_dump(toPHP($bson));
3124
}
25+
ini_set("mongodb.debug", "off");
3226

3327
?>
3428
===DONE===
3529
<?php exit(0); ?>
36-
--EXPECT--
37-
OK: MongoDB\Driver\Exception\UnexpectedValueException
38-
Detected unsupported BSON type 0x06 (undefined) for fieldname "u2"
39-
OK: MongoDB\Driver\Exception\UnexpectedValueException
40-
Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
41-
42-
OK: MongoDB\Driver\Exception\UnexpectedValueException
43-
Detected unsupported BSON type 0x0E (symbol) for fieldname "s1"
44-
OK: MongoDB\Driver\Exception\UnexpectedValueException
45-
Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
46-
47-
OK: MongoDB\Driver\Exception\UnexpectedValueException
48-
Detected unsupported BSON type 0x06 (undefined) for fieldname "u2"
49-
OK: MongoDB\Driver\Exception\UnexpectedValueException
50-
Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
51-
52-
OK: MongoDB\Driver\Exception\UnexpectedValueException
53-
Detected unsupported BSON type 0x06 (undefined) for fieldname "u2"
54-
OK: MongoDB\Driver\Exception\UnexpectedValueException
55-
Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
56-
57-
OK: MongoDB\Driver\Exception\UnexpectedValueException
58-
Detected unsupported BSON type 0x06 (undefined) for fieldname "u2"
59-
OK: MongoDB\Driver\Exception\UnexpectedValueException
60-
Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
61-
30+
--EXPECTF--
31+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
32+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "u2"
33+
object(stdClass)#%d (%d) {
34+
["u1"]=>
35+
NULL
36+
["u2"]=>
37+
NULL
38+
}
39+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
40+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x0E (symbol) for fieldname "s1"
41+
object(stdClass)#%d (%d) {
42+
["u1"]=>
43+
NULL
44+
["s1"]=>
45+
NULL
46+
}
47+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
48+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "u2"
49+
object(stdClass)#%d (%d) {
50+
["u1"]=>
51+
NULL
52+
["e1"]=>
53+
object(stdClass)#%d (%d) {
54+
["u2"]=>
55+
NULL
56+
}
57+
}
58+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
59+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "u2"
60+
object(stdClass)#%d (%d) {
61+
["e1"]=>
62+
object(stdClass)#%d (%d) {
63+
["u1"]=>
64+
NULL
65+
}
66+
["u2"]=>
67+
NULL
68+
}
69+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "u1"
70+
[%s] PHONGO-BSON: WARNING > Detected unsupported BSON type 0x06 (undefined) for fieldname "u2"
71+
object(stdClass)#3 (%d) {
72+
["e1"]=>
73+
object(stdClass)#%d (%d) {
74+
["u1"]=>
75+
NULL
76+
}
77+
["e2"]=>
78+
object(stdClass)#%d (%d) {
79+
["u2"]=>
80+
NULL
81+
}
82+
}
6283
===DONE===

0 commit comments

Comments
 (0)