Skip to content

Commit 1606f51

Browse files
committed
PHON-97: Add MongoDB\RuntimeException
This exception extends SPL RuntimeException - and implements MongoDB\Exception This makes it easy to catch "all MongoDB exceptions" and/or RuntimeExceptions
1 parent f1031fe commit 1606f51

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

config.m4

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

php_phongo.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ zend_class_entry* phongo_exception_from_phongo_domain(php_phongo_error_domain_t
7575
case PHONGO_ERROR_INVALID_ARGUMENT:
7676
return spl_ce_InvalidArgumentException;
7777
case PHONGO_ERROR_RUNTIME:
78-
return spl_ce_RuntimeException;
78+
return php_phongo_runtimeexception_ce;
7979
case PHONGO_ERROR_MONGOC_FAILED:
80-
return spl_ce_RuntimeException;
80+
return php_phongo_runtimeexception_ce;
8181
case PHONGO_ERROR_WRITE_FAILED:
8282
return php_phongo_writeexception_ce;
8383
case PHONGO_ERROR_CONNECTION_FAILED:
@@ -1398,6 +1398,7 @@ PHP_MINIT_FUNCTION(phongo)
13981398
PHP_MINIT(WriteResult)(INIT_FUNC_ARGS_PASSTHRU);
13991399

14001400
PHP_MINIT(Exception)(INIT_FUNC_ARGS_PASSTHRU);
1401+
PHP_MINIT(RuntimeException)(INIT_FUNC_ARGS_PASSTHRU);
14011402
PHP_MINIT(WriteException)(INIT_FUNC_ARGS_PASSTHRU);
14021403

14031404
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
@@ -189,6 +189,7 @@ extern PHONGO_API zend_class_entry *php_phongo_writeerror_ce;
189189
extern PHONGO_API zend_class_entry *php_phongo_writeresult_ce;
190190

191191
extern PHONGO_API zend_class_entry *php_phongo_exception_ce;
192+
extern PHONGO_API zend_class_entry *php_phongo_runtimeexception_ce;
192193
extern PHONGO_API zend_class_entry *php_phongo_writeexception_ce;
193194

194195
extern PHONGO_API zend_class_entry *php_phongo_type_ce;
@@ -221,6 +222,7 @@ PHP_MINIT_FUNCTION(WriteError);
221222
PHP_MINIT_FUNCTION(WriteResult);
222223

223224
PHP_MINIT_FUNCTION(Exception);
225+
PHP_MINIT_FUNCTION(RuntimeException);
224226
PHP_MINIT_FUNCTION(WriteException);
225227

226228
PHP_MINIT_FUNCTION(Type);

src/MongoDB/RuntimeException.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_runtimeexception_ce;
47+
48+
/* {{{ MongoDB\RuntimeException */
49+
50+
static zend_function_entry php_phongo_runtimeexception_me[] = {
51+
PHP_FE_END
52+
};
53+
54+
/* }}} */
55+
56+
57+
/* {{{ PHP_MINIT_FUNCTION */
58+
PHP_MINIT_FUNCTION(RuntimeException)
59+
{
60+
(void)type;
61+
(void)module_number;
62+
zend_class_entry ce;
63+
64+
INIT_NS_CLASS_ENTRY(ce, "MongoDB", "RuntimeException", php_phongo_runtimeexception_me);
65+
php_phongo_runtimeexception_ce = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException, NULL TSRMLS_CC);
66+
zend_class_implements(php_phongo_runtimeexception_ce TSRMLS_CC, 1, php_phongo_exception_ce);
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ PHP_MINIT_FUNCTION(WriteException)
9292
zend_class_entry ce;
9393

9494
INIT_NS_CLASS_ENTRY(ce, "MongoDB", "WriteException", php_phongo_writeexception_me);
95-
php_phongo_writeexception_ce = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException, NULL TSRMLS_CC);
95+
php_phongo_writeexception_ce = zend_register_internal_class_ex(&ce, php_phongo_runtimeexception_ce, NULL TSRMLS_CC);
9696
php_phongo_writeexception_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
9797

9898
zend_declare_property_null(php_phongo_writeexception_ce, ZEND_STRL("writeResult"), ZEND_ACC_PRIVATE TSRMLS_CC);

0 commit comments

Comments
 (0)