-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathResultNext.php
More file actions
116 lines (98 loc) · 4.51 KB
/
ResultNext.php
File metadata and controls
116 lines (98 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
/*
* This file is part of the Neo4j PHP Client and Driver package.
*
* (c) Nagels <https://nagels.tech>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Laudis\Neo4j\TestkitBackend\Handlers;
use Bolt\error\BoltException;
use Laudis\Neo4j\Databags\Neo4jError;
use Laudis\Neo4j\Exception\Neo4jException;
use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface;
use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface;
use Laudis\Neo4j\TestkitBackend\MainRepository;
use Laudis\Neo4j\TestkitBackend\Requests\ResultNextRequest;
use Laudis\Neo4j\TestkitBackend\Responses\DriverErrorResponse;
use Laudis\Neo4j\TestkitBackend\Responses\NullRecordResponse;
use Laudis\Neo4j\TestkitBackend\Responses\RecordResponse;
use Laudis\Neo4j\TestkitBackend\Responses\Types\CypherObject;
use Throwable;
/**
* @implements RequestHandlerInterface<ResultNextRequest>
*/
final class ResultNext implements RequestHandlerInterface
{
private MainRepository $repository;
public function __construct(MainRepository $repository)
{
$this->repository = $repository;
}
/**
* @param ResultNextRequest $request
*/
public function handle($request): TestkitResponseInterface
{
try {
$record = $this->repository->getRecords($request->getResultId());
if ($record instanceof TestkitResponseInterface) {
return $record;
}
$iterator = $this->repository->getIterator($request->getResultId());
// Defer Iterator::next() until here so the Bolt stream is not advanced (e.g. second PULL)
// until the client asks for the next record — required for disconnect stubs and Result.list().
$this->repository->drainPendingIteratorNexts($request->getResultId(), $iterator);
// Check if iterator is valid - this may trigger generator to start and fetch results
// If the connection is closed, this will throw an exception which we catch below
if (!$iterator->valid()) {
return new NullRecordResponse();
}
// Get the current record
$current = $iterator->current();
$this->repository->setIteratorFetchedFirst($request->getResultId(), true);
$values = [];
foreach ($current as $value) {
$values[] = CypherObject::autoDetect($value);
}
$this->repository->addPendingIteratorNext($request->getResultId());
return new RecordResponse($values);
} catch (Neo4jException $e) {
$response = new DriverErrorResponse($request->getResultId(), $e);
$this->repository->addRecords($request->getResultId(), $response);
return $response;
} catch (BoltException $e) {
$neo4jError = Neo4jError::fromMessageAndCode('Neo.ClientError.General.ConnectionError', $e->getMessage());
$wrapped = new Neo4jException([$neo4jError], $e);
$response = new DriverErrorResponse($request->getResultId(), $wrapped);
$this->repository->addRecords($request->getResultId(), $response);
return $response;
} catch (Throwable $e) {
if ($this->isConnectionOrSocketError($e)) {
$neo4jError = Neo4jError::fromMessageAndCode('Neo.ClientError.General.ConnectionError', $e->getMessage());
$wrapped = new Neo4jException([$neo4jError], $e);
$response = new DriverErrorResponse($request->getResultId(), $wrapped);
$this->repository->addRecords($request->getResultId(), $response);
return $response;
}
throw $e;
}
}
private function isConnectionOrSocketError(Throwable $e): bool
{
$message = strtolower($e->getMessage());
return str_contains($message, 'broken pipe')
|| str_contains($message, 'connection reset')
|| str_contains($message, 'connection refused')
|| str_contains($message, 'connection closed')
|| str_contains($message, 'connection is closed')
|| str_contains($message, 'interrupted system call')
|| str_contains($message, 'i/o error')
|| str_contains($message, 'network read incomplete')
|| str_contains($message, 'network write incomplete')
|| str_contains($message, 'socket')
|| str_contains($message, 'broken');
}
}