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

Commit 2e58861

Browse files
Added RequestFailedException
1 parent 75e010d commit 2e58861

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

src/connection.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ 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+
auto statusCode = response->getHttpCode();
117+
if((!(statusCode >= 200 && statusCode <= 299))/* || response->getFuerteResponse()->slices().front().get("error").getBool()*/) {
118+
throwRequestFailedException("This is the error message", statusCode, response->getBody());
119+
}
120+
121+
return response;
115122
}
116123

117124
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

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ extern "C" {
106106
vpack.method<&arangodb::fuerte::php::Vpack::fromArray>("fromArray", {
107107
Php::ByVal("array", Php::Type::Array, true)
108108
});
109+
vpack.method<&arangodb::fuerte::php::Vpack::fromArrayNative>("fromArrayNative", {
110+
Php::ByVal("array", Php::Type::Array, true)
111+
});
109112
vpack.method<&arangodb::fuerte::php::Vpack::fromJson>("fromJson", {
110113
Php::ByVal("json", Php::Type::String, true)
111114
});

tests/ConnectionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,34 @@ public function it_rewinds_cursor_half_way_through()
374374
$this->assertCount(100, $dataSet2);
375375
$this->assertTrue($dataSet1 === array_slice($dataSet2, 0, 50));
376376
}
377+
378+
379+
/**
380+
* @test
381+
*/
382+
public function it_throws_request_failed_exception_with_response_data()
383+
{
384+
$this->connection = TestUtil::getConnection();
385+
386+
$res = $this->connection->post(
387+
'/_api/collection/',
388+
TestUtil::getVpackCreateCollection('request_failed_exception_test')
389+
);
390+
391+
try {
392+
$this->connection->post(
393+
'/_api/collection/',
394+
TestUtil::getVpackCreateCollection('request_failed_exception_test')
395+
);
396+
397+
$this->assertTrue(false);
398+
399+
} catch(\ArangoDb\RequestFailedException $e) {
400+
$this->assertNotNull($e->getCode());
401+
$this->assertNotNull($e->getHttpCode());
402+
$this->assertTrue($e->getCode() === $e->getHttpCode());
403+
$this->assertJson($e->getBody());
404+
$this->assertTrue(json_decode($e->getBody(), true)['errorMessage'] === 'duplicate name');
405+
}
406+
}
377407
}

0 commit comments

Comments
 (0)