Automattic\WooCommerce\Internal\PushNotifications\Controllers

PushTokenRestController::convert_exception_to_wp_errorprivateWC 10.6.0

Converts an exception to an instance of WP_Error.

Method of the class: PushTokenRestController{}

No Hooks.

Returns

WP_Error.

Usage

// private - for code of main (parent) class only
$result = $this->convert_exception_to_wp_error( $e ): WP_Error;
$e(Exception) (required)
The exception to convert.

Changelog

Since 10.6.0 Introduced.

PushTokenRestController::convert_exception_to_wp_error() code WC 10.7.0

private function convert_exception_to_wp_error( Exception $e ): WP_Error {
	/**
	 * If the exception is `WC_Data_Exception`, and doesn't represent an
	 * internal server error (which may contain internal details that should
	 * be obscured) then format it as a `WP_Error`.
	 */
	if (
		$e instanceof WC_Data_Exception
		&& $e->getCode() !== WP_Http::INTERNAL_SERVER_ERROR
	) {
		return new WP_Error(
			$e->getErrorCode(),
			$e->getMessage(),
			$e->getErrorData()
		);
	}

	wc_get_container()
		->get( LegacyProxy::class )
		->call_function( 'wc_get_logger' )
		->error( (string) $e->getMessage(), array( 'source' => PushNotifications::FEATURE_NAME ) );

	return new WP_Error(
		'woocommerce_internal_error',
		'Internal server error',
		array( 'status' => WP_Http::INTERNAL_SERVER_ERROR )
	);
}