Skip to content

Bug/24286 create block error#24287

Merged
youknowriad merged 10 commits intoWordPress:masterfrom
ryanwelcher:bug/24286-create-block-error
Jul 30, 2020
Merged

Bug/24286 create block error#24287
youknowriad merged 10 commits intoWordPress:masterfrom
ryanwelcher:bug/24286-create-block-error

Conversation

@ryanwelcher
Copy link
Contributor

@ryanwelcher ryanwelcher commented Jul 30, 2020

closes #24286

Description

This PR implements the proposed solution indicated in #24286

How has this been tested?

Running `wp.blocks.createBlock('not/registered') in the console.

Screenshots

create-block-warning

Types of changes

This introduces a warning() call and an early return if the blockType is not registered

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.


if ( undefined === blockType ) {
warning( `Block type '${ name }' is not registered.` );
return false;
Copy link
Contributor

@youknowriad youknowriad Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this non-blocking changes the expectation from someone calling this function.

We do things like that in general in Gutenberg configs.map( config => createBlock( config.name, config.attributes, config.innerBlocks )

With the current implementation, this will hide bugs and just move them down the road. IMO, we should throw an exception here instead of calling warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowriad thanks for the review here! I have updated the PR to throw an exception.

@ryanwelcher ryanwelcher requested a review from youknowriad July 30, 2020 14:36
// Get the type definition associated with a registered block.
const blockType = getBlockType( name );

if ( undefined === blockType ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important but yoda conditions are not a guideline in JavaScript because we enforce strict equality.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR. The error message is way clearer this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createBlock fires error if name doesn't match a registered block type.

2 participants