class-wc-data-exception.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * WooCommerce Data Exception Class
  4. *
  5. * Extends Exception to provide additional data.
  6. *
  7. * @package WooCommerce\Classes
  8. * @since 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Data exception class.
  13. */
  14. class WC_Data_Exception extends Exception {
  15. /**
  16. * Sanitized error code.
  17. *
  18. * @var string
  19. */
  20. protected $error_code;
  21. /**
  22. * Error extra data.
  23. *
  24. * @var array
  25. */
  26. protected $error_data;
  27. /**
  28. * Setup exception.
  29. *
  30. * @param string $code Machine-readable error code, e.g `woocommerce_invalid_product_id`.
  31. * @param string $message User-friendly translated error message, e.g. 'Product ID is invalid'.
  32. * @param int $http_status_code Proper HTTP status code to respond with, e.g. 400.
  33. * @param array $data Extra error data.
  34. */
  35. public function __construct( $code, $message, $http_status_code = 400, $data = array() ) {
  36. $this->error_code = $code;
  37. $this->error_data = array_merge( array( 'status' => $http_status_code ), $data );
  38. parent::__construct( $message, $http_status_code );
  39. }
  40. /**
  41. * Returns the error code.
  42. *
  43. * @return string
  44. */
  45. public function getErrorCode() {
  46. return $this->error_code;
  47. }
  48. /**
  49. * Returns error data.
  50. *
  51. * @return array
  52. */
  53. public function getErrorData() {
  54. return $this->error_data;
  55. }
  56. }