cs-eliseev /
base-exceptions
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace cse\base; |
||
| 6 | |||
| 7 | use Exception; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Class CseException |
||
| 11 | * |
||
| 12 | * @package cse\based\extend |
||
| 13 | */ |
||
| 14 | class CseExceptions extends Exception |
||
| 15 | { |
||
| 16 | protected const DEFAULT_ERROR_MSG = 'Unknown error'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private static $errorsMsg = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Get error msg |
||
| 25 | * |
||
| 26 | * @param int $code |
||
| 27 | * |
||
| 28 | * @return string |
||
| 29 | */ |
||
| 30 | public static function getErrorMsg(int $code): ?string |
||
| 31 | { |
||
| 32 | return static::$errorsMsg[$code] ?? static::DEFAULT_ERROR_MSG; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Throw new exception |
||
| 37 | * |
||
| 38 | * @param int $code |
||
| 39 | * @param string|null $msg |
||
| 40 | * |
||
| 41 | * @throws CseExceptions |
||
| 42 | */ |
||
| 43 | public static function throwException(int $code, ?string $msg = ''): void |
||
| 44 | { |
||
| 45 | throw new static(static::getErrorMsg($code) . (empty($msg) ? '' : $msg), $code); |
||
| 46 | } |
||
| 47 | } |