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

Commit 5857463

Browse files
Added basic support for custom exceptions
1 parent c8613d5 commit 5857463

File tree

8 files changed

+106
-8
lines changed

8 files changed

+106
-8
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ endif()
3131
option(BuildVelocyPackExamples "Build examples" OFF)
3232
option(BuildTools "Build support programs and tools" OFF)
3333

34-
#include_directories(/usr/local/include/php /usr/local/include/php/TSRM /usr/local/include/php/main /usr/local/include/php/Zend)
34+
include_directories(/usr/local/include/php /usr/local/include/php/TSRM /usr/local/include/php/main /usr/local/include/php/Zend)
3535

3636
add_subdirectory(deps/fuerte)
3737
#add_subdirectory(deps/phpcpp)

Dockerfile.builder

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ VOLUME ["/app"]
3030

3131
RUN apk add --update bash && rm -rf /tmp/*
3232

33+
RUN docker-php-source extract
34+
3335
ENTRYPOINT []
3436
CMD bash -c "cd /app/build && cmake .. -DPHPCPP_ARCH=x86_64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-fPIC && make"

src/connection.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ namespace arangodb { namespace fuerte { namespace php {
5555

5656
fu::ConnectionBuilder Connection::createConnectionBuilder()
5757
{
58-
try {
59-
fu::ConnectionBuilder cbuilder{};
58+
fu::ConnectionBuilder cbuilder{};
6059

60+
try {
6161
for (const auto &p : this->options) {
6262
switch (connectionOptions.at(p.first)) {
6363
case ConnectionOptions::HOST:
@@ -80,12 +80,12 @@ namespace arangodb { namespace fuerte { namespace php {
8080
break;
8181
}
8282
}
83-
84-
return cbuilder;
8583
}
8684
catch (const std::exception &ex) {
87-
throw Php::Exception("Unknown option provided.");
85+
ARANGODB_THROW(InvalidOptionException(), "Unknown option provided in %s on line %d");
8886
}
87+
88+
return cbuilder;
8989
}
9090

9191
void Connection::connect()

src/connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "request.h"
77
#include "response.h"
8+
#include "exception.h"
89

910
namespace fu = ::arangodb::fuerte;
1011

src/exception.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
3+
#include <cmath>
4+
using namespace std;
5+
6+
#include <Zend/zend.h>
7+
#include <Zend/zend_exceptions.h>
8+
#include <Zend/zend_API.h>
9+
10+
#define ARANGODB_THROW(ex, message) zend_throw_exception_ex(ex, 0, message, __FILE__, __LINE__);
11+
12+
13+
namespace arangodb { namespace fuerte { namespace php {
14+
15+
static zend_class_entry* _RuntimeException;
16+
static zend_class_entry* _InvalidOptionException;
17+
18+
19+
static zend_class_entry* RuntimeException() {
20+
zend_string *exceptionClassName = zend_string_tolower(zend_string_init(ZEND_STRL("ArangoDb\\RuntimeException"), 1));
21+
zend_class_entry *exceptionClass = static_cast<zend_class_entry*>(zend_hash_find_ptr(CG(class_table), exceptionClassName));
22+
zend_string_release(exceptionClassName);
23+
24+
return exceptionClass;
25+
}
26+
27+
static zend_class_entry* InvalidOptionException() {
28+
zend_string *exceptionClassName = zend_string_tolower(zend_string_init(ZEND_STRL("ArangoDb\\InvalidOptionException"), 1));
29+
zend_class_entry *exceptionClass = static_cast<zend_class_entry*>(zend_hash_find_ptr(CG(class_table), exceptionClassName));
30+
zend_string_release(exceptionClassName);
31+
32+
return exceptionClass;
33+
}
34+
35+
36+
static void registerRuntimeException()
37+
{
38+
zend_class_entry ce;
39+
INIT_CLASS_ENTRY(ce, "ArangoDb\\RuntimeException", NULL);
40+
_RuntimeException = zend_register_internal_class_ex(&ce, zend_exception_get_default());
41+
}
42+
43+
static void registerInvalidOptionException()
44+
{
45+
zend_class_entry ce;
46+
INIT_CLASS_ENTRY(ce, "ArangoDb\\InvalidOptionException", NULL);
47+
_InvalidOptionException = zend_register_internal_class_ex(&ce, _RuntimeException);
48+
}
49+
50+
static void registerCustomExceptions()
51+
{
52+
registerRuntimeException();
53+
registerInvalidOptionException();
54+
}
55+
56+
}}}

src/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "response.h"
66
#include "vpack.h"
77
#include "cursor.h"
8+
#include "exception.h"
89

910
extern "C" {
1011

@@ -33,6 +34,10 @@ extern "C" {
3334
exportClassResponse(&extension);
3435
exportClassCursor(&extension);
3536

37+
extension.onStartup([]() {
38+
arangodb::fuerte::php::registerCustomExceptions();
39+
});
40+
3641
return extension;
3742
}
3843

tests/ConnectionTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public function it_throws_invalid_option_exception(): void
6161
'unknown' => 'error',
6262
]
6363
);
64-
65-
$this->expectException(\Exception::class);
64+
$this->expectException(\ArangoDb\InvalidOptionException::class);
6665
$this->expectExceptionMessage('Unknown option provided');
6766

6867
$connection->connect();

tests/ExceptionTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArangoDbDriverTest;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use ArangoDb\Connection;
9+
10+
/**
11+
* @group connection
12+
*/
13+
class ExceptionTest extends TestCase
14+
{
15+
/**
16+
* @test
17+
*/
18+
public function it_throws_valid_custom_exception(): void
19+
{
20+
try {
21+
$connection = new Connection([
22+
'unknown' => 'error'
23+
]);
24+
25+
$connection->connect();
26+
27+
$this->assertTrue(false);
28+
29+
} catch(\Throwable $e) {
30+
$this->assertInstanceOf(\Exception::class, $e);
31+
$this->assertInstanceOf(\ArangoDb\RuntimeException::class, $e);
32+
$this->assertInstanceOf(\ArangoDb\InvalidOptionException::class, $e);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)