Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/24286 create block error #24287

Merged
merged 10 commits into from Jul 30, 2020

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.
*/
export function createBlock( name, attributes = {}, innerBlocks = [] ) {
// Get the type definition associated with a registered block.
const blockType = getBlockType( name );

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

This comment has been minimized.

@youknowriad

youknowriad Jul 30, 2020
Contributor

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.

This comment has been minimized.

@ryanwelcher

ryanwelcher Jul 30, 2020
Author Contributor

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

@ryanwelcher ryanwelcher requested a review from youknowriad Jul 30, 2020
@@ -46,6 +46,10 @@ export function createBlock( name, attributes = {}, innerBlocks = [] ) {
// Get the type definition associated with a registered block.
const blockType = getBlockType( name );

if ( undefined === blockType ) {

This comment has been minimized.

@youknowriad

youknowriad Jul 30, 2020
Contributor

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

Copy link
Contributor

@youknowriad youknowriad left a comment

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

@youknowriad youknowriad merged commit 90df863 into WordPress:master Jul 30, 2020
14 checks passed
14 checks passed
Cancel Previous Runs
Details
Check Check
Details
build
Details
Admin - 1
Details
Compare performance with master
Details
test (gutenberg-editor-gallery)
Details
test (gutenberg-editor-gallery)
Details
All
Details
JavaScript
Details
Admin - 2
Details
PHP
Details
Admin - 3
Details
Mobile
Details
Admin - 4
Details
@github-actions github-actions bot added this to the Gutenberg 8.7 milestone Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

2 participants
You can’t perform that action at this time.