Skip to content

Commit

Permalink
Merge pull request woocommerce#12947 from woocommerce/improve-rest-ap…
Browse files Browse the repository at this point in the history
…i-error-data

Allow pass error data using WC_Data_Exception
  • Loading branch information
mikejolley authored Jan 24, 2017
2 parents 8768fc5 + bac0b29 commit eb2b9d7
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 33 deletions.
11 changes: 7 additions & 4 deletions includes/abstracts/abstract-wc-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,14 @@ protected function get_prop( $prop, $context = 'view' ) {

/**
* When invalid data is found, throw an exception unless reading from the DB.
* @param string $error_code Error code.
* @param string $error_message Error message.
*
* @throws WC_Data_Exception
* @param string $code Error code.
* @param string $message Error message.
* @param int $http_status_code HTTP status code.
* @param array $data Extra error data.
*/
protected function error( $error_code, $error_message ) {
throw new WC_Data_Exception( $error_code, $error_message );
protected function error( $code, $message, $http_status_code = 400, $data = array() ) {
throw new WC_Data_Exception( $code, $message, $http_status_code, $data );
}
}
4 changes: 3 additions & 1 deletion includes/abstracts/abstract-wc-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,9 @@ public function set_short_description( $short_description ) {
public function set_sku( $sku ) {
$sku = (string) $sku;
if ( $this->get_object_read() && ! empty( $sku ) && ! wc_product_has_unique_sku( $this->get_id(), $sku ) ) {
$this->error( 'product_invalid_sku', __( 'Invalid or duplicated SKU.', 'woocommerce' ) );
$sku_found = wc_get_product_id_by_sku( $sku );

$this->error( 'product_invalid_sku', __( 'Invalid or duplicated SKU.', 'woocommerce' ), 400, array( 'resource_id' => $sku_found ) );
}
$this->set_prop( 'sku', $sku );
}
Expand Down
12 changes: 6 additions & 6 deletions includes/abstracts/abstract-wc-rest-terms-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,15 @@ public function create_item( $request ) {

$term = wp_insert_term( $name, $taxonomy, $args );
if ( is_wp_error( $term ) ) {
$error_data = array( 'status' => 400 );

// If we're going to inform the client that the term exists, give them the identifier
// they can actually use.
if ( ( $term_id = $term->get_error_data( 'term_exists' ) ) ) {
$existing_term = get_term( $term_id, $taxonomy );
$term->add_data( $existing_term->term_id, 'term_exists' );
// If we're going to inform the client that the term exists,
// give them the identifier they can actually use.
if ( $term_id = $term->get_error_data( 'term_exists' ) ) {
$error_data['resource_id'] = $term_id;
}

return $term;
return new WP_Error( $term->get_error_code(), $term->get_error_message(), $error_data );
}

$term = get_term( $term['term_id'], $taxonomy );
Expand Down
2 changes: 1 addition & 1 deletion includes/api/class-wc-rest-coupons-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public function save_coupon( $request ) {
$coupon->save();
return $coupon->get_id();
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
Expand Down
4 changes: 2 additions & 2 deletions includes/api/class-wc-rest-orders-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ protected function create_order( $request ) {

return $order->get_id();
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
Expand Down Expand Up @@ -466,7 +466,7 @@ protected function update_order( $request ) {

return $order->get_id();
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
Expand Down
4 changes: 2 additions & 2 deletions includes/api/class-wc-rest-products-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ public function create_item( $request ) {
return $response;
} catch ( WC_Data_Exception $e ) {
$this->delete_post( $product_id );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
$this->delete_post( $product_id );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
Expand Down Expand Up @@ -805,7 +805,7 @@ public function update_item( $request ) {

return rest_ensure_response( $response );
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
Expand Down
54 changes: 37 additions & 17 deletions includes/class-wc-data-exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,65 @@
*
* Extends Exception to provide additional data.
*
* @author WooThemes
* @category Core
* @package WooCommerce
* @since 2.7
* @author WooThemes
* @category Core
* @package WooCommerce
* @since 2.7
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* WC_Data_Exception class
* WC_Data_Exception class.
*/
class WC_Data_Exception extends Exception {

/** @var string sanitized error code */
/**
* Sanitized error code.
*
* @var string
*/
protected $error_code;

/**
* Setup exception.
* Error extra data.
*
* error code - machine-readable, e.g. `woocommerce_invalid_product_id`
* error message - friendly message, e.g. 'Product ID is invalid'
* http status code - proper HTTP status code to respond with, e.g. 400
* @var array
*/
protected $error_data;

/**
* Setup exception.
*
* @param string $error_code
* @param string $error_message user-friendly translated error message
* @param int $http_status_code HTTP status code to respond with
* @param string $code Machine-readable error code, e.g `woocommerce_invalid_product_id`.
* @param string $message User-friendly translated error message, e.g. 'Product ID is invalid'.
* @param int $http_status_code Proper HTTP status code to respond with, e.g. 400.
* @param array $data Extra error data.
*/
public function __construct( $error_code, $error_message, $http_status_code = 400 ) {
$this->error_code = $error_code;
parent::__construct( $error_message, $http_status_code );
public function __construct( $code, $message, $http_status_code = 400, $data = array() ) {
$this->error_code = $code;
$this->error_data = array_merge( array( 'status' => $http_status_code ), $data );

parent::__construct( $message, $http_status_code );
}

/**
* Returns the error code
* Returns the error code.
*
* @return string
*/
public function getErrorCode() {
return $this->error_code;
}

/**
* Returns error data.
*
* @return array
*/
public function getErrorData() {
return $this->error_data;
}
}

0 comments on commit eb2b9d7

Please sign in to comment.