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

Commit 6632554

Browse files
Added some more custom exceptions and used them throughout the project
1 parent 5857463 commit 6632554

File tree

11 files changed

+103
-28
lines changed

11 files changed

+103
-28
lines changed

src/connection.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace arangodb { namespace fuerte { namespace php {
3636
int threadCount = params[0];
3737

3838
if(threadCount < 1) {
39-
throw Php::Exception("Invalid threadCount provided, must be >= 1");
39+
ARANGODB_THROW(InvalidArgumentException(), "Invalid threadCount provided, must be >= 1 in %s on line %d");
40+
return;
4041
}
4142

4243
this->threadCount = threadCount;
@@ -47,7 +48,8 @@ namespace arangodb { namespace fuerte { namespace php {
4748
int defaultTimeout = params[0];
4849

4950
if(defaultTimeout < 1) {
50-
throw Php::Exception("Invalid defaultTimeout provided, must be >= 1");
51+
ARANGODB_THROW(InvalidArgumentException(), "Invalid defaultTimeout provided, must be >= 1 in %s on line %d");
52+
return;
5153
}
5254

5355
this->defaultTimeout = defaultTimeout;
@@ -128,8 +130,10 @@ namespace arangodb { namespace fuerte { namespace php {
128130

129131
Php::Value Connection::send(Php::Parameters &params)
130132
{
131-
if(!params[0].instanceOf("ArangoDb\\Request"))
132-
throw Php::Exception("Expected request to be of type Request");
133+
if(!params[0].instanceOf("ArangoDb\\Request")) {
134+
ARANGODB_THROW(InvalidArgumentException(), "Expected request to be of type Request in %s on line %d");
135+
return NULL;
136+
}
133137

134138
Request* request = (Request*)params[0].implementation();
135139

@@ -139,11 +143,15 @@ namespace arangodb { namespace fuerte { namespace php {
139143

140144
void Connection::sendAsync(Php::Parameters &params)
141145
{
142-
if(!params[0].instanceOf("ArangoDb\\Request"))
143-
throw Php::Exception("Expected request to be of type Request");
146+
if(!params[0].instanceOf("ArangoDb\\Request")) {
147+
ARANGODB_THROW(InvalidArgumentException(), "Expected request to be of type Request in %s on line %d");
148+
return;
149+
}
144150

145-
if(!params[1].isCallable())
146-
throw Php::Exception("Expected callback to be of type Callable");
151+
if(!params[1].isCallable()) {
152+
ARANGODB_THROW(InvalidArgumentException(), "Expected callback to be of type Callable in %s on line %d");
153+
return;
154+
}
147155

148156
Request* request = (Request*)params[0].implementation();
149157
this->sendRequestAsync(request, params[1]);
@@ -213,14 +221,17 @@ namespace arangodb { namespace fuerte { namespace php {
213221
this->asyncWaitGroup = new fu::WaitGroup();
214222

215223
if(!success) {
216-
throw Php::Exception("Sending request to ArangoDB failed");
224+
ARANGODB_THROW(RuntimeException(), "Sending request to ArangoDB failed in %s on line %d");
225+
return;
217226
}
218227
}
219228

220229
Php::Value Connection::query(Php::Parameters &params)
221230
{
222-
if(!params[0].instanceOf("ArangoDb\\Vpack"))
223-
throw Php::Exception("Expected vpack to be of type Vpack");
231+
if(!params[0].instanceOf("ArangoDb\\Vpack")) {
232+
ARANGODB_THROW(InvalidArgumentException(), "Expected vpack to be of type Vpack in %s on line %d");
233+
return NULL;
234+
}
224235

225236
Cursor* cursor = new Cursor(this, (Vpack*)params[0].implementation());
226237

src/cursor.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ namespace arangodb { namespace fuerte { namespace php {
99
void Cursor::setOption(int option, int value)
1010
{
1111
if(this->options.size() <= option) {
12-
throw Php::Exception("Invalid option provided for Cursor");
12+
ARANGODB_THROW(InvalidOptionException(), "Invalid option provided for Cursor in %s on line %d");
13+
return;
1314
}
1415

1516
this->options[option] = value;
@@ -24,10 +25,13 @@ namespace arangodb { namespace fuerte { namespace php {
2425
this->response = response;
2526

2627
if(this->response->getFuerteResponse()->slices().front().get("error").getBool()) {
27-
throw Php::Exception(
28-
"Error while executing query: " +
29-
this->response->getFuerteResponse()->slices().front().get("errorMessage").copyString()
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()
3032
);
33+
34+
return;
3135
}
3236

3337
this->hasMore = this->response->getFuerteResponse()->slices().front().get("hasMore").getBool();
@@ -87,7 +91,8 @@ namespace arangodb { namespace fuerte { namespace php {
8791
vp::Dumper dumper(&sink, &dumperOptions);
8892
dumper.dump(slice);
8993
} catch(vp::Exception const& e) {
90-
throw Php::Exception(e.what());
94+
ARANGODB_THROW(RuntimeException(), e.what());
95+
return NULL;
9196
}
9297

9398
switch(this->options[Cursor::ENTRY_TYPE]) {

src/exception.h

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,30 @@ using namespace std;
1212

1313
namespace arangodb { namespace fuerte { namespace php {
1414

15+
/*
16+
Exception hierarchy
17+
18+
\Exception
19+
\ArangoDb\Exception
20+
\ArangoDb\RuntimeException
21+
\ArangoDb\InvalidOptionException
22+
23+
\Arangodb\InvalidArgumentException
24+
*/
25+
26+
static zend_class_entry* _Exception;
1527
static zend_class_entry* _RuntimeException;
1628
static zend_class_entry* _InvalidOptionException;
29+
static zend_class_entry* _InvalidArgumentException;
30+
31+
32+
static zend_class_entry* Exception() {
33+
zend_string *exceptionClassName = zend_string_tolower(zend_string_init(ZEND_STRL("ArangoDb\\Exception"), 1));
34+
zend_class_entry *exceptionClass = static_cast<zend_class_entry*>(zend_hash_find_ptr(CG(class_table), exceptionClassName));
35+
zend_string_release(exceptionClassName);
1736

37+
return exceptionClass;
38+
}
1839

1940
static zend_class_entry* RuntimeException() {
2041
zend_string *exceptionClassName = zend_string_tolower(zend_string_init(ZEND_STRL("ArangoDb\\RuntimeException"), 1));
@@ -32,12 +53,27 @@ namespace arangodb { namespace fuerte { namespace php {
3253
return exceptionClass;
3354
}
3455

56+
static zend_class_entry* InvalidArgumentException() {
57+
zend_string *exceptionClassName = zend_string_tolower(zend_string_init(ZEND_STRL("ArangoDb\\InvalidArgumentException"), 1));
58+
zend_class_entry *exceptionClass = static_cast<zend_class_entry*>(zend_hash_find_ptr(CG(class_table), exceptionClassName));
59+
zend_string_release(exceptionClassName);
60+
61+
return exceptionClass;
62+
}
63+
64+
65+
static void registerException()
66+
{
67+
zend_class_entry ce;
68+
INIT_CLASS_ENTRY(ce, "ArangoDb\\Exception", NULL);
69+
_Exception = zend_register_internal_class_ex(&ce, zend_exception_get_default());
70+
}
3571

3672
static void registerRuntimeException()
3773
{
3874
zend_class_entry ce;
3975
INIT_CLASS_ENTRY(ce, "ArangoDb\\RuntimeException", NULL);
40-
_RuntimeException = zend_register_internal_class_ex(&ce, zend_exception_get_default());
76+
_RuntimeException = zend_register_internal_class_ex(&ce, _Exception);
4177
}
4278

4379
static void registerInvalidOptionException()
@@ -47,10 +83,19 @@ namespace arangodb { namespace fuerte { namespace php {
4783
_InvalidOptionException = zend_register_internal_class_ex(&ce, _RuntimeException);
4884
}
4985

86+
static void registerInvalidArgumentException()
87+
{
88+
zend_class_entry ce;
89+
INIT_CLASS_ENTRY(ce, "ArangoDb\\InvalidArgumentException", NULL);
90+
_RuntimeException = zend_register_internal_class_ex(&ce, _Exception);
91+
}
92+
5093
static void registerCustomExceptions()
5194
{
95+
registerException();
5296
registerRuntimeException();
5397
registerInvalidOptionException();
98+
registerInvalidArgumentException();
5499
}
55100

56101
}}}

src/request.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace arangodb { namespace fuerte { namespace php {
1717
Request::Request(Php::Parameters params)
1818
{
1919
if(!params[1].instanceOf("ArangoDb\\Vpack")) {
20-
throw Php::Exception("Expected vpack to be of type Vpack");
20+
ARANGODB_THROW(InvalidArgumentException(), "Expected vpack to be of type Vpack in %s on line %d");
21+
return;
2122
}
2223

2324
if(params.size() == 3 && params[2].size() > 0) {
@@ -31,7 +32,8 @@ namespace arangodb { namespace fuerte { namespace php {
3132
void Request::__construct(Php::Parameters &params)
3233
{
3334
if(!params[2].instanceOf("ArangoDb\\Vpack")) {
34-
throw Php::Exception("Expected vpack to be of type Vpack");
35+
ARANGODB_THROW(InvalidArgumentException(), "Expected vpack to be of type Vpack in %s on line %d");
36+
return;
3537
}
3638

3739
if(params.size() == 4 && params[3].size() > 0) {

src/response.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ namespace arangodb { namespace fuerte { namespace php {
2323
vp::Dumper dumper(&sink, &dumperOptions);
2424
dumper.dump(slice);
2525
} catch(vp::Exception const& e) {
26-
throw Php::Exception(e.what());
26+
ARANGODB_THROW(InvalidArgumentException(), e.what());
27+
return NULL;
2728
}
2829

2930
return body;

src/response.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "velocypack/vpack.h"
99
#include "velocypack/velocypack-exception-macros.h"
1010

11+
#include "exception.h"
12+
1113
namespace vp = ::arangodb::velocypack;
1214
namespace fu = ::arangodb::fuerte;
1315

src/vpack.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ namespace arangodb { namespace fuerte { namespace php {
1414
parser.parse(json);
1515
}
1616
catch (std::bad_alloc const& e) {
17-
throw Php::Exception("Out of memory");
17+
ARANGODB_THROW(RuntimeException(), "Out of memory in %s on line %d");
18+
return NULL;
1819
}
1920
catch (vp::Exception const& e) {
20-
throw Php::Exception(e.what());
21+
ARANGODB_THROW(RuntimeException(), e.what());
22+
return NULL;
2123
}
2224

2325
instance->builder = *parser.steal();
@@ -33,10 +35,12 @@ namespace arangodb { namespace fuerte { namespace php {
3335
parser.parse(params[0]);
3436
}
3537
catch (std::bad_alloc const& e) {
36-
throw Php::Exception("Out of memory");
38+
ARANGODB_THROW(RuntimeException(), "Out of memory in %s on line %d");
39+
return NULL;
3740
}
3841
catch (vp::Exception const& e) {
39-
throw Php::Exception(e.what());
42+
ARANGODB_THROW(RuntimeException(), e.what());
43+
return NULL;
4044
}
4145

4246
instance->builder = *parser.steal();
@@ -64,7 +68,8 @@ namespace arangodb { namespace fuerte { namespace php {
6468

6569
return json;
6670
} catch(vp::Exception const& e) {
67-
throw Php::Exception(e.what());
71+
ARANGODB_THROW(RuntimeException(), e.what());
72+
return NULL;
6873
}
6974
}
7075

src/vpack.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "velocypack/vpack.h"
66
#include "velocypack/velocypack-exception-macros.h"
77

8+
#include "exception.h"
9+
810
namespace vp = ::arangodb::velocypack;
911

1012
namespace arangodb { namespace fuerte { namespace php {

tests/ConnectionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public function it_throws_invalid_option_exception(): void
7373
*/
7474
public function it_throws_exception_on_attempt_to_set_invalid_thread_count(): void
7575
{
76-
$this->expectException(\Exception::class);
76+
$this->expectException(\ArangoDb\InvalidArgumentException::class);
77+
$this->expectExceptionMessage('Invalid threadCount provided, must be >= 1');
7778

7879
$connection = new Connection(
7980
[

tests/ExceptionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function it_throws_valid_custom_exception(): void
2828

2929
} catch(\Throwable $e) {
3030
$this->assertInstanceOf(\Exception::class, $e);
31+
$this->assertInstanceOf(\ArangoDb\Exception::class, $e);
3132
$this->assertInstanceOf(\ArangoDb\RuntimeException::class, $e);
3233
$this->assertInstanceOf(\ArangoDb\InvalidOptionException::class, $e);
3334
}

0 commit comments

Comments
 (0)