Automattic\WooCommerce\StoreApi\Utilities

NoticeHandler::convert_notices_to_exceptions()public staticWC 1.0

Convert queued error notices into an exception.

For example, Payment methods may add error notices during validate_fields call to prevent checkout. Since we're not rendering notices at all, we need to convert them to exceptions.

This method will find the first error message and thrown an exception instead. Discards notices once complete.

Method of the class: NoticeHandler{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = NoticeHandler::convert_notices_to_exceptions( $error_code );
$error_code(string)
Error code for the thrown exceptions.
Default: 'unknown_server_error'

NoticeHandler::convert_notices_to_exceptions() code WC 8.7.0

public static function convert_notices_to_exceptions( $error_code = 'unknown_server_error' ) {
	if ( 0 === wc_notice_count( 'error' ) ) {
		wc_clear_notices();
		return;
	}

	$error_notices = wc_get_notices( 'error' );

	// Prevent notices from being output later on.
	wc_clear_notices();

	foreach ( $error_notices as $error_notice ) {
		throw new RouteException( $error_code, wp_strip_all_tags( $error_notice['notice'] ), 400 );
	}
}