From 009a21af1516151779c69236c838283314bff9ec Mon Sep 17 00:00:00 2001 From: Manos Psychogyiopoulos Date: Wed, 10 Jan 2018 20:46:02 +0000 Subject: [PATCH] Fix exception handling in v2 update_additional_fields_for_object REST API calls --- .../abstract-wc-rest-crud-controller.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/includes/abstracts/abstract-wc-rest-crud-controller.php b/includes/abstracts/abstract-wc-rest-crud-controller.php index a9b82fce089..fee15e83e40 100644 --- a/includes/abstracts/abstract-wc-rest-crud-controller.php +++ b/includes/abstracts/abstract-wc-rest-crud-controller.php @@ -192,7 +192,15 @@ abstract class WC_REST_CRUD_Controller extends WC_REST_Posts_Controller { return $object; } - $this->update_additional_fields_for_object( $object, $request ); + try { + $this->update_additional_fields_for_object( $object, $request ); + } catch ( WC_Data_Exception $e ) { + $object->delete(); + return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); + } catch ( WC_REST_Exception $e ) { + $object->delete(); + return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } /** * Fires after a single object is created or updated via the REST API. @@ -231,7 +239,13 @@ abstract class WC_REST_CRUD_Controller extends WC_REST_Posts_Controller { return $object; } - $this->update_additional_fields_for_object( $object, $request ); + try { + $this->update_additional_fields_for_object( $object, $request ); + } catch ( WC_Data_Exception $e ) { + 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() ) ); + } /** * Fires after a single object is created or updated via the REST API.