Skip to content

Commit 1c22676

Browse files
Merge pull request #40 from stefanak-michal/issue/24_remove_error_handler
Issue/24 remove error handler
2 parents f5a99bf + 6001507 commit 1c22676

File tree

19 files changed

+435
-361
lines changed

19 files changed

+435
-361
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Bolt
22
Bolt protocol library over TCP socket. Bolt protocol is primary used for communication with [Neo4j](https://neo4j.com/) Graph database. The documentation is available at [https://7687.org/](https://7687.org/).
33

4-
![](https://img.shields.io/badge/phpunit-passed-success) ![](https://img.shields.io/badge/coverage-77%25-green) ![](https://img.shields.io/github/stars/stefanak-michal/Bolt) ![](https://img.shields.io/packagist/dt/stefanak-michal/bolt) ![](https://img.shields.io/github/v/release/stefanak-michal/bolt) ![](https://img.shields.io/github/commits-since/stefanak-michal/bolt/latest)
4+
![](https://img.shields.io/badge/phpunit-passed-success) ![](https://img.shields.io/badge/coverage-86%25-green) ![](https://img.shields.io/github/stars/stefanak-michal/Bolt) ![](https://img.shields.io/packagist/dt/stefanak-michal/bolt) ![](https://img.shields.io/github/v/release/stefanak-michal/bolt) ![](https://img.shields.io/github/commits-since/stefanak-michal/bolt/latest)
55

66
## Version support
77
Bolt <= 4.1

src/Bolt.php

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace Bolt;
44

5+
use Bolt\error\{
6+
ConnectException,
7+
PackException,
8+
UnpackException
9+
};
510
use Exception;
611
use Bolt\PackStream\{IPacker, IUnpacker};
712
use Bolt\protocol\AProtocol;
@@ -47,22 +52,11 @@ final class Bolt
4752
*/
4853
private $version;
4954

50-
/**
51-
* @var int
52-
*/
53-
private $packStreamVersion = 1;
54-
5555
/**
5656
* @var string
5757
*/
5858
private $scheme = 'basic';
5959

60-
/**
61-
* Custom error handler instead of throwing Exceptions
62-
* @var callable (string message, string code)
63-
*/
64-
public static $errorHandler;
65-
6660
/**
6761
* Print debug info
6862
* @var bool
@@ -77,20 +71,7 @@ final class Bolt
7771
public function __construct(IConnection $connection)
7872
{
7973
$this->connection = $connection;
80-
81-
$packerClass = "\\Bolt\\PackStream\\v" . $this->packStreamVersion . "\\Packer";
82-
if (!class_exists($packerClass)) {
83-
Bolt::error('Requested PackStream version (' . $this->packStreamVersion . ') not yet implemented');
84-
} else {
85-
$this->packer = new $packerClass();
86-
}
87-
88-
$unpackerClass = "\\Bolt\\PackStream\\v" . $this->packStreamVersion . "\\Unpacker";
89-
if (!class_exists($unpackerClass)) {
90-
Bolt::error('Requested PackStream version (' . $this->packStreamVersion . ') not yet implemented');
91-
} else {
92-
$this->unpacker = new $unpackerClass();
93-
}
74+
$this->setPackStreamVersion();
9475
}
9576

9677
/**
@@ -106,10 +87,22 @@ public function setProtocolVersions(...$v): Bolt
10687
/**
10788
* @param int $version
10889
* @return Bolt
90+
* @throws Exception
10991
*/
11092
public function setPackStreamVersion(int $version = 1)
11193
{
112-
$this->packStreamVersion = $version;
94+
$packerClass = "\\Bolt\\PackStream\\v" . $version . "\\Packer";
95+
if (!class_exists($packerClass)) {
96+
throw new PackException('Requested PackStream version (' . $version . ') not yet implemented');
97+
}
98+
$this->packer = new $packerClass();
99+
100+
$unpackerClass = "\\Bolt\\PackStream\\v" . $version . "\\Unpacker";
101+
if (!class_exists($unpackerClass)) {
102+
throw new UnpackException('Requested PackStream version (' . $version . ') not yet implemented');
103+
}
104+
$this->unpacker = new $unpackerClass();
105+
113106
return $this;
114107
}
115108

@@ -146,16 +139,14 @@ private function handshake(): bool
146139

147140
$this->unpackProtocolVersion();
148141
if (empty($this->version)) {
149-
Bolt::error('Wrong version');
150-
return false;
142+
throw new ConnectException('Wrong version');
151143
}
152144

153145
$protocolClass = "\\Bolt\\protocol\\V" . str_replace('.', '_', $this->version);
154146
if (!class_exists($protocolClass)) {
155-
Bolt::error('Requested Protocol version (' . $this->version . ') not yet implemented');
156-
} else {
157-
$this->protocol = new $protocolClass($this->packer, $this->unpacker, $this->connection);
147+
throw new ConnectException('Requested Protocol version (' . $this->version . ') not yet implemented');
158148
}
149+
$this->protocol = new $protocolClass($this->packer, $this->unpacker, $this->connection);
159150

160151
return $this->protocol instanceof AProtocol;
161152
}
@@ -258,7 +249,8 @@ public function hello(string $name, string $user, string $password, array $routi
258249
The tx_metadata is a dictionary that can contain some metadata information, mainly used for logging.
259250
The mode specifies what kind of server the RUN message is targeting. For write access use "w" and for read access use "r". Defaults to write access if no mode is sent.
260251
The db specifies the database name for multi-database to select where the transaction takes place. If no db is sent or empty string it implies that it is the default database.</pre>
261-
* @return mixed Return false on error
252+
* @return array
253+
* @throws Exception
262254
*/
263255
public function run(string $statement, array $parameters = [], array $extra = [])
264256
{
@@ -272,7 +264,8 @@ public function run(string $statement, array $parameters = [], array $extra = []
272264
* @version <4
273265
* @param int $n The n specifies how many records to fetch. n=-1 will fetch all records.
274266
* @param int $qid The qid (query identification) specifies the result of which statement the operation should be carried out. (Explicit Transaction only). qid=-1 can be used to denote the last executed statement and if no ``.
275-
* @return mixed Array of records or false on error. Last array element is success message.
267+
* @return array
268+
* @throws Exception
276269
*/
277270
public function pullAll(int $n = -1, int $qid = -1)
278271
{
@@ -287,7 +280,8 @@ public function pullAll(int $n = -1, int $qid = -1)
287280
* @internal PULL_ALL alias
288281
* @param int $n The n specifies how many records to fetch. n=-1 will fetch all records.
289282
* @param int $qid The qid (query identification) specifies the result of which statement the operation should be carried out. (Explicit Transaction only). qid=-1 can be used to denote the last executed statement and if no ``.
290-
* @return mixed Array of records or false on error. Last array element is success message.
283+
* @return array Array of records. Last array element is success message.
284+
* @throws Exception
291285
*/
292286
public function pull(int $n = -1, int $qid = -1)
293287
{
@@ -300,6 +294,7 @@ public function pull(int $n = -1, int $qid = -1)
300294
* @param int $n The n specifies how many records to throw away. n=-1 will throw away all records.
301295
* @param int $qid The qid (query identification) specifies the result of which statement the operation should be carried out. (Explicit Transaction only). qid=-1 can be used to denote the last executed statement and if no ``.
302296
* @return bool
297+
* @throws Exception
303298
*/
304299
public function discardAll(int $n = -1, int $qid = -1)
305300
{
@@ -315,6 +310,7 @@ public function discardAll(int $n = -1, int $qid = -1)
315310
* @param int $n The n specifies how many records to throw away. n=-1 will throw away all records.
316311
* @param int $qid The qid (query identification) specifies the result of which statement the operation should be carried out. (Explicit Transaction only). qid=-1 can be used to denote the last executed statement and if no ``.
317312
* @return bool
313+
* @throws Exception
318314
*/
319315
public function discard(int $n = -1, int $qid = -1): bool
320316
{
@@ -330,6 +326,7 @@ public function discard(int $n = -1, int $qid = -1): bool
330326
The mode specifies what kind of server the RUN message is targeting. For write access use "w" and for read access use "r". Defaults to write access if no mode is sent.
331327
The db specifies the database name for multi-database to select where the transaction takes place. If no db is sent or empty string it implies that it is the default database.</pre>
332328
* @return bool
329+
* @throws Exception
333330
*/
334331
public function begin(array $extra = []): bool
335332
{
@@ -341,6 +338,7 @@ public function begin(array $extra = []): bool
341338
/**
342339
* Send COMMIT message
343340
* @return bool
341+
* @throws Exception
344342
*/
345343
public function commit(): bool
346344
{
@@ -352,6 +350,7 @@ public function commit(): bool
352350
/**
353351
* Send ROLLBACK message
354352
* @return bool
353+
* @throws Exception
355354
*/
356355
public function rollback(): bool
357356
{
@@ -363,6 +362,7 @@ public function rollback(): bool
363362
/**
364363
* Send RESET message
365364
* @return bool
365+
* @throws Exception
366366
*/
367367
public function reset(): bool
368368
{
@@ -371,24 +371,6 @@ public function reset(): bool
371371
return $this->protocol->reset();
372372
}
373373

374-
/**
375-
* Process error
376-
* @param string $msg
377-
* @param string $code
378-
* @throws Exception
379-
*/
380-
public static function error(string $msg, string $code = '')
381-
{
382-
if (is_callable(self::$errorHandler)) {
383-
call_user_func(self::$errorHandler, $msg, $code);
384-
} else {
385-
if (!empty($code)) {
386-
$msg .= ' (' . $code . ')';
387-
}
388-
throw new Exception($msg);
389-
}
390-
}
391-
392374
/**
393375
* Say goodbye
394376
*/

src/PackStream/v1/Packer.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Bolt\PackStream\v1;
44

5-
use Exception;
65
use Bolt\PackStream\IPacker;
6+
use Bolt\error\PackException;
77

88
/**
99
* Class Packer of PackStream version 1
@@ -24,7 +24,7 @@ class Packer implements IPacker
2424
* @param $signature
2525
* @param mixed ...$params
2626
* @return string
27-
* @throws Exception
27+
* @throws PackException
2828
*/
2929
public function pack($signature, ...$params): string
3030
{
@@ -39,7 +39,7 @@ public function pack($signature, ...$params): string
3939
} elseif ($length < self::LARGE) { //STRUCT_16
4040
$output .= chr(0xDD) . pack('n', $length);
4141
} else {
42-
throw new Exception('Too many parameters');
42+
throw new PackException('Too many parameters');
4343
}
4444

4545
$output .= chr($signature);
@@ -55,7 +55,7 @@ public function pack($signature, ...$params): string
5555
/**
5656
* @param mixed $param
5757
* @return string
58-
* @throws Exception
58+
* @throws PackException
5959
*/
6060
private function p($param): string
6161
{
@@ -80,7 +80,7 @@ private function p($param): string
8080
} elseif (is_object($param)) {
8181
$output .= $this->packMap((array)$param);
8282
} else {
83-
throw new Exception('Not recognized type of parameter');
83+
throw new PackException('Not recognized type of parameter');
8484
}
8585

8686
return $output;
@@ -89,7 +89,7 @@ private function p($param): string
8989
/**
9090
* @param string $str
9191
* @return string
92-
* @throws Exception
92+
* @throws PackException
9393
*/
9494
private function packString(string $str): string
9595
{
@@ -105,7 +105,7 @@ private function packString(string $str): string
105105
} elseif ($length < self::HUGE) { //STRING_32
106106
$output .= chr(0xD2) . pack('N', $length) . $str;
107107
} else {
108-
throw new Exception('String too long');
108+
throw new PackException('String too long');
109109
}
110110

111111
return $output;
@@ -123,7 +123,7 @@ private function packFloat(float $value): string
123123
/**
124124
* @param int $value
125125
* @return string
126-
* @throws Exception
126+
* @throws PackException
127127
*/
128128
private function packInteger(int $value): string
129129
{
@@ -150,7 +150,7 @@ private function packInteger(int $value): string
150150
$packed = pack('q', $value);
151151
$output .= chr(0xCB) . ($little ? strrev($packed) : $packed);
152152
} else {
153-
throw new Exception('Integer out of range');
153+
throw new PackException('Integer out of range');
154154
}
155155

156156
return $output;
@@ -159,7 +159,7 @@ private function packInteger(int $value): string
159159
/**
160160
* @param array $arr
161161
* @return string
162-
* @throws Exception
162+
* @throws PackException
163163
*/
164164
private function packMap(array $arr): string
165165
{
@@ -175,7 +175,7 @@ private function packMap(array $arr): string
175175
} elseif ($size < self::HUGE) { //MAP_32
176176
$output .= chr(0xDA) . pack('N', $size);
177177
} else {
178-
throw new Exception('Too many map elements');
178+
throw new PackException('Too many map elements');
179179
}
180180

181181
foreach ($arr as $k => $v) {
@@ -189,7 +189,7 @@ private function packMap(array $arr): string
189189
/**
190190
* @param array $arr
191191
* @return string
192-
* @throws Exception
192+
* @throws PackException
193193
*/
194194
private function packList(array $arr): string
195195
{
@@ -205,7 +205,7 @@ private function packList(array $arr): string
205205
} elseif ($size < self::HUGE) { //LIST_32
206206
$output .= chr(0xD6) . pack('N', $size);
207207
} else {
208-
throw new Exception('Too many list elements');
208+
throw new PackException('Too many list elements');
209209
}
210210

211211
foreach ($arr as $v) {

0 commit comments

Comments
 (0)