class-wc-api-exception.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * WooCommerce API Exception Class
  4. *
  5. * Extends Exception to provide additional data
  6. *
  7. * @author WooThemes
  8. * @category API
  9. * @package WooCommerce/API
  10. * @since 2.2
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit; // Exit if accessed directly
  14. }
  15. class WC_API_Exception extends Exception {
  16. /** @var string sanitized error code */
  17. protected $error_code;
  18. /**
  19. * Setup exception, requires 3 params:
  20. *
  21. * error code - machine-readable, e.g. `woocommerce_invalid_product_id`
  22. * error message - friendly message, e.g. 'Product ID is invalid'
  23. * http status code - proper HTTP status code to respond with, e.g. 400
  24. *
  25. * @since 2.2
  26. * @param string $error_code
  27. * @param string $error_message user-friendly translated error message
  28. * @param int $http_status_code HTTP status code to respond with
  29. */
  30. public function __construct( $error_code, $error_message, $http_status_code ) {
  31. $this->error_code = $error_code;
  32. parent::__construct( $error_message, $http_status_code );
  33. }
  34. /**
  35. * Returns the error code
  36. *
  37. * @since 2.2
  38. * @return string
  39. */
  40. public function getErrorCode() {
  41. return $this->error_code;
  42. }
  43. }