From 0e6c026c87933f64dae78049cc9bb53c7fa8dffb Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 23 Aug 2023 13:17:24 -0500 Subject: [PATCH] Eliminates try/catch --- .../font-library/class-wp-font-collection.php | 17 +++++++++++++---- .../font-library/class-wp-font-library.php | 10 +++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-collection.php b/lib/experimental/fonts/font-library/class-wp-font-collection.php index 46897f0a99a90d..05206086fa0e32 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-collection.php +++ b/lib/experimental/fonts/font-library/class-wp-font-collection.php @@ -36,20 +36,29 @@ class WP_Font_Collection { * * @param string $id Font collection id. * @param array $config Font collection config options. - * @throws Exception If the required parameters are missing. + * @throws WP_Error When passed an invalid argument. */ public function __construct( $id, $config ) { if ( empty( $id ) && is_string( $id ) ) { - throw new Exception( 'Font Collection ID is required as a non-empty string.' ); + return new WP_Error( + 'font_collection_id_required', + __( 'Font Collection ID is required as a non-empty string.', 'gutenberg' ) + ); } if ( empty( $config ) ) { - throw new Exception( 'Font Collection config options is required as a non-empty array.' ); + return new WP_Error( + 'font_collection_config_required', + __( 'Font Collection config options is required as a non-empty array.', 'gutenberg' ) + ); } if ( empty( $config['data_json_file'] ) && is_string( $config['data_json_file'] ) ) { - throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' ); + return new WP_Error( + 'font_collection_data_json_file_required', + __( 'Font Collection config "data_json_file" option is required as a non-empty string.', 'gutenberg' ) + ); } $config['id'] = $id; diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index a342076ae7f26b..ca84f8a33dbabd 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -50,13 +50,13 @@ public static function register_font_collection( $id, $config ) { return new WP_Error( 'font_collection_registration_error', 'Font collection already registered.' ); } - try { - $new_collection = new WP_Font_Collection( $id, $config ); - self::$collections[ $id ] = $new_collection; + $new_collection = new WP_Font_Collection( $id, $config ); + if ( is_wp_error( $new_collection ) ) { return $new_collection; - } catch ( Exception $e ) { - return new WP_Error( 'font_collection_error', $e->getMessage() ); } + + self::$collections[ $id ] = $new_collection; + return $new_collection; } /**