Replaced REST exception with a custom exception and handle it in REST controllers.

This commit is contained in:
Peter Fabian 2019-01-29 12:13:52 +01:00
parent e4e09b4c39
commit c2eaf6d71c
9 changed files with 45 additions and 11 deletions

View File

@ -62,7 +62,11 @@ class WC_Admin_REST_Reports_Coupons_Stats_Controller extends WC_REST_Reports_Con
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$coupons_query = new WC_Admin_Reports_Coupons_Stats_Query( $query_args );
$report_data = $coupons_query->get_data();
try {
$report_data = $coupons_query->get_data();
} catch ( WC_Admin_Reports_Parameter_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),

View File

@ -70,7 +70,11 @@ class WC_Admin_REST_Reports_Orders_Stats_Controller extends WC_Admin_REST_Report
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$orders_query = new WC_Admin_Reports_Orders_Stats_Query( $query_args );
$report_data = $orders_query->get_data();
try {
$report_data = $orders_query->get_data();
} catch ( WC_Admin_Reports_Parameter_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),

View File

@ -74,8 +74,12 @@ class WC_Admin_REST_Reports_Products_Stats_Controller extends WC_REST_Reports_Co
}
}
$query = new WC_Admin_Reports_Products_Stats_Query( $query_args );
$report_data = $query->get_data();
$query = new WC_Admin_Reports_Products_Stats_Query( $query_args );
try {
$report_data = $query->get_data();
} catch ( WC_Admin_Reports_Parameter_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),

View File

@ -60,7 +60,11 @@ class WC_Admin_REST_Reports_Revenue_Stats_Controller extends WC_REST_Reports_Con
public function get_items( $request ) {
$query_args = $this->prepare_reports_query( $request );
$reports_revenue = new WC_Admin_Reports_Revenue_Query( $query_args );
$report_data = $reports_revenue->get_data();
try {
$report_data = $reports_revenue->get_data();
} catch ( WC_Admin_Reports_Parameter_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
$out_data = array(
'totals' => get_object_vars( $report_data->totals ),

View File

@ -110,6 +110,9 @@ class WC_Admin_Api_Init {
// Common date time code.
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-interval.php';
// Exceptions.
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-parameter-exception.php';
// Segmentation.
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-segmenting.php';
require_once dirname( __FILE__ ) . '/class-wc-admin-reports-orders-stats-segmenting.php';

View File

@ -244,7 +244,7 @@ class WC_Admin_Reports_Coupons_Stats_Segmenting extends WC_Admin_Reports_Segment
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
*
* @return array
* @throws WC_REST_Exception In case of segmenting by variations, when no parent product is specified.
* @throws WC_Admin_Reports_Parameter_Exception In case of segmenting by variations, when no parent product is specified.
*/
protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb;
@ -271,7 +271,7 @@ class WC_Admin_Reports_Coupons_Stats_Segmenting extends WC_Admin_Reports_Segment
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'variation' === $this->query_args['segmentby'] ) {
if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ), 400 );
throw new WC_Admin_Reports_Parameter_Exception( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ) );
}
$segmenting_selections = array(

View File

@ -335,7 +335,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
*
* @return array
* @throws WC_REST_Exception In case of segmenting by variations, when no parent product is specified.
* @throws WC_Admin_Reports_Parameter_Exception In case of segmenting by variations, when no parent product is specified.
*/
protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb;
@ -363,7 +363,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'variation' === $this->query_args['segmentby'] ) {
if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ), 400 );
throw new WC_Admin_Reports_Parameter_Exception( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ) );
}
$segmenting_selections = array(

View File

@ -0,0 +1,15 @@
<?php
/**
* WooCommerce Admin Input Parameter Exception Class
*
* Exception class thrown when user provides incorrect parameters.
*
* @package WooCommerce Admin
*/
defined( 'ABSPATH' ) || exit;
/**
* WC_Admin_Reports_Parameter_Exception class.
*/
class WC_Admin_Reports_Parameter_Exception extends WC_Data_Exception {}

View File

@ -131,7 +131,7 @@ class WC_Admin_Reports_Products_Stats_Segmenting extends WC_Admin_Reports_Segmen
* @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
*
* @return array
* @throws WC_REST_Exception In case of segmenting by variations, when no parent product is specified.
* @throws WC_Admin_Reports_Parameter_Exception In case of segmenting by variations, when no parent product is specified.
*/
protected function get_segments( $type, $query_params, $table_name ) {
global $wpdb;
@ -157,7 +157,7 @@ class WC_Admin_Reports_Products_Stats_Segmenting extends WC_Admin_Reports_Segmen
$segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
} elseif ( 'variation' === $this->query_args['segmentby'] ) {
if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ), 400 );
throw new WC_Admin_Reports_Parameter_Exception( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ) );
}
$segmenting_selections = array(