Skip to content
This repository was archived by the owner on Jun 18, 2020. It is now read-only.

Commit c0e144b

Browse files
authored
Merge pull request #42 from sandrokeil/feature/exceptions
Added RequestFailedException
2 parents 75e010d + d35e8d2 commit c0e144b

File tree

8 files changed

+412
-219
lines changed

8 files changed

+412
-219
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ services:
2828
command: ["vendor/bin/phpunit"]
2929

3030
arangodb:
31-
image: arangodb:3.2
31+
image: arangodb:3.2.9
3232
ports:
3333
- 8529:8529
3434
environment:

phpstorm-stubs/arangodb/arangodb.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,41 @@ public function toArray(): array
311311
{
312312
}
313313
}
314+
315+
class Exception extends \Exception
316+
{
317+
318+
}
319+
320+
class RuntimeException extends Exception
321+
{
322+
323+
}
324+
325+
class InvalidOptionException extends RuntimeException
326+
{
327+
328+
}
329+
330+
class RequestFailedException extends RuntimeException
331+
{
332+
/**
333+
* @return int HTTP status code
334+
*/
335+
public function getHttpCode(): int
336+
{
337+
}
338+
339+
/**
340+
* @return string Response body
341+
*/
342+
public function getBody(): string
343+
{
344+
}
345+
}
346+
347+
class InvalidArgumentException extends Exception
348+
{
349+
350+
}
314351
}

src/connection.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,25 @@ namespace arangodb { namespace fuerte { namespace php {
111111
std::move(request->getFuerteRequest())
112112
);
113113

114-
return new Response(*result);
114+
auto response = new Response(*result);
115+
116+
bool failure = false;
117+
if(response->getFuerteResponse()->slices().front().isObject()) {
118+
failure = response->getFuerteResponse()->slices().front().get("error").getBool();
119+
}
120+
121+
auto statusCode = response->getHttpCode();
122+
if((!(statusCode >= 200 && statusCode <= 299)) || failure) {
123+
std::string errorMessage = "Response contains an error";
124+
125+
if(response->getFuerteResponse()->slices().front().isObject()) {
126+
errorMessage = response->getFuerteResponse()->slices().front().get("errorMessage").copyString();
127+
}
128+
129+
throwRequestFailedException(errorMessage.c_str(), statusCode, response->getBody());
130+
}
131+
132+
return response;
115133
}
116134

117135
void Connection::sendRequestAsync(Request* request, Php::Value& callback)

src/exception.h

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,76 @@ using namespace std;
66
#include <Zend/zend.h>
77
#include <Zend/zend_exceptions.h>
88
#include <Zend/zend_API.h>
9+
#include <php.h>
910

1011
#define ARANGODB_THROW(ex, message) zend_throw_exception_ex(ex, 0, message, __FILE__, __LINE__);
1112

1213

13-
namespace arangodb { namespace fuerte { namespace php {
1414

15+
namespace {
1516
/*
1617
Exception hierarchy
1718
1819
\Exception
1920
\ArangoDb\Exception
2021
\ArangoDb\RuntimeException
2122
\ArangoDb\InvalidOptionException
23+
\ArangoDb\RequestFailedException
2224
2325
\Arangodb\InvalidArgumentException
2426
*/
2527

2628
static zend_class_entry* _Exception;
2729
static zend_class_entry* _RuntimeException;
2830
static zend_class_entry* _InvalidOptionException;
31+
static zend_class_entry* _RequestFailedException;
2932
static zend_class_entry* _InvalidArgumentException;
3033

3134

35+
PHP_METHOD(RequestFailedException, getBody)
36+
{
37+
zval *obj, *value;
38+
39+
if(zend_parse_parameters_none() == FAILURE) {
40+
return;
41+
}
42+
43+
obj = getThis();
44+
45+
value = zend_read_property(_RequestFailedException, obj, "body", sizeof("body") - 1, 1, NULL TSRMLS_CC);
46+
47+
RETURN_ZVAL(value, 1, 0);
48+
}
49+
50+
PHP_METHOD(RequestFailedException, getHttpCode)
51+
{
52+
zval *obj, *value;
53+
54+
if(zend_parse_parameters_none() == FAILURE) {
55+
return;
56+
}
57+
58+
obj = getThis();
59+
60+
value = zend_read_property(_RequestFailedException, obj, "code", sizeof("code") - 1, 1, NULL TSRMLS_CC);
61+
62+
RETURN_ZVAL(value, 1, 0);
63+
}
64+
65+
66+
ZEND_BEGIN_ARG_INFO_EX(arginfo_void, 0, 0, 0)
67+
ZEND_END_ARG_INFO()
68+
69+
const zend_function_entry request_failed_exception_functions[] = {
70+
PHP_ME(RequestFailedException, getBody, arginfo_void, ZEND_ACC_PUBLIC)
71+
PHP_ME(RequestFailedException, getHttpCode, arginfo_void, ZEND_ACC_PUBLIC)
72+
PHP_FE_END
73+
};
74+
}
75+
76+
77+
namespace arangodb { namespace fuerte { namespace php {
78+
3279
static zend_class_entry* Exception() {
3380
zend_string *exceptionClassName = zend_string_tolower(zend_string_init(ZEND_STRL("ArangoDb\\Exception"), 1));
3481
zend_class_entry *exceptionClass = static_cast<zend_class_entry*>(zend_hash_find_ptr(CG(class_table), exceptionClassName));
@@ -53,6 +100,29 @@ namespace arangodb { namespace fuerte { namespace php {
53100
return exceptionClass;
54101
}
55102

103+
static zend_class_entry* RequestFailedException() {
104+
zend_string *exceptionClassName = zend_string_tolower(zend_string_init(ZEND_STRL("ArangoDb\\RequestFailedException"), 1));
105+
zend_class_entry *exceptionClass = static_cast<zend_class_entry*>(zend_hash_find_ptr(CG(class_table), exceptionClassName));
106+
zend_string_release(exceptionClassName);
107+
108+
return exceptionClass;
109+
}
110+
111+
static void throwRequestFailedException(const char* message, const int code, const char* body) {
112+
zval exObj;
113+
zend_class_entry* exceptionClass = RequestFailedException();
114+
115+
object_init_ex(&exObj, exceptionClass);
116+
117+
//zend_declare_property_long(_RequestFailedException, "code", sizeof("code") - 1, code, ZEND_ACC_PROTECTED TSRMLS_CC);
118+
119+
zend_update_property_string(exceptionClass, &exObj, "message", sizeof("message") - 1, message);
120+
zend_update_property_long(exceptionClass, &exObj, "code", sizeof("code") - 1, code);
121+
zend_update_property_string(exceptionClass, &exObj, "body", sizeof("body") - 1, body);
122+
123+
zend_throw_exception_object(&exObj);
124+
}
125+
56126
static zend_class_entry* InvalidArgumentException() {
57127
zend_string *exceptionClassName = zend_string_tolower(zend_string_init(ZEND_STRL("ArangoDb\\InvalidArgumentException"), 1));
58128
zend_class_entry *exceptionClass = static_cast<zend_class_entry*>(zend_hash_find_ptr(CG(class_table), exceptionClassName));
@@ -83,6 +153,13 @@ namespace arangodb { namespace fuerte { namespace php {
83153
_InvalidOptionException = zend_register_internal_class_ex(&ce, _RuntimeException);
84154
}
85155

156+
static void registerRequestFailedException()
157+
{
158+
zend_class_entry ce;
159+
INIT_CLASS_ENTRY(ce, "ArangoDb\\RequestFailedException", request_failed_exception_functions);
160+
_RequestFailedException = zend_register_internal_class_ex(&ce, _RuntimeException);
161+
}
162+
86163
static void registerInvalidArgumentException()
87164
{
88165
zend_class_entry ce;
@@ -95,6 +172,7 @@ namespace arangodb { namespace fuerte { namespace php {
95172
registerException();
96173
registerRuntimeException();
97174
registerInvalidOptionException();
175+
registerRequestFailedException();
98176
registerInvalidArgumentException();
99177
}
100178

0 commit comments

Comments
 (0)