-
Notifications
You must be signed in to change notification settings - Fork 35
/
exceptions.c
227 lines (178 loc) · 9.42 KB
/
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Copyright (c) 2026 - 2024 Gregor Kralik <g.kralik+php-sapnwrfc (at) gmail.com>
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* Author: Gregor Kralik <g.kralik+php-sapnwrfc (at) gmail.com>
*/
#include "php.h"
#include "Zend/zend_exceptions.h"
#include "ext/spl/spl_exceptions.h"
#include "exceptions.h"
#include "string_helper.h"
#include "sapnwrfc.h"
zend_class_entry *sapnwrfc_exception_ce;
zend_class_entry *sapnwrfc_connection_exception_ce;
zend_class_entry *sapnwrfc_function_exception_ce;
typedef struct _sapnwrfc_exception_object {
zend_object *zobj;
} sapnwrfc_exception_object;
typedef struct _sapnwrfc_connection_exception_object {
zend_object zobj;
} sapnwrfc_connection_exception_object;
typedef struct _sapnwrfc_function_exception_object {
zend_object zobj;
} sapnwrfc_functioncall_exception_object;
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_Exception_getErrorInfo, IS_ARRAY, 1)
ZEND_END_ARG_INFO()
static zend_function_entry sapnwrfc_exception_class_functions[] = {
PHP_ME(Exception, getErrorInfo, arginfo_Exception_getErrorInfo, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void sapnwrfc_throw_connection_exception(RFC_ERROR_INFO error_info, char *msg, ...)
{
va_list args;
zend_string *message;
zval info;
zval ex;
zend_error_handling zeh;
va_start(args, msg);
message = vstrpprintf(0, msg, args);
va_end(args);
array_init(&info);
zend_replace_error_handling(EH_THROW, sapnwrfc_connection_exception_ce, &zeh);
object_init_ex(&ex, sapnwrfc_connection_exception_ce);
zend_update_property_string(sapnwrfc_connection_exception_ce, SAPNWRFC_EXC_Z_OBJ_OR_ZVAL(ex), "message", sizeof("message") - 1, ZSTR_VAL(message));
zend_update_property_long(sapnwrfc_connection_exception_ce, SAPNWRFC_EXC_Z_OBJ_OR_ZVAL(ex), "code", sizeof("code") - 1, error_info.code);
// populate errorInfo array
add_assoc_long(&info, "code", error_info.code);
add_assoc_str(&info, "key", sapuc_to_zend_string(error_info.key));
add_assoc_str(&info, "message", sapuc_to_zend_string(error_info.message));
switch(error_info.group) {
case LOGON_FAILURE: // Error message raised when logon fails
case COMMUNICATION_FAILURE: // Problems with the network connection (or backend broke down and killed the connection)
case EXTERNAL_RUNTIME_FAILURE: // Problems in the RFC runtime of the external program (i.e "this" library)
// no additional properties, just the generic one's from above
break;
case ABAP_APPLICATION_FAILURE: // ABAP Exception raised in ABAP function modules
case ABAP_RUNTIME_FAILURE: // ABAP Message raised in ABAP function modules or in ABAP runtime of the backend (e.g Kernel)
case EXTERNAL_APPLICATION_FAILURE: // Problems in the external program (e.g in the external server implementation)
case EXTERNAL_AUTHORIZATION_FAILURE: // Problems raised in the authorization check handler provided by the external server implementation
add_assoc_str(&info, "abapMsgClass", sapuc_to_zend_string(error_info.abapMsgClass));
add_assoc_str(&info, "abapMsgType", sapuc_to_zend_string(error_info.abapMsgType));
add_assoc_str(&info, "abapMsgNumber", sapuc_to_zend_string(error_info.abapMsgNumber));
add_assoc_str(&info, "abapMsgV1", sapuc_to_zend_string(error_info.abapMsgV1));
add_assoc_str(&info, "abapMsgV2", sapuc_to_zend_string(error_info.abapMsgV2));
add_assoc_str(&info, "abapMsgV3", sapuc_to_zend_string(error_info.abapMsgV3));
add_assoc_str(&info, "abapMsgV4", sapuc_to_zend_string(error_info.abapMsgV4));
break;
case RFC_OK: // LCOV_EXCL_START
zend_error(E_ERROR, "Internal error: exception handler called for RFC_OK");
zend_restore_error_handling(&zeh);
zend_string_release(message);
return;
// LCOV_EXCL_STOP
default:
zend_error(E_ERROR, "Internal error: unknown error group");
zend_restore_error_handling(&zeh);
zend_string_release(message);
return;
}
zend_update_property(sapnwrfc_connection_exception_ce, SAPNWRFC_EXC_Z_OBJ_OR_ZVAL(ex), "errorInfo", sizeof("errorInfo") - 1, &info);
zval_ptr_dtor(&info);
zend_string_release(message);
zend_throw_exception_object(&ex);
zend_restore_error_handling(&zeh);
}
void sapnwrfc_throw_function_exception_ex(char *msg, ...)
{
va_list args;
char *buf;
va_start(args, msg);
vspprintf(&buf, 0, msg, args);
va_end(args);
zend_throw_exception(sapnwrfc_function_exception_ce, buf, 0);
efree(buf);
}
void sapnwrfc_throw_function_exception(RFC_ERROR_INFO error_info, char *msg, ...)
{
va_list args;
zend_string *message;
zval info;
zval ex;
zend_error_handling zeh;
va_start(args, msg);
message = vstrpprintf(0, msg, args);
va_end(args);
array_init(&info);
zend_replace_error_handling(EH_THROW, sapnwrfc_function_exception_ce, &zeh);
object_init_ex(&ex, sapnwrfc_function_exception_ce);
zend_update_property_string(sapnwrfc_function_exception_ce, SAPNWRFC_EXC_Z_OBJ_OR_ZVAL(ex), "message", sizeof("message") - 1, ZSTR_VAL(message));
zend_update_property_long(sapnwrfc_function_exception_ce, SAPNWRFC_EXC_Z_OBJ_OR_ZVAL(ex), "code", sizeof("code") - 1, error_info.code);
// populate errorInfo array
add_assoc_long(&info, "code", error_info.code);
add_assoc_str(&info, "key", sapuc_to_zend_string(error_info.key));
add_assoc_str(&info, "message", sapuc_to_zend_string(error_info.message));
switch(error_info.group) {
case LOGON_FAILURE: // Error message raised when logon fails
case COMMUNICATION_FAILURE: // Problems with the network connection (or backend broke down and killed the connection)
case EXTERNAL_RUNTIME_FAILURE: // Problems in the RFC runtime of the external program (i.e "this" library)
// no additional properties, just the generic one's from above
break;
case ABAP_APPLICATION_FAILURE: // ABAP Exception raised in ABAP function modules
case ABAP_RUNTIME_FAILURE: // ABAP Message raised in ABAP function modules or in ABAP runtime of the backend (e.g Kernel)
case EXTERNAL_APPLICATION_FAILURE: // Problems in the external program (e.g in the external server implementation)
case EXTERNAL_AUTHORIZATION_FAILURE: // Problems raised in the authorization check handler provided by the external server implementation
add_assoc_str(&info, "abapMsgClass", sapuc_to_zend_string(error_info.abapMsgClass));
add_assoc_str(&info, "abapMsgType", sapuc_to_zend_string(error_info.abapMsgType));
add_assoc_str(&info, "abapMsgNumber", sapuc_to_zend_string(error_info.abapMsgNumber));
add_assoc_str(&info, "abapMsgV1", sapuc_to_zend_string(error_info.abapMsgV1));
add_assoc_str(&info, "abapMsgV2", sapuc_to_zend_string(error_info.abapMsgV2));
add_assoc_str(&info, "abapMsgV3", sapuc_to_zend_string(error_info.abapMsgV3));
add_assoc_str(&info, "abapMsgV4", sapuc_to_zend_string(error_info.abapMsgV4));
break;
case RFC_OK: // LCOV_EXCL_START
zend_error(E_ERROR, "Internal error: exception handler called for RFC_OK");
zend_restore_error_handling(&zeh);
zend_string_release(message);
return;
// LCOV_EXCL_STOP
default:
zend_error(E_ERROR, "Internal error: unknown error group");
zend_restore_error_handling(&zeh);
zend_string_release(message);
return;
}
zend_update_property(sapnwrfc_function_exception_ce, SAPNWRFC_EXC_Z_OBJ_OR_ZVAL(ex), "errorInfo", sizeof("errorInfo") - 1, &info);
zval_ptr_dtor(&info);
zend_string_release(message);
zend_throw_exception_object(&ex);
zend_restore_error_handling(&zeh);
}
PHP_METHOD(Exception, getErrorInfo)
{
zval tmp;
zval *error_info;
zend_parse_parameters_none();
error_info = zend_read_property(sapnwrfc_exception_ce, SAPNWRFC_EXC_Z_OBJ_OR_ZVAL_P(getThis()), "errorInfo", sizeof("errorInfo") - 1, 0, &tmp);
RETURN_ZVAL(error_info, 1, 0);
}
void sapnwrfc_register_exceptions()
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "SAPNWRFC\\Exception", sapnwrfc_exception_class_functions);
sapnwrfc_exception_ce = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException);
zval property_errorInfo_default_value;
ZVAL_NULL(&property_errorInfo_default_value);
zend_string *property_errorInfo_name = zend_string_init("errorInfo", sizeof("errorInfo") - 1, 1);
zend_declare_typed_property(sapnwrfc_exception_ce, property_errorInfo_name, &property_errorInfo_default_value,
ZEND_ACC_PROTECTED, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY|MAY_BE_NULL));
zend_string_release(property_errorInfo_name);
INIT_CLASS_ENTRY(ce, "SAPNWRFC\\ConnectionException", NULL);
sapnwrfc_connection_exception_ce = zend_register_internal_class_ex(&ce, sapnwrfc_exception_ce);
sapnwrfc_connection_exception_ce->ce_flags |= ZEND_ACC_FINAL;
INIT_CLASS_ENTRY(ce, "SAPNWRFC\\FunctionCallException", NULL);
sapnwrfc_function_exception_ce = zend_register_internal_class_ex(&ce, sapnwrfc_exception_ce);
sapnwrfc_function_exception_ce->ce_flags |= ZEND_ACC_FINAL;
}