← All changes
|
includes/classes/exceptions/ablocks-exception.php
+133
-0
2.7.7
→
2.13.0
View file →
| @@ -1,0 +1,133 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\Classes\Exceptions; | |
| 4 | + | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + die(); | |
| 7 | +} | |
| 8 | + | |
| 9 | +use Exception; | |
| 10 | +use Throwable; | |
| 11 | +use WP_Error; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * WP_Error compatible exception. | |
| 15 | + */ | |
| 16 | +class AblocksException extends Exception { | |
| 17 | + | |
| 18 | + protected string $wp_code = ''; | |
| 19 | + | |
| 20 | + protected $data = []; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * @var Throwable|AblocksException|null | |
| 24 | + */ | |
| 25 | + protected ?Throwable $previous = null; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * @param string $message | |
| 29 | + * @param string $wp_code | |
| 30 | + * @param mixed $data | |
| 31 | + * @param int $code | |
| 32 | + * @param ?Throwable $previous | |
| 33 | + */ | |
| 34 | + public function __construct( string $message, string $wp_code = '', $data = null, int $code = 0, ?Throwable $previous = null ) { | |
| 35 | + parent::__construct( $message, $code, $previous ); | |
| 36 | + | |
| 37 | + $this->wp_code = $wp_code ?: $this->get_class_code(); | |
| 38 | + $this->previous = $previous; | |
| 39 | + | |
| 40 | + if ( ! $data ) { | |
| 41 | + $data = []; | |
| 42 | + } | |
| 43 | + | |
| 44 | + if ( empty( $data['status'] ) ) { | |
| 45 | + $data['status'] = $code ? $code : 500; | |
| 46 | + } | |
| 47 | + | |
| 48 | + $this->data = $data; | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * @throws self | |
| 53 | + */ | |
| 54 | + public static function throw( string $message, $data = null, int $code = 0, ?Throwable $previous = null ) { | |
| 55 | + // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 56 | + throw new self( $message, '', $data, $code, $previous ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + protected function get_class_code(): string { | |
| 60 | + $code = str_replace( [ __NAMESPACE__, 'ABlocks', 'Exception' ], '', __CLASS__ ); | |
| 61 | + return $code ?: 'UNKNOWN-ERROR'; | |
| 62 | + } | |
| 63 | + | |
| 64 | + public function get_wp_error_code(): string { | |
| 65 | + return $this->wp_code; | |
| 66 | + } | |
| 67 | + | |
| 68 | + public function get_data( string $key = null ) { | |
| 69 | + if ( $key ) { | |
| 70 | + if ( isset( $this->data[ $key ] ) ) { | |
| 71 | + return $this->data[ $key ]; | |
| 72 | + } | |
| 73 | + | |
| 74 | + return null; | |
| 75 | + } | |
| 76 | + | |
| 77 | + return $this->data; | |
| 78 | + } | |
| 79 | + | |
| 80 | + public function set_data( array $data ) { | |
| 81 | + $this->data = $data; | |
| 82 | + } | |
| 83 | + | |
| 84 | + public function add_data( string $key, $value ) { | |
| 85 | + $this->data[ $key ] = $value; | |
| 86 | + } | |
| 87 | + | |
| 88 | + public function get_wp_error(): WP_Error { | |
| 89 | + return $this->toWpError(); | |
| 90 | + } | |
| 91 | + | |
| 92 | + public function toWpError(): WP_Error { | |
| 93 | + return new WP_Error( $this->wp_code, $this->getMessage(), $this->data ); | |
| 94 | + } | |
| 95 | + | |
| 96 | + public function get_previous_data(): ?array { | |
| 97 | + | |
| 98 | + if ( ! $this->previous ) { | |
| 99 | + return null; | |
| 100 | + } | |
| 101 | + | |
| 102 | + if ( is_a( $this->previous, self::class ) ) { | |
| 103 | + return $this->previous->__toArray(); | |
| 104 | + } | |
| 105 | + | |
| 106 | + return [ | |
| 107 | + 'code' => $this->previous, | |
| 108 | + 'type' => get_class( $this->previous ), | |
| 109 | + 'message' => $this->previous->getMessage(), | |
| 110 | + 'data' => null, | |
| 111 | + 'trace' => $this->previous->getTrace(), | |
| 112 | + ]; | |
| 113 | + } | |
| 114 | + | |
| 115 | + public function __toArray(): array { | |
| 116 | + return [ | |
| 117 | + 'code' => $this->wp_code, | |
| 118 | + 'type' => get_class( $this ), | |
| 119 | + 'message' => $this->getMessage(), | |
| 120 | + 'data' => $this->data, | |
| 121 | + 'trace' => $this->getTrace(), | |
| 122 | + 'previous' => $this->get_previous_data(), | |
| 123 | + ]; | |
| 124 | + } | |
| 125 | + | |
| 126 | + public function __toJsonString( $flags = 0, $depth = 512 ): string { | |
| 127 | + return wp_json_encode( $this->__toArray(), $flags, $depth ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + public static function from_wp_error( WP_Error $wp_error ): self { | |
| 131 | + return new self( $wp_error->get_error_message(), $wp_error->get_error_code(), $wp_error->get_error_data() ); | |
| 132 | + } | |
| 133 | +} | |