Base.php
39 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WonderPush\Errors; |
| 4 | |
| 5 | if (count(get_included_files()) === 1) { http_response_code(403); exit(); } // Prevent direct access |
| 6 | |
| 7 | /** |
| 8 | * Base class for WonderPush errors. |
| 9 | * |
| 10 | * The WonderPush API returns string error codes. |
| 11 | * This class exposes them as string, in addition to expose their parsed version with {@link getCode()}. |
| 12 | */ |
| 13 | abstract class Base extends \Exception { |
| 14 | |
| 15 | /** |
| 16 | * The default error message to use when none has been provided at construct time. |
| 17 | */ |
| 18 | const DEFAULT_MESSAGE = ''; |
| 19 | |
| 20 | protected $codeStr; |
| 21 | |
| 22 | public function __construct($message = '', $codeStr = '0', \Exception $previous = null) { |
| 23 | if ($message === '') { |
| 24 | $message = static::DEFAULT_MESSAGE; |
| 25 | } |
| 26 | parent::__construct($message, (int)$codeStr, $previous); |
| 27 | $this->codeStr = (string)$codeStr; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * The code as is has been returned by the API, as a string. |
| 32 | * @return string |
| 33 | */ |
| 34 | public function getCodeStr() { |
| 35 | return $this->codeStr; |
| 36 | } |
| 37 | |
| 38 | } |
| 39 |