forked from php-zookeeper/php-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_zookeeper_exceptions.c
114 lines (96 loc) · 3.78 KB
/
php_zookeeper_exceptions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
+----------------------------------------------------------------------+
| Copyright (c) 2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Ryan Uber <[email protected]> |
| Timandes White <[email protected]> |
+----------------------------------------------------------------------+
*/
#include <php.h>
#ifdef ZTS
#include "TSRM.h"
#endif
#include "php_zookeeper.h"
#include "php_zookeeper_exceptions.h"
#include <Zend/zend_exceptions.h>
static zend_class_entry *zk_base_exception;
static zend_class_entry *zk_optimeout_exception;
static zend_class_entry *zk_connection_exception;
static zend_class_entry *zk_marshalling_exception;
static zend_class_entry *zk_auth_exception;
static zend_class_entry *zk_session_exception;
static zend_class_entry *zk_nonode_exception;
void php_zk_register_exceptions(TSRMLS_D)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "ZookeeperException", NULL);
zk_base_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C));
INIT_CLASS_ENTRY(ce, "ZookeeperOperationTimeoutException", NULL);
zk_optimeout_exception = zend_register_internal_class_ex(&ce, zk_base_exception);
INIT_CLASS_ENTRY(ce, "ZookeeperConnectionException", NULL);
zk_connection_exception = zend_register_internal_class_ex(&ce, zk_base_exception);
INIT_CLASS_ENTRY(ce, "ZookeeperMarshallingException", NULL);
zk_marshalling_exception = zend_register_internal_class_ex(&ce, zk_base_exception);
INIT_CLASS_ENTRY(ce, "ZookeeperAuthenticationException", NULL);
zk_auth_exception = zend_register_internal_class_ex(&ce, zk_base_exception);
INIT_CLASS_ENTRY(ce, "ZookeeperSessionException", NULL);
zk_session_exception = zend_register_internal_class_ex(&ce, zk_base_exception);
INIT_CLASS_ENTRY(ce, "ZookeeperNoNodeException", NULL);
zk_nonode_exception = zend_register_internal_class_ex(&ce, zk_base_exception);
}
zend_class_entry * php_zk_get_exception_with_message(zend_class_entry *ce, char *message TSRMLS_DC)
{
zend_declare_property_string(ce, "message", strlen("message"), message, ZEND_ACC_PUBLIC TSRMLS_CC);
return ce;
}
void php_zk_throw_exception(int zk_status TSRMLS_DC)
{
zend_class_entry *ce;
char *message = NULL;
switch(zk_status) {
case PHPZK_INITIALIZATION_FAILURE:
ce = zk_connection_exception;
message = "Failed to initialize ZooKeeper C Client, perhaps not enough memory.";
break;
case PHPZK_CONNECT_NOT_CALLED:
ce = zk_connection_exception;
message = "Zookeeper->connect() was not called";
break;
case ZCONNECTIONLOSS:
ce = zk_connection_exception;
break;
case ZOPERATIONTIMEOUT:
ce = zk_optimeout_exception;
break;
case ZMARSHALLINGERROR:
ce = zk_marshalling_exception;
break;
case ZNOAUTH:
case ZAUTHFAILED:
ce = zk_auth_exception;
break;
case ZSESSIONEXPIRED:
case ZSESSIONMOVED:
ce = zk_session_exception;
break;
case ZNONODE:
ce = zk_nonode_exception;
break;
default:
ce = zk_base_exception;
break;
}
if (!message) {
message = (char *)zerror(zk_status);
}
zend_throw_exception_ex(ce, zk_status TSRMLS_CC, "%s", message);
return;
}