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

Commit ff67a6f

Browse files
authored
Merge pull request #43 from sandrokeil/hotfix/check-error
Add check if response keys available
2 parents c0e144b + 69ee95e commit ff67a6f

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

src/connection.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,20 @@ namespace arangodb { namespace fuerte { namespace php {
114114
auto response = new Response(*result);
115115

116116
bool failure = false;
117-
if(response->getFuerteResponse()->slices().front().isObject()) {
117+
118+
if (response->getFuerteResponse()->slices().front().isObject()
119+
&& response->getFuerteResponse()->slices().front().hasKey("error")
120+
) {
118121
failure = response->getFuerteResponse()->slices().front().get("error").getBool();
119122
}
120123

121124
auto statusCode = response->getHttpCode();
122125
if((!(statusCode >= 200 && statusCode <= 299)) || failure) {
123126
std::string errorMessage = "Response contains an error";
124127

125-
if(response->getFuerteResponse()->slices().front().isObject()) {
128+
if(response->getFuerteResponse()->slices().front().isObject()
129+
&& response->getFuerteResponse()->slices().front().hasKey("errorMessage")
130+
) {
126131
errorMessage = response->getFuerteResponse()->slices().front().get("errorMessage").copyString();
127132
}
128133

src/cursor.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,26 @@ namespace arangodb { namespace fuerte { namespace php {
2424
Response* response = this->connection->sendRequest(&request);
2525
this->response = response;
2626

27-
if(this->response->getFuerteResponse()->slices().front().get("error").getBool()) {
28-
ARANGODB_THROW(
29-
RuntimeException(),
30-
("Error while executing query in %s on line %d: " +
31-
this->response->getFuerteResponse()->slices().front().get("errorMessage").copyString()).c_str()
32-
);
27+
bool failure = false;
3328

29+
if (this->response->getFuerteResponse()->slices().front().isObject()
30+
&& this->response->getFuerteResponse()->slices().front().hasKey("error")
31+
) {
32+
failure = this->response->getFuerteResponse()->slices().front().get("error").getBool();
33+
}
34+
35+
auto statusCode = this->response->getHttpCode();
36+
37+
if((!(statusCode >= 200 && statusCode <= 299)) || failure) {
38+
std::string errorMessage = "Response contains an error";
39+
40+
if(this->response->getFuerteResponse()->slices().front().isObject()
41+
&& this->response->getFuerteResponse()->slices().front().hasKey("errorMessage")
42+
) {
43+
errorMessage = this->response->getFuerteResponse()->slices().front().get("errorMessage").copyString();
44+
}
45+
46+
throwRequestFailedException(errorMessage.c_str(), statusCode, this->response->getBody());
3447
return;
3548
}
3649

tests/CursorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,23 @@ public function it_rewinds_cursor_half_way_through()
247247
$this->assertCount(100, $dataSet2);
248248
$this->assertSame($dataSet1, \array_slice($dataSet2, 0, 50));
249249
}
250+
251+
/**
252+
* @test
253+
*/
254+
public function it_throws_exception_if_collection_not_found()
255+
{
256+
$collection = 'event_streams';
257+
$this->connection = TestUtil::getConnection();
258+
259+
$this->expectException(\ArangoDb\RequestFailedException::class);
260+
261+
$cursor = $this->connection->query(Vpack::fromArray([
262+
'query' => 'FOR c IN @@collection RETURN c',
263+
'bindVars' => ['@collection' => $collection]
264+
]));
265+
266+
$cursor->rewind();
267+
$iterations = 0;
268+
}
250269
}

0 commit comments

Comments
 (0)