Automattic\WooCommerce\StoreApi\Routes\V1

AbstractRoute::error_to_response()protectedWC 1.0

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

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.

AbstractRoute::error_to_response() code WC 9.3.3

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 );
}