ApiException.php
58 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Admin\Settings\Exceptions; |
| 5 | |
| 6 | /** |
| 7 | * ApiException class. |
| 8 | */ |
| 9 | class ApiException extends \Exception { |
| 10 | /** |
| 11 | * Sanitized error code. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | public string $error_code; |
| 16 | |
| 17 | /** |
| 18 | * Additional error data. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | public array $additional_data = array(); |
| 23 | |
| 24 | /** |
| 25 | * Setup exception. |
| 26 | * |
| 27 | * @param string $error_code Machine-readable error code, e.g `woocommerce_invalid_step_id`. |
| 28 | * @param string $message User-friendly translated error message, e.g. 'Step ID is invalid'. |
| 29 | * @param int $http_status_code Optional. Proper HTTP status code to respond with. |
| 30 | * Defaults to 400 (Bad request). |
| 31 | * @param array $additional_data Optional. Extra data (key value pairs) to expose in the error response. |
| 32 | * Defaults to empty array. |
| 33 | */ |
| 34 | public function __construct( string $error_code, string $message, int $http_status_code = 400, array $additional_data = array() ) { |
| 35 | $this->error_code = $error_code; |
| 36 | $this->additional_data = array_filter( (array) $additional_data ); |
| 37 | parent::__construct( $message, $http_status_code ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns the error code. |
| 42 | * |
| 43 | * @return string The machine-readable error code. |
| 44 | */ |
| 45 | public function getErrorCode(): string { |
| 46 | return $this->error_code; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns additional error data. |
| 51 | * |
| 52 | * @return array Extra data (key value pairs). |
| 53 | */ |
| 54 | public function getAdditionalData(): array { |
| 55 | return $this->additional_data; |
| 56 | } |
| 57 | } |
| 58 |