Automattic\WooCommerce\Internal\RestApi\Routes\V4

AbstractController::get_route_error_response_from_objectprotectedWC 10.2.0

Get route response when something went wrong and the supplied error is a WP_Error.

Method of the class: AbstractController{}

No Hooks.

Returns

WP_Error. WP Error object.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_route_error_response_from_object( $error_object, $http_status_code, $additional_data ): WP_Error;
$error_object(WP_Error) (required)
The WP_Error object containing the error.
$http_status_code(int)
HTTP status.
Default: 400
$additional_data(array)
Extra data (key value pairs) to expose in the error response.
Default: array()

Changelog

Since 10.2.0 Introduced.

AbstractController::get_route_error_response_from_object() code WC 10.4.3

protected function get_route_error_response_from_object( WP_Error $error_object, int $http_status_code = WP_Http::BAD_REQUEST, array $additional_data = array() ): WP_Error {
	if ( ! $error_object instanceof WP_Error ) {
		return $this->get_route_error_response( 'invalid_error_object', __( 'Invalid error object provided.', 'woocommerce' ), $http_status_code, $additional_data );
	}

	$error_object->add_data( array_merge( $additional_data, array( 'status' => $http_status_code ) ) );
	return $error_object;
}