|
2 | 2 |
|
3 | 3 | namespace Neo4j\QueryAPI;
|
4 | 4 |
|
| 5 | +use Exception; |
5 | 6 | use GuzzleHttp\Client;
|
6 | 7 | use GuzzleHttp\Exception\GuzzleException;
|
7 | 8 | use GuzzleHttp\Exception\RequestException;
|
| 9 | +use Neo4j\QueryAPI\Exception\Neo4jException; |
8 | 10 | use RuntimeException;
|
9 | 11 | use stdClass;
|
10 | 12 |
|
@@ -38,16 +40,35 @@ public static function login(string $address, string $username, string $password
|
38 | 40 | */
|
39 | 41 | public function run(string $cypher, array $parameters, string $database = 'neo4j'): array
|
40 | 42 | {
|
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 | + ]; |
45 | 49 |
|
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 | + ]); |
51 | 54 |
|
| 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 | + } |
52 | 73 |
|
53 | 74 | }
|
0 commit comments