Skip to content

Commit e166cb7

Browse files
committed
better exception handling
1 parent ad2140a commit e166cb7

12 files changed

+84
-30
lines changed

src/BlockFileReader.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace AndKom\Bitcoin\Blockchain;
66

77
use AndKom\BCDataStream\Reader;
8+
use AndKom\Bitcoin\Blockchain\Exception\IOException;
89

910
/**
1011
* Class BlockFileReader
@@ -18,30 +19,30 @@ class BlockFileReader
1819
* @param $fp
1920
* @param int|null $pos
2021
* @return Block
21-
* @throws Exception
22+
* @throws IOException
2223
*/
2324
public function readBlockFromFile($fp, int $pos = null): Block
2425
{
2526
if (!is_resource($fp)) {
26-
throw new Exception('Invalid file resource.');
27+
throw new IOException('Invalid file resource.');
2728
}
2829

2930
if ($pos && fseek($fp, $pos - 4) === false) {
30-
throw new Exception('Unable to seek block file.');
31+
throw new IOException('Unable to seek block file.');
3132
}
3233

3334
$size = fread($fp, 4);
3435

3536
if ($size === false) {
36-
throw new Exception('Unable to read block size.');
37+
throw new IOException('Unable to read block size.');
3738
}
3839

3940
$length = unpack('V', $size)[1];
4041

4142
$data = fread($fp, $length);
4243

4344
if ($data === false) {
44-
throw new Exception('Unable to read block data.');
45+
throw new IOException('Unable to read block data.');
4546
}
4647

4748
return Block::parse(new Reader($data));
@@ -51,14 +52,14 @@ public function readBlockFromFile($fp, int $pos = null): Block
5152
* @param string $file
5253
* @param int $pos
5354
* @return Block
54-
* @throws Exception
55+
* @throws IOException
5556
*/
5657
public function readBlock(string $file, int $pos): Block
5758
{
5859
$fp = fopen($file, 'r');
5960

6061
if (!$fp) {
61-
throw new Exception("Unable to open block file '$file'.");
62+
throw new IOException("Unable to open block file '$file'.");
6263
}
6364

6465
$block = $this->readBlockFromFile($fp, $pos);
@@ -71,14 +72,14 @@ public function readBlock(string $file, int $pos): Block
7172
/**
7273
* @param string $file
7374
* @return \Generator
74-
* @throws Exception
75+
* @throws IOException
7576
*/
7677
public function read(string $file): \Generator
7778
{
7879
$fp = fopen($file, 'r');
7980

8081
if (!$fp) {
81-
throw new Exception("Unable to open block file '$file'.");
82+
throw new IOException("Unable to open block file '$file'.");
8283
}
8384

8485
while (fread($fp, 4) == static::MAGIC) {

src/BlockInfo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace AndKom\Bitcoin\Blockchain;
66

77
use AndKom\BCDataStream\Reader;
8+
use AndKom\Bitcoin\Blockchain\Exception\Exception;
89

910
/**
1011
* Class BlockInfo

src/BlockchainReader.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace AndKom\Bitcoin\Blockchain;
66

7+
use AndKom\Bitcoin\Blockchain\Exception\Exception;
8+
use AndKom\Bitcoin\Blockchain\Exception\IOException;
9+
710
/**
811
* Class BlockchainReader
912
* @package AndKom\Bitcoin\Blockchain
@@ -74,6 +77,7 @@ public function getChainstate(): ChainstateReader
7477
* @param BlockInfo $blockInfo
7578
* @return Block
7679
* @throws Exception
80+
* @throws IOException
7781
*/
7882
public function getBlockByInfo(BlockInfo $blockInfo): Block
7983
{
@@ -86,6 +90,7 @@ public function getBlockByInfo(BlockInfo $blockInfo): Block
8690
* @param string $hash
8791
* @return Block
8892
* @throws Exception
93+
* @throws IOException
8994
*/
9095
public function getBlockByHash(string $hash): Block
9196
{
@@ -96,6 +101,7 @@ public function getBlockByHash(string $hash): Block
96101
* @param int $height
97102
* @return Block
98103
* @throws Exception
104+
* @throws IOException
99105
*/
100106
public function getBlockByHeight(int $height): Block
101107
{
@@ -105,8 +111,9 @@ public function getBlockByHeight(int $height): Block
105111
/**
106112
* @param int|null $minHeight
107113
* @param int|null $maxHeight
108-
* @return \Generator
109114
* @throws Exception
115+
* @throws IOException
116+
* @return \Generator
110117
*/
111118
public function readBlocks(int $minHeight = null, int $maxHeight = null): \Generator
112119
{
@@ -128,12 +135,12 @@ public function readBlocks(int $minHeight = null, int $maxHeight = null): \Gener
128135

129136
/**
130137
* @return \Generator
131-
* @throws Exception
138+
* @throws IOException
132139
*/
133140
public function readBlocksUnordered(): \Generator
134141
{
135142
if (!file_exists($this->blocksDir)) {
136-
throw new Exception("Blocks dir '{$this->blocksDir}' not found.");
143+
throw new IOException("Blocks dir '{$this->blocksDir}' not found.");
137144
}
138145

139146
$dir = dir($this->blocksDir);

src/ChainstateReader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace AndKom\Bitcoin\Blockchain;
66

77
use AndKom\BCDataStream\Reader;
8+
use AndKom\Bitcoin\Blockchain\Exception\Exception;
89

910
/**
1011
* Class ChainstateReader
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
declare(strict_types=1);
44

5-
namespace AndKom\Bitcoin\Blockchain;
5+
namespace AndKom\Bitcoin\Blockchain\Exception;
66

77
/**
88
* Class Exception
9-
* @package AndKom\Bitcoin\Blockchain
9+
* @package AndKom\Bitcoin\Blockchain\Exception
1010
*/
1111
class Exception extends \Exception
1212
{

src/Exception/IOException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Blockchain\Exception;
6+
7+
/**
8+
* Class ScriptException
9+
* @package AndKom\Bitcoin\Blockchain\Exception
10+
*/
11+
class IOException extends Exception
12+
{
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Blockchain\Exception;
6+
7+
/**
8+
* Class ScriptException
9+
* @package AndKom\Bitcoin\Blockchain\Exception
10+
*/
11+
class PublicKeyException extends Exception
12+
{
13+
}

src/Exception/ScriptException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Blockchain\Exception;
6+
7+
/**
8+
* Class ScriptException
9+
* @package AndKom\Bitcoin\Blockchain\Exception
10+
*/
11+
class ScriptException extends Exception
12+
{
13+
}

src/Index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace AndKom\Bitcoin\Blockchain;
66

7+
use AndKom\Bitcoin\Blockchain\Exception\Exception;
8+
79
/**
810
* Class Index
911
* @package AndKom\Bitcoin\Blockchain

src/IndexReader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace AndKom\Bitcoin\Blockchain;
66

77
use AndKom\BCDataStream\Reader;
8+
use AndKom\Bitcoin\Blockchain\Exception\Exception;
89

910
/**
1011
* Class IndexReader

0 commit comments

Comments
 (0)