Skip to content

Commit 2aa41c0

Browse files
committed
PHON-98: Implement and properly throw MongoDB\ConnectionException
1 parent 1606f51 commit 2aa41c0

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ if test "$PHONGO" != "no"; then
172172
PHONGO_MONGODB_EXCEPTIONS="\
173173
src/MongoDB/Exception.c \
174174
src/MongoDB/RuntimeException.c \
175+
src/MongoDB/ConnectionException.c \
175176
src/MongoDB/WriteException.c \
176177
";
177178

php_phongo.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ zend_class_entry* phongo_exception_from_phongo_domain(php_phongo_error_domain_t
8181
case PHONGO_ERROR_WRITE_FAILED:
8282
return php_phongo_writeexception_ce;
8383
case PHONGO_ERROR_CONNECTION_FAILED:
84-
/* FIXME: Add ConnectionException */
85-
return php_phongo_writeexception_ce;
84+
return php_phongo_connectionexception_ce;
8685
}
8786

8887
mongoc_log(MONGOC_LOG_LEVEL_ERROR, MONGOC_LOG_DOMAIN, "Resolving unknown exception domain!!!");
@@ -97,6 +96,7 @@ zend_class_entry* phongo_exception_from_mongoc_domain(uint32_t /* mongoc_error_d
9796
case MONGOC_ERROR_STREAM_SOCKET:
9897
case MONGOC_ERROR_STREAM_CONNECT:
9998
case MONGOC_ERROR_STREAM_NOT_ESTABLISHED:
99+
return php_phongo_connectionexception_ce;
100100
case MONGOC_ERROR_CLIENT_NOT_READY:
101101
case MONGOC_ERROR_CLIENT_TOO_BIG:
102102
case MONGOC_ERROR_CLIENT_TOO_SMALL:
@@ -561,10 +561,13 @@ bool phongo_execute_write(mongoc_client_t *client, char *namespace, mongoc_bulk_
561561
hint = mongoc_bulk_operation_execute(batch, &reply, &error);
562562

563563
if (!hint) {
564-
zval *e = phongo_throw_exception(PHONGO_ERROR_WRITE_FAILED TSRMLS_CC, "%s", error.message);
564+
/* If no exception has been thrown already, for example connection exception */
565+
if (!EG(exception)) {
566+
zval *e = phongo_throw_exception(PHONGO_ERROR_WRITE_FAILED TSRMLS_CC, "%s", error.message);
565567

566-
if (Z_OBJCE_P(e) == php_phongo_writeexception_ce) {
567-
phongo_writeexception_init(e, &reply, hint TSRMLS_CC);
568+
if (Z_OBJCE_P(e) == php_phongo_writeexception_ce) {
569+
phongo_writeexception_init(e, &reply, hint TSRMLS_CC);
570+
}
568571
}
569572
bson_destroy(&reply);
570573

@@ -1399,6 +1402,7 @@ PHP_MINIT_FUNCTION(phongo)
13991402

14001403
PHP_MINIT(Exception)(INIT_FUNC_ARGS_PASSTHRU);
14011404
PHP_MINIT(RuntimeException)(INIT_FUNC_ARGS_PASSTHRU);
1405+
PHP_MINIT(ConnectionException)(INIT_FUNC_ARGS_PASSTHRU);
14021406
PHP_MINIT(WriteException)(INIT_FUNC_ARGS_PASSTHRU);
14031407

14041408
PHP_MINIT(Type)(INIT_FUNC_ARGS_PASSTHRU);

php_phongo_classes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ extern PHONGO_API zend_class_entry *php_phongo_writeresult_ce;
190190

191191
extern PHONGO_API zend_class_entry *php_phongo_exception_ce;
192192
extern PHONGO_API zend_class_entry *php_phongo_runtimeexception_ce;
193+
extern PHONGO_API zend_class_entry *php_phongo_connectionexception_ce;
193194
extern PHONGO_API zend_class_entry *php_phongo_writeexception_ce;
194195

195196
extern PHONGO_API zend_class_entry *php_phongo_type_ce;
@@ -223,6 +224,7 @@ PHP_MINIT_FUNCTION(WriteResult);
223224

224225
PHP_MINIT_FUNCTION(Exception);
225226
PHP_MINIT_FUNCTION(RuntimeException);
227+
PHP_MINIT_FUNCTION(ConnectionException);
226228
PHP_MINIT_FUNCTION(WriteException);
227229

228230
PHP_MINIT_FUNCTION(Type);

src/MongoDB/ConnectionException.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_connectionexception_ce;
47+
48+
/* {{{ MongoDB\ConnectionException */
49+
50+
static zend_function_entry php_phongo_connectionexception_me[] = {
51+
PHP_FE_END
52+
};
53+
54+
/* }}} */
55+
56+
57+
/* {{{ PHP_MINIT_FUNCTION */
58+
PHP_MINIT_FUNCTION(ConnectionException)
59+
{
60+
(void)type;
61+
(void)module_number;
62+
zend_class_entry ce;
63+
64+
INIT_NS_CLASS_ENTRY(ce, "MongoDB", "ConnectionException", php_phongo_connectionexception_me);
65+
php_phongo_connectionexception_ce = zend_register_internal_class_ex(&ce, php_phongo_runtimeexception_ce, NULL TSRMLS_CC);
66+
67+
return SUCCESS;
68+
}
69+
/* }}} */
70+
71+
72+
73+
/*
74+
* Local variables:
75+
* tab-width: 4
76+
* c-basic-offset: 4
77+
* End:
78+
* vim600: noet sw=4 ts=4 fdm=marker
79+
* vim<600: noet sw=4 ts=4
80+
*/

0 commit comments

Comments
 (0)