4
4
5
5
use Http \Discovery \Psr17FactoryDiscovery ;
6
6
use Http \Discovery \Psr18ClientDiscovery ;
7
+ use http \Exception \RuntimeException ;
8
+ use Neo4j \QueryAPI \Exception \Neo4jException ;
7
9
use Psr \Http \Client \ClientInterface ;
8
10
use Neo4j \QueryAPI \Authentication \AuthenticateInterface ;
9
11
use Neo4j \QueryAPI \Objects \Authentication ;
10
12
use Neo4j \QueryAPI \Results \ResultSet ;
11
- use RuntimeException ;
13
+ use Psr \ Http \ Message \ ResponseInterface ;
12
14
use Psr \Http \Client \RequestExceptionInterface ;
13
15
14
16
final class Neo4jQueryAPI
@@ -47,23 +49,25 @@ public function run(string $cypher, array $parameters = []): ResultSet
47
49
{
48
50
$ request = $ this ->requestFactory ->buildRunQueryRequest ($ cypher , $ parameters );
49
51
52
+ $ response = $ this ->client ->sendRequest ($ request );
53
+
50
54
try {
51
55
$ response = $ this ->client ->sendRequest ($ request );
52
56
} catch (RequestExceptionInterface $ e ) {
53
- throw new RuntimeException ( ' Request failed: ' . $ e -> getMessage (), 0 , $ e );
57
+ $ this -> handleRequestException ( $ e );
54
58
}
55
-
56
59
return $ this ->responseParser ->parseRunQueryResponse ($ response );
57
60
}
58
61
59
62
public function beginTransaction (): Transaction
60
63
{
61
64
$ request = $ this ->requestFactory ->buildBeginTransactionRequest ();
65
+ $ response = $ this ->client ->sendRequest ($ request );
62
66
63
67
try {
64
68
$ response = $ this ->client ->sendRequest ($ request );
65
69
} catch (RequestExceptionInterface $ e ) {
66
- throw new RuntimeException ( ' Request failed: ' . $ e -> getMessage (), 0 , $ e );
70
+ $ this -> handleRequestException ( $ e );
67
71
}
68
72
69
73
$ clusterAffinity = $ response ->getHeaderLine ('neo4j-cluster-affinity ' );
@@ -79,4 +83,24 @@ public function beginTransaction(): Transaction
79
83
$ transactionId
80
84
);
81
85
}
86
+
87
+
88
+
89
+ /**
90
+ * Handles request exceptions by parsing error details and throwing a Neo4jException.
91
+ *
92
+ * @throws Neo4jException
93
+ */
94
+ private function handleRequestException (RequestExceptionInterface $ e ): void
95
+ {
96
+ // ✅ Corrected: Check if exception has a response
97
+ $ response = method_exists ($ e , 'getResponse ' ) ? $ e ->getResponse () : null ;
98
+
99
+ if ($ response instanceof ResponseInterface) {
100
+ $ errorResponse = json_decode ((string )$ response ->getBody (), true );
101
+ throw Neo4jException::fromNeo4jResponse ($ errorResponse , $ e );
102
+ }
103
+
104
+ throw new Neo4jException (['message ' => $ e ->getMessage ()], 500 , $ e );
105
+ }
82
106
}
0 commit comments