Skip to content

Commit 339ddb5

Browse files
committed
PHPC-1510: Expose error labels from write concern errors
1 parent d59e00e commit 339ddb5

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

php_phongo.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,34 +193,56 @@ void phongo_throw_exception(php_phongo_error_domain_t domain, const char* format
193193
va_end(args);
194194
}
195195

196+
static int phongo_exception_append_error_labels(zval* labels, const bson_iter_t* iter)
197+
{
198+
bson_iter_t error_labels;
199+
uint32_t label_count = 0;
200+
201+
bson_iter_recurse(iter, &error_labels);
202+
while (bson_iter_next(&error_labels)) {
203+
if (BSON_ITER_HOLDS_UTF8(&error_labels)) {
204+
const char* error_label;
205+
uint32_t error_label_len;
206+
207+
error_label = bson_iter_utf8(&error_labels, &error_label_len);
208+
ADD_NEXT_INDEX_STRINGL(labels, error_label, error_label_len);
209+
label_count++;
210+
}
211+
}
212+
213+
return label_count;
214+
}
215+
196216
static void phongo_exception_add_error_labels(const bson_t* reply)
197217
{
198218
bson_iter_t iter;
219+
zval labels;
220+
uint32_t label_count = 0;
199221

200222
if (!reply) {
201223
return;
202224
}
203225

204-
if (bson_iter_init_find(&iter, reply, "errorLabels")) {
205-
bson_iter_t error_labels;
206-
zval labels;
226+
array_init(&labels);
207227

208-
array_init(&labels);
228+
if (bson_iter_init_find(&iter, reply, "errorLabels")) {
229+
label_count += phongo_exception_append_error_labels(&labels, &iter);
230+
}
209231

210-
bson_iter_recurse(&iter, &error_labels);
211-
while (bson_iter_next(&error_labels)) {
212-
if (BSON_ITER_HOLDS_UTF8(&error_labels)) {
213-
const char* error_label;
214-
uint32_t error_label_len;
232+
if (bson_iter_init_find(&iter, reply, "writeConcernError")) {
233+
bson_iter_t write_concern_error_iter;
215234

216-
error_label = bson_iter_utf8(&error_labels, &error_label_len);
217-
ADD_NEXT_INDEX_STRINGL(&labels, error_label, error_label_len);
218-
}
235+
bson_iter_recurse(&iter, &write_concern_error_iter);
236+
if (bson_iter_find(&write_concern_error_iter, "errorLabels")) {
237+
label_count += phongo_exception_append_error_labels(&labels, &write_concern_error_iter);
219238
}
239+
}
220240

241+
if (label_count > 0) {
221242
phongo_add_exception_prop(ZEND_STRL("errorLabels"), &labels);
222-
zval_ptr_dtor(&labels);
223243
}
244+
245+
zval_ptr_dtor(&labels);
224246
}
225247

226248
void phongo_throw_exception_from_bson_error_t_and_reply(bson_error_t* error, const bson_t* reply)

0 commit comments

Comments
 (0)