Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Ddeboer\Imap\Exception; |
| 6 | |
| 7 | abstract class AbstractException extends \RuntimeException |
| 8 | { |
| 9 | /** |
| 10 | * @var array |
| 11 | */ |
| 12 | private static $errorLabels = [ |
| 13 | \E_ERROR => 'E_ERROR', |
| 14 | \E_WARNING => 'E_WARNING', |
| 15 | \E_PARSE => 'E_PARSE', |
| 16 | \E_NOTICE => 'E_NOTICE', |
| 17 | \E_CORE_ERROR => 'E_CORE_ERROR', |
| 18 | \E_CORE_WARNING => 'E_CORE_WARNING', |
| 19 | \E_COMPILE_ERROR => 'E_COMPILE_ERROR', |
| 20 | \E_COMPILE_WARNING => 'E_COMPILE_WARNING', |
| 21 | \E_USER_ERROR => 'E_USER_ERROR', |
| 22 | \E_USER_WARNING => 'E_USER_WARNING', |
| 23 | \E_USER_NOTICE => 'E_USER_NOTICE', |
| 24 | \E_STRICT => 'E_STRICT', |
| 25 | \E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', |
| 26 | \E_DEPRECATED => 'E_DEPRECATED', |
| 27 | \E_USER_DEPRECATED => 'E_USER_DEPRECATED', |
| 28 | ]; |
| 29 | |
| 30 | /** |
| 31 | * @param string $message The exception message |
| 32 | * @param int $code The exception code |
| 33 | * @param \Throwable $previous The previous exception |
| 34 | */ |
| 35 | final public function __construct(string $message, int $code = 0, \Throwable $previous = null) |
| 36 | { |
| 37 | $errorType = ''; |
| 38 | if (isset(self::$errorLabels[$code])) { |
| 39 | $errorType = \sprintf('[%s] ', self::$errorLabels[$code]); |
| 40 | } |
| 41 | |
| 42 | $joinString = "\n- "; |
| 43 | $alerts = \imap_alerts(); |
| 44 | $errors = \imap_errors(); |
| 45 | $completeMessage = \sprintf( |
| 46 | "%s%s\nimap_alerts (%s):%s\nimap_errors (%s):%s", |
| 47 | $errorType, |
| 48 | $message, |
| 49 | false !== $alerts ? \count($alerts) : 0, |
| 50 | false !== $alerts ? $joinString . \implode($joinString, $alerts) : '', |
| 51 | false !== $errors ? \count($errors) : 0, |
| 52 | false !== $errors ? $joinString . \implode($joinString, $errors) : '' |
| 53 | ); |
| 54 | |
| 55 | parent::__construct($completeMessage, $code, $previous); |
| 56 | } |
| 57 | } |