| | 1 | <?php |
| | 2 | /** |
| | 3 | * WordPress Exception. |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | */ |
| | 7 | |
| | 8 | /** |
| | 9 | * WordPress Exception class. |
| | 10 | * |
| | 11 | * @since 6.3 |
| | 12 | */ |
| | 13 | class WP_Exception extends Exception { |
| | 14 | /** |
| | 15 | * Error code, compatible with WP_Error. |
| | 16 | * |
| | 17 | * @since 6.3 |
| | 18 | * |
| | 19 | * @var int|string Error code. |
| | 20 | */ |
| | 21 | protected $error_code = ''; |
| | 22 | |
| | 23 | /** |
| | 24 | * Error data, compatible with WP_Error. |
| | 25 | * |
| | 26 | * @since 6.3 |
| | 27 | * |
| | 28 | * @var mixed Data. |
| | 29 | */ |
| | 30 | protected $error_data = []; |
| | 31 | |
| | 32 | /** |
| | 33 | * Constructor. |
| | 34 | * |
| | 35 | * @since 6.3 |
| | 36 | * |
| | 37 | * @param int|string $error_code Error code. |
| | 38 | * @param string $error_message Error message. |
| | 39 | * @param mixed $error_data Error data. |
| | 40 | */ |
| | 41 | public function __construct( $error_code = '', $error_message = '', $error_data = [] ) { |
| | 42 | $this->error_code = $error_code; |
| | 43 | $this->error_data = $error_data; |
| | 44 | |
| | 45 | parent::__construct( $error_message ); |
| | 46 | } |
| | 47 | |
| | 48 | /** |
| | 49 | * Get error code. |
| | 50 | * |
| | 51 | * @since 6.3 |
| | 52 | * |
| | 53 | * @return int|string |
| | 54 | */ |
| | 55 | public function get_error_code() { |
| | 56 | return $this->error_code; |
| | 57 | } |
| | 58 | |
| | 59 | /** |
| | 60 | * Get error message. |
| | 61 | * |
| | 62 | * @since 6.3 |
| | 63 | * |
| | 64 | * @return string |
| | 65 | */ |
| | 66 | public function get_error_message() { |
| | 67 | return parent::getMessage(); |
| | 68 | } |
| | 69 | |
| | 70 | /** |
| | 71 | * Get error data. |
| | 72 | * |
| | 73 | * @since 6.3 |
| | 74 | * |
| | 75 | * @return mixed |
| | 76 | */ |
| | 77 | public function get_error_data() { |
| | 78 | return $this->error_data; |
| | 79 | } |
| | 80 | |
| | 81 | /** |
| | 82 | * Convert a WP_Error into this Exception. |
| | 83 | * |
| | 84 | * @since 6.3 |
| | 85 | * |
| | 86 | * @param WP_Error $wp_error WP_Error object. |
| | 87 | * |
| | 88 | * @return $this |
| | 89 | */ |
| | 90 | public function from_wp_error( $wp_error ) { |
| | 91 | $this->error_code = $wp_error->get_error_code(); |
| | 92 | $this->message = $wp_error->get_error_message(); |
| | 93 | $this->error_data = $wp_error->get_error_data(); |
| | 94 | |
| | 95 | return $this; |
| | 96 | } |
| | 97 | |
| | 98 | /** |
| | 99 | * Convert this exception into a WP_Error. |
| | 100 | * |
| | 101 | * @since 6.3 |
| | 102 | * |
| | 103 | * @return WP_Error |
| | 104 | */ |
| | 105 | public function to_wp_error() { |
| | 106 | return new WP_Error( |
| | 107 | $this->get_error_code(), |
| | 108 | $this->get_error_message(), |
| | 109 | $this->get_error_data() |
| | 110 | ); |
| | 111 | } |
| | 112 | } |