Skip to content

Commit 97b5c6c

Browse files
committed
added the neo4j exception implementation
1 parent 945e9f8 commit 97b5c6c

File tree

5 files changed

+126
-10
lines changed

5 files changed

+126
-10
lines changed

src/Exception/Neo4jException.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Exception;
4+
5+
use Exception;
6+
7+
class Neo4jException extends Exception
8+
{
9+
private string $errorCode;
10+
11+
public function __construct(string $errorCode, string $message, int $code = 0, Exception $previous = null)
12+
{
13+
$this->errorCode = $errorCode;
14+
parent::__construct($message, $code, $previous);
15+
}
16+
17+
public function getErrorCode(): string
18+
{
19+
return $this->errorCode;
20+
}
21+
22+
public static function fromNeo4jResponse(array $response): self
23+
{
24+
$errorCode = $response['code'] ?? 'Neo.UnknownError';
25+
$message = $response['message'] ?? 'An unknown error occurred.';
26+
return new self($errorCode, $message);
27+
}
28+
}

src/Neo4jQueryAPI.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Neo4j\QueryAPI;
44

5+
use Exception;
56
use GuzzleHttp\Client;
67
use GuzzleHttp\Exception\GuzzleException;
78
use GuzzleHttp\Exception\RequestException;
9+
use Neo4j\QueryAPI\Exception\Neo4jException;
810
use RuntimeException;
911
use stdClass;
1012

@@ -38,16 +40,35 @@ public static function login(string $address, string $username, string $password
3840
*/
3941
public function run(string $cypher, array $parameters, string $database = 'neo4j'): array
4042
{
41-
$payload = [
42-
'statement' => $cypher,
43-
'parameters' => $parameters === [] ? new stdClass() : $parameters,
44-
];
43+
try {
44+
// Prepare the payload for the request
45+
$payload = [
46+
'statement' => $cypher,
47+
'parameters' => empty($parameters) ? new stdClass() : $parameters,
48+
];
4549

46-
$response = $this->client->post('/db/' . $database . '/query/v2', [
47-
'json' => $payload,
48-
]);
49-
return json_decode($response->getBody()->getContents(), true);
50-
}
50+
// Execute the request to the Neo4j server
51+
$response = $this->client->post('/db/' . $database . '/query/v2', [
52+
'json' => $payload,
53+
]);
5154

55+
// Decode the response body
56+
return json_decode($response->getBody()->getContents(), true);
57+
} catch (RequestException $e) {
58+
// Catch any HTTP request errors
59+
$errorResponse = [
60+
'code' => 'Neo.HttpRequestError',
61+
'message' => 'HTTP request failed: ' . $e->getMessage(),
62+
];
63+
throw Neo4jException::fromNeo4jResponse($errorResponse);
64+
} catch (Exception $e) {
65+
// Catch any other unexpected errors
66+
$errorResponse = [
67+
'code' => 'Neo.UnknownError',
68+
'message' => 'An unknown error occurred: ' . $e->getMessage(),
69+
];
70+
throw Neo4jException::fromNeo4jResponse($errorResponse);
71+
}
72+
}
5273

5374
}

src/Service/Neo4jClient.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Service;
4+
5+
use Neo4j\QueryAPI\Exception\Neo4jException;
6+
use Exception;
7+
8+
class Neo4jClient
9+
{
10+
public function executeQuery(string $query, array $parameters): void
11+
{
12+
try {
13+
// Simulated query execution
14+
// Replace this with actual query execution using your Neo4j client.
15+
throw new Exception(json_encode([
16+
'code' => 'Neo.DatabaseError.Database.UnableToStartDatabase',
17+
'message' => 'Unable to start database.'
18+
]));
19+
} catch (Exception $e) {
20+
$errorResponse = json_decode($e->getMessage(), true);
21+
22+
if (json_last_error() === JSON_ERROR_NONE) {
23+
throw Neo4jException::fromNeo4jResponse($errorResponse);
24+
}
25+
26+
// Fallback for unexpected exceptions
27+
throw new Neo4jException('Neo.UnknownError', $e->getMessage());
28+
}
29+
}
30+
}

tests/Integration/Neo4jOGMTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ public static function booleanDataProvider():array
6666
];
6767
}
6868

69+
public static function stringDataProvider():array
70+
{
71+
return [
72+
['query1', ['_value' => 'Hello, world!'], 'Hello, world!'],
73+
['query2', ['_value' => ''], ''], // Test empty string
74+
['query3', ['_value' => null], null], // Optional if null handling is needed
75+
];
76+
}
77+
6978

7079
public function setUp(): void
7180
{
@@ -146,7 +155,7 @@ public function testWithWGS84_2DPoint(): void
146155

147156
public function testWithWGS84_3DPoint(): void
148157
{
149-
// Simulate mapping the raw WKT data into the Point object
158+
150159
$point = $this->ogm->map([
151160
'$type' => 'Point',
152161
'_value' => 'SRID=4979;POINT Z (12.34 56.78 100.5)',
@@ -420,6 +429,16 @@ public function testWithBoolean(string $query, array $parameters, ?bool $expecte
420429
$this->assertEquals($expectedResult, $actual);
421430
}
422431

432+
#[DataProvider('stringDataProvider')]
433+
public function testWithString(string $query, array $parameters, ?string $expectedResult): void
434+
{
435+
$actual = $this->ogm->map([
436+
'$type' => 'String',
437+
'_value' => $parameters['_value'],
438+
]);
439+
$this->assertEquals($expectedResult, $actual);
440+
}
441+
423442

424443

425444
}

tests/Integration/Neo4jQueryAPIIntegrationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Neo4j\QueryAPI\Tests\Integration;
44

55
use GuzzleHttp\Exception\GuzzleException;
6+
use Neo4j\QueryAPI\Exception\Neo4jException;
67
use Neo4j\QueryAPI\Neo4jQueryAPI;
8+
use Neo4j\QueryAPI\Service\Neo4jClient;
79
use PHPUnit\Framework\Attributes\DataProvider;
810
use PHPUnit\Framework\TestCase;
911

@@ -79,6 +81,22 @@ public function testRunSuccessWithParameters(
7981
$this->assertEquals($expectedResults, $subsetResults);
8082
}
8183

84+
public function testInvalidQueryException(): void
85+
{
86+
$this->expectException(Neo4jException::class);
87+
88+
try {
89+
$client = new Neo4jClient();
90+
$client->executeQuery('CREATE (:Person {createdAt: $date})', [
91+
'date' => new \DateTime('2000-01-01 00:00:00')
92+
]);
93+
} catch (Neo4jException $e) {
94+
$this->assertEquals('Neo.DatabaseError.Database.UnableToStartDatabase', $e->getErrorCode());
95+
$this->assertEquals('Unable to start database.', $e->getMessage());
96+
throw $e;
97+
}
98+
}
99+
82100
private function createSubset(array $expected, array $actual): array
83101
{
84102
$subset = [];

0 commit comments

Comments
 (0)