Handle exceptions in API

This commit is contained in:
Mike Jolley 2016-08-30 15:57:45 +01:00
parent 1d7c8d8370
commit b82415dfa3
1 changed files with 92 additions and 91 deletions

View File

@ -117,7 +117,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
@ -131,7 +130,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'create' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
@ -147,7 +145,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'read', $id ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
@ -163,7 +160,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'edit', $id ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
@ -179,7 +175,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'delete', $id ) ) {
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
@ -193,7 +188,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'batch' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
@ -301,8 +295,9 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
try {
if ( ! empty( $request['id'] ) ) {
return new WP_Error( 'woocommerce_rest_customer_exists', __( 'Cannot create existing resource.', 'woocommerce' ), array( 'status' => 400 ) );
throw new WC_REST_Exception( 'woocommerce_rest_customer_exists', __( 'Cannot create existing resource.', 'woocommerce' ), 400 );
}
// Sets the username.
@ -319,7 +314,7 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
$customer->create();
if ( ! $customer->get_id() ) {
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'This resource cannot be created.', 'woocommerce' ), array( 'status' => 400 ) );
throw new WC_REST_Exception( 'woocommerce_rest_cannot_create', __( 'This resource cannot be created.', 'woocommerce' ), 400 );
}
$this->update_customer_meta_fields( $customer, $request );
@ -344,6 +339,9 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $customer->get_id() ) ) );
return $response;
} catch ( Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
/**
@ -373,19 +371,20 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
try {
$id = (int) $request['id'];
$customer = new WC_Customer( $id );
if ( ! $customer->get_id() ) {
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 400 ) );
throw new WC_REST_Exception( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), 400 );
}
if ( ! empty( $request['email'] ) && email_exists( $request['email'] ) && $request['email'] !== $customer->get_email() ) {
return new WP_Error( 'woocommerce_rest_customer_invalid_email', __( 'Email address is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
throw new WC_REST_Exception( 'woocommerce_rest_customer_invalid_email', __( 'Email address is invalid.', 'woocommerce' ), 400 );
}
if ( ! empty( $request['username'] ) && $request['username'] !== $customer->get_username() ) {
return new WP_Error( 'woocommerce_rest_customer_invalid_argument', __( "Username isn't editable.", 'woocommerce' ), array( 'status' => 400 ) );
throw new WC_REST_Exception( 'woocommerce_rest_customer_invalid_argument', __( "Username isn't editable.", 'woocommerce' ), 400 );
}
// Customer email.
@ -417,6 +416,9 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
$response = $this->prepare_item_for_response( $user_data, $request );
$response = rest_ensure_response( $response );
return $response;
} catch ( Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
/**
@ -839,7 +841,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
*/
protected function get_role_names() {
global $wp_roles;
return array_keys( $wp_roles->role_names );
}