WordPress at Your Fingertips
function is not described

Automattic\WooCommerce\Blocks\StoreApi\Routes

AbstractRoute::error_to_response() protected WC 1.0

Converts an error to a response object. Based on \WP_REST_Server.

{} It's a method of the class: AbstractRoute{}

No Hooks.

Return

WP_REST_Response. List of associative arrays with code and message keys.

Usage

// protected - for code of main (parent) or child class
$result = $this->error_to_response( $error );
$error(WP_Error) (required)
WP_Error instance.

Code of AbstractRoute::error_to_response() WC 5.6.0

protected function error_to_response( $error ) {
	$error_data = $error->get_error_data();
	$status     = isset( $error_data, $error_data['status'] ) ? $error_data['status'] : 500;
	$errors     = [];

	foreach ( (array) $error->errors as $code => $messages ) {
		foreach ( (array) $messages as $message ) {
			$errors[] = array(
				'code'    => $code,
				'message' => $message,
				'data'    => $error->get_error_data( $code ),
			);
		}
	}

	$data = array_shift( $errors );

	if ( count( $errors ) ) {
		$data['additional_errors'] = $errors;
	}

	return new \WP_REST_Response( $data, $status );
}