Skip to content

Commit 95774b1

Browse files
committed
PHPC-101: throw MongoDB\DuplicateKeyException
1 parent 499dc0e commit 95774b1

File tree

8 files changed

+95
-16
lines changed

8 files changed

+95
-16
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ if test "$PHONGO" != "no"; then
174174
src/MongoDB/RuntimeException.c \
175175
src/MongoDB/ConnectionException.c \
176176
src/MongoDB/SSLConnectionException.c \
177+
src/MongoDB/DuplicateKeyException.c \
177178
src/MongoDB/WriteException.c \
178179
";
179180

php_phongo.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ zend_class_entry* phongo_exception_from_phongo_domain(php_phongo_error_domain_t
9090
zend_class_entry* phongo_exception_from_mongoc_domain(uint32_t /* mongoc_error_domain_t */ domain, uint32_t /* mongoc_error_code_t */ code)
9191
{
9292
switch(code) {
93+
case 11000: /* Duplicate key */
94+
return php_phongo_duplicatekeyexception_ce;
9395
case MONGOC_ERROR_STREAM_INVALID_TYPE:
9496
case MONGOC_ERROR_STREAM_INVALID_STATE:
9597
case MONGOC_ERROR_STREAM_NAME_RESOLUTION:
@@ -563,9 +565,9 @@ bool phongo_execute_write(mongoc_client_t *client, char *namespace, mongoc_bulk_
563565
if (!hint) {
564566
/* If no exception has been thrown already, for example connection exception */
565567
if (!EG(exception)) {
566-
zval *e = phongo_throw_exception(PHONGO_ERROR_WRITE_FAILED TSRMLS_CC, "%s", error.message);
568+
zval *e = phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
567569

568-
if (Z_OBJCE_P(e) == php_phongo_writeexception_ce) {
570+
if (instanceof_function(Z_OBJCE_P(e), php_phongo_writeexception_ce TSRMLS_CC)) {
569571
phongo_writeexception_init(e, &reply, hint TSRMLS_CC);
570572
}
571573
}
@@ -1426,6 +1428,7 @@ PHP_MINIT_FUNCTION(phongo)
14261428
PHP_MINIT(ConnectionException)(INIT_FUNC_ARGS_PASSTHRU);
14271429
PHP_MINIT(SSLConnectionException)(INIT_FUNC_ARGS_PASSTHRU);
14281430
PHP_MINIT(WriteException)(INIT_FUNC_ARGS_PASSTHRU);
1431+
PHP_MINIT(DuplicateKeyException)(INIT_FUNC_ARGS_PASSTHRU);
14291432

14301433
PHP_MINIT(Type)(INIT_FUNC_ARGS_PASSTHRU);
14311434
PHP_MINIT(Binary)(INIT_FUNC_ARGS_PASSTHRU);

php_phongo_classes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ extern PHONGO_API zend_class_entry *php_phongo_exception_ce;
192192
extern PHONGO_API zend_class_entry *php_phongo_runtimeexception_ce;
193193
extern PHONGO_API zend_class_entry *php_phongo_connectionexception_ce;
194194
extern PHONGO_API zend_class_entry *php_phongo_sslconnectionexception_ce;
195+
extern PHONGO_API zend_class_entry *php_phongo_duplicatekeyexception_ce;
195196
extern PHONGO_API zend_class_entry *php_phongo_writeexception_ce;
196197

197198
extern PHONGO_API zend_class_entry *php_phongo_type_ce;
@@ -227,6 +228,7 @@ PHP_MINIT_FUNCTION(Exception);
227228
PHP_MINIT_FUNCTION(RuntimeException);
228229
PHP_MINIT_FUNCTION(ConnectionException);
229230
PHP_MINIT_FUNCTION(SSLConnectionException);
231+
PHP_MINIT_FUNCTION(DuplicateKeyException);
230232
PHP_MINIT_FUNCTION(WriteException);
231233

232234
PHP_MINIT_FUNCTION(Type);

src/MongoDB/DuplicateKeyException.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
+---------------------------------------------------------------------------+
3+
| PHP Driver for MongoDB |
4+
+---------------------------------------------------------------------------+
5+
| Copyright 2013-2014 MongoDB, Inc. |
6+
| |
7+
| Licensed under the Apache License, Version 2.0 (the "License"); |
8+
| you may not use this file except in compliance with the License. |
9+
| You may obtain a copy of the License at |
10+
| |
11+
| http://www.apache.org/licenses/LICENSE-2.0 |
12+
| |
13+
| Unless required by applicable law or agreed to in writing, software |
14+
| distributed under the License is distributed on an "AS IS" BASIS, |
15+
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16+
| See the License for the specific language governing permissions and |
17+
| limitations under the License. |
18+
+---------------------------------------------------------------------------+
19+
| Copyright (c) 2014, MongoDB, Inc. |
20+
+---------------------------------------------------------------------------+
21+
*/
22+
23+
#ifdef HAVE_CONFIG_H
24+
# include "config.h"
25+
#endif
26+
27+
/* External libs */
28+
#include <bson.h>
29+
#include <mongoc.h>
30+
31+
/* PHP Core stuff */
32+
#include <php.h>
33+
#include <php_ini.h>
34+
#include <ext/standard/info.h>
35+
#include <Zend/zend_interfaces.h>
36+
#include <ext/spl/spl_iterators.h>
37+
/* Our Compatability header */
38+
#include "php_compat_53.h"
39+
40+
/* Our stuffz */
41+
#include "php_phongo.h"
42+
#include "php_bson.h"
43+
#include <ext/spl/spl_exceptions.h>
44+
45+
46+
PHONGO_API zend_class_entry *php_phongo_duplicatekeyexception_ce;
47+
48+
/* {{{ MongoDB\DuplicateKeyException */
49+
50+
static zend_function_entry php_phongo_duplicatekeyexception_me[] = {
51+
PHP_FE_END
52+
};
53+
54+
/* }}} */
55+
56+
57+
/* {{{ PHP_MINIT_FUNCTION */
58+
PHP_MINIT_FUNCTION(DuplicateKeyException)
59+
{
60+
(void)type;
61+
(void)module_number;
62+
zend_class_entry ce;
63+
64+
INIT_NS_CLASS_ENTRY(ce, "MongoDB", "DuplicateKeyException", php_phongo_duplicatekeyexception_me);
65+
php_phongo_duplicatekeyexception_ce = zend_register_internal_class_ex(&ce, php_phongo_writeexception_ce, NULL TSRMLS_CC);
66+
php_phongo_duplicatekeyexception_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
67+
68+
return SUCCESS;
69+
}
70+
/* }}} */
71+
72+
73+
74+
/*
75+
* Local variables:
76+
* tab-width: 4
77+
* c-basic-offset: 4
78+
* End:
79+
* vim600: noet sw=4 ts=4 fdm=marker
80+
* vim<600: noet sw=4 ts=4
81+
*/

src/MongoDB/WriteException.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ PHP_MINIT_FUNCTION(WriteException)
9393

9494
INIT_NS_CLASS_ENTRY(ce, "MongoDB", "WriteException", php_phongo_writeexception_me);
9595
php_phongo_writeexception_ce = zend_register_internal_class_ex(&ce, php_phongo_runtimeexception_ce, NULL TSRMLS_CC);
96-
php_phongo_writeexception_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
9796

98-
zend_declare_property_null(php_phongo_writeexception_ce, ZEND_STRL("writeResult"), ZEND_ACC_PRIVATE TSRMLS_CC);
97+
zend_declare_property_null(php_phongo_writeexception_ce, ZEND_STRL("writeResult"), ZEND_ACC_PROTECTED TSRMLS_CC);
9998

10099
return SUCCESS;
101100
}

tests/standalone/manager-executeWriteBatch-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var_dump(iterator_to_array($cursor));
3434
<?php exit(0); ?>
3535
--EXPECTF--
3636
WriteException.message: %s
37-
WriteException.code: 0
37+
WriteException.code: 11000
3838

3939
===> WriteResult
4040
server: %s:%d

tests/standalone/manager-executeWriteBatch-003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var_dump(iterator_to_array($cursor));
3434
<?php exit(0); ?>
3535
--EXPECTF--
3636
WriteException.message: %s
37-
WriteException.code: 0
37+
WriteException.code: 11000
3838

3939
===> WriteResult
4040
server: %s:%d

tests/standalone/write-error-001.phpt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,13 @@ $hannes_id = $insertBatch->insert($hannes);
1717
$w = 2;
1818
$wtimeout = 1000;
1919
$writeConcern = new \MongoDB\WriteConcern($w, $wtimeout);
20-
try {
20+
throws(function() use($insertBatch, $writeConcern, $manager) {
2121
$result = $manager->executeWriteBatch("db.collection", $insertBatch, $writeConcern);
22-
} catch(MongoDB\WriteException $e) {
23-
printWriteResult($e->getWriteResult());
24-
}
22+
}, "MongoDB\ConnectionException");
2523

2624
?>
2725
===DONE===
2826
<?php exit(0); ?>
2927
--EXPECTF--
30-
server: %s:%d
31-
insertedCount: 0
32-
matchedCount: 0
33-
modifiedCount: 0
34-
upsertedCount: 0
35-
deletedCount: 0
28+
OK: Got MongoDB\ConnectionException
3629
===DONE===

0 commit comments

Comments
 (0)