CacheException.php
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Caching; |
| 4 | |
| 5 | /** |
| 6 | * Exception thrown by classes derived from ObjectCache. |
| 7 | */ |
| 8 | class CacheException extends \Exception { |
| 9 | |
| 10 | /** |
| 11 | * Error messages. |
| 12 | * |
| 13 | * @var array |
| 14 | */ |
| 15 | private $errors; |
| 16 | |
| 17 | /** |
| 18 | * The object that threw the exception. |
| 19 | * |
| 20 | * @var ObjectCache |
| 21 | */ |
| 22 | private $thrower; |
| 23 | |
| 24 | /** |
| 25 | * The id of the cached object, if available. |
| 26 | * |
| 27 | * @var int|string|null |
| 28 | */ |
| 29 | private $cached_id; |
| 30 | |
| 31 | /** |
| 32 | * Creates a new instance of the class. |
| 33 | * |
| 34 | * @param string $message The exception message. |
| 35 | * @param ObjectCache $thrower The object that is throwing the exception. |
| 36 | * @param int|string|null $cached_id The involved cached object id, if available. |
| 37 | * @param array|null $errors An array of error messages, if available. |
| 38 | * @param mixed $code An error code, if available. |
| 39 | * @param \Throwable|null $previous The previous exception, if available. |
| 40 | */ |
| 41 | public function __construct( string $message, ObjectCache $thrower, $cached_id = null, ?array $errors = null, $code = 0, ?\Throwable $previous = null ) { |
| 42 | $this->errors = $errors ?? array(); |
| 43 | $this->thrower = $thrower; |
| 44 | $this->cached_id = $cached_id; |
| 45 | |
| 46 | parent::__construct( $message, $code, $previous ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get a string representation of the exception object. |
| 51 | * |
| 52 | * @return string String representation of the exception object. |
| 53 | */ |
| 54 | public function __toString(): string { |
| 55 | $cached_id_part = $this->cached_id ? ", id: {$this->cached_id}" : ''; |
| 56 | return "CacheException: [{$this->thrower->get_object_type()}{$cached_id_part}]: {$this->message}"; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gets the array of error messages passed to the exception constructor. |
| 61 | * |
| 62 | * @return array Error messages passed to the exception constructor. |
| 63 | */ |
| 64 | public function get_errors(): array { |
| 65 | return $this->errors; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Gets the object that threw the exception as passed to the exception constructor. |
| 70 | * |
| 71 | * @return object The object that threw the exception. |
| 72 | */ |
| 73 | public function get_thrower(): object { |
| 74 | return $this->thrower; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets the id of the cached object as passed to the exception constructor. |
| 79 | * |
| 80 | * @return int|string|null The id of the cached object. |
| 81 | */ |
| 82 | public function get_cached_id() { |
| 83 | return $this->cached_id; |
| 84 | } |
| 85 | } |
| 86 |