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' ) ) { 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 new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
} }
@ -131,7 +130,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'create' ) ) { 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 new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
} }
@ -147,7 +145,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'read', $id ) ) { 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 new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
} }
return true; return true;
} }
@ -163,7 +160,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'edit', $id ) ) { 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 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; return true;
} }
@ -179,7 +175,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'delete', $id ) ) { 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 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; return true;
} }
@ -193,7 +188,6 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
if ( ! wc_rest_check_user_permissions( 'batch' ) ) { 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 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; return true;
} }
@ -301,8 +295,9 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
public function create_item( $request ) { public function create_item( $request ) {
try {
if ( ! empty( $request['id'] ) ) { 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. // Sets the username.
@ -319,7 +314,7 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
$customer->create(); $customer->create();
if ( ! $customer->get_id() ) { 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 ); $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() ) ) ); $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $customer->get_id() ) ) );
return $response; 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 * @return WP_Error|WP_REST_Response
*/ */
public function update_item( $request ) { public function update_item( $request ) {
try {
$id = (int) $request['id']; $id = (int) $request['id'];
$customer = new WC_Customer( $id ); $customer = new WC_Customer( $id );
if ( ! $customer->get_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() ) { 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() ) { 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. // 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 = $this->prepare_item_for_response( $user_data, $request );
$response = rest_ensure_response( $response ); $response = rest_ensure_response( $response );
return $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() { protected function get_role_names() {
global $wp_roles; global $wp_roles;
return array_keys( $wp_roles->role_names ); return array_keys( $wp_roles->role_names );
} }