Skip to content

Commit

Permalink
Eliminates try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed Aug 23, 2023
1 parent fb913af commit 0e6c026
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 13 additions & 4 deletions lib/experimental/fonts/font-library/class-wp-font-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 0e6c026

Please sign in to comment.