Skip to content

Commit d67fc15

Browse files
authored
Refactored with better error & exception handling.
1 parent 163056f commit d67fc15

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/Math.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
namespace Phactor;
2929

30+
use \Phactor\MathException;
31+
3032
/**
3133
* Generic math trait used by all other Phactor classes and proxy for math objects.
3234
*
@@ -268,8 +270,9 @@ public function D2B($num)
268270
return $bin;
269271

270272
} catch (\Exception $e) {
271-
// TODO: Need to do something useful here instead of re-throwing the exception.
272-
throw $e;
273+
throw new MathException("Caught the following exception in Math::D2B(): " . $e->getMessage(), 0, $e);
274+
} catch (\Error $e) {
275+
throw new MathException("Fatal error in Math::D2B(): " . $e->getMessage(), 0, $e);
273276
}
274277
}
275278

@@ -278,7 +281,7 @@ public function D2B($num)
278281
*
279282
* @param string $hex
280283
* @return string
281-
* @throws \Exception
284+
* @throws \Phactor\MathException
282285
*/
283286
public function binConv($hex)
284287
{
@@ -292,7 +295,7 @@ public function binConv($hex)
292295
$hex = $this->addHexPrefix($this->prepAndClean($hex));
293296
break;
294297
default:
295-
throw new \Exception('Unknown data type passed to the binConv() function.');
298+
throw new MathException('Unknown data type passed to the binConv() function.');
296299
}
297300

298301
return strrev($this->encodeValue($hex, '256'));
@@ -303,7 +306,7 @@ public function binConv($hex)
303306
*
304307
* @param string $hex
305308
* @return string $return
306-
* @throws \Exception
309+
* @throws \Phactor\MathException
307310
*/
308311
public function encodeBase58($hex)
309312
{
@@ -312,7 +315,7 @@ public function encodeBase58($hex)
312315
try {
313316

314317
if (strlen($hex) % 2 != 0 || $this->Test($hex) != 'hex') {
315-
throw new \Exception('Uneven number of hex characters or invalid parameter passed to encodeBase58 function.');
318+
throw new MathException('Uneven number of hex characters or invalid parameter passed to encodeBase58 function.');
316319
}
317320

318321
$orighex = $hex;
@@ -326,8 +329,9 @@ public function encodeBase58($hex)
326329
return $return;
327330

328331
} catch (\Exception $e) {
329-
// TODO: Need to do something useful here instead of re-throwing the exception.
330-
throw $e;
332+
throw new MathException("Caught the following exception in Math::encodeBase58(): " . $e->getMessage(), 0, $e);
333+
} catch (\Error $e) {
334+
throw new MathException("Fatal error in Math::encodeBase58(): " . $e->getMessage(), 0, $e);
331335
}
332336
}
333337

@@ -336,7 +340,7 @@ public function encodeBase58($hex)
336340
*
337341
* @param string $base58
338342
* @return string $return
339-
* @throws \Exception
343+
* @throws \Phactor\MathException
340344
*/
341345
public function decodeBase58($base58)
342346
{
@@ -362,15 +366,17 @@ public function decodeBase58($base58)
362366
return (strlen($return) % 2 != 0) ? '0' . $return : $return;
363367

364368
} catch (\Exception $e) {
365-
// TODO: Need to do something useful here instead of re-throwing the exception.
366-
throw $e;
369+
throw new MathException("Caught the following exception in Math::decodeBase58(): " . $e->getMessage(), 0, $e);
370+
} catch (\Error $e) {
371+
throw new MathException("Fatal error in Math::decodeBase58(): " . $e->getMessage(), 0, $e);
367372
}
368373
}
369374

370375
/**
371376
* Internal function to make sure we can find an acceptable math extension to use here.
377+
* The preference is to use GMP but will use BC if that's not available.
372378
*
373-
* @throws \Exception
379+
* @throws \Phactor\MathException
374380
*/
375381
private function MathCheck()
376382
{
@@ -380,7 +386,7 @@ private function MathCheck()
380386
} else if (function_exists('bcadd')) {
381387
$this->math = new BC();
382388
} else {
383-
throw new \Exception('Both GMP and BC Math extensions are missing on this system! Please install one to use the Phactor math library.');
389+
throw new MathException('Both GMP and BC Math extensions are missing on this system! Please install one to use the Phactor math library.');
384390
}
385391
}
386392

@@ -392,7 +398,7 @@ private function MathCheck()
392398
*
393399
* @param array $params The array of parameters to check.
394400
* @return boolean Will only be true, otherwise throws \Exception
395-
* @throws \Exception
401+
* @throws \Phactor\MathException
396402
*/
397403
private function preOpMethodParamsCheck($params)
398404
{
@@ -401,7 +407,7 @@ private function preOpMethodParamsCheck($params)
401407
foreach ($params as $key => $value) {
402408
if ($this->numberCheck($value) === false) {
403409
$caller = debug_backtrace();
404-
throw new \Exception('Empty or invalid parameters passed to ' . $caller[count($caller) - 1]['function'] . ' function.');
410+
throw new MathException('Empty or invalid parameters passed to ' . $caller[count($caller) - 1]['function'] . ' function.');
405411
}
406412
}
407413
}
@@ -412,7 +418,7 @@ private function preOpMethodParamsCheck($params)
412418
* @param string $val A number to convert.
413419
* @param string $base The base to convert it into.
414420
* @return string The same number but in a different base.
415-
* @throws \Exception
421+
* @throws \Phactor\MathException
416422
*/
417423
private function encodeValue($val, $base)
418424
{
@@ -434,8 +440,9 @@ private function encodeValue($val, $base)
434440
return $new;
435441

436442
} catch (\Exception $e) {
437-
// TODO: Ditto here as well...
438-
throw $e;
443+
throw new MathException("Caught the following exception in Math::encodeValue(): " . $e->getMessage(), 0, $e);
444+
} catch (\Error $e) {
445+
throw new MathException("Fatal error in Math::encodeValue(): " . $e->getMessage(), 0, $e);
439446
}
440447
}
441448

0 commit comments

Comments
 (0)