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

Broken block editor because of uncatched fatal error in field JSON serialization #78

Closed
rvdsteege opened this issue Oct 14, 2022 · 1 comment · Fixed by #81
Closed
Assignees

Comments

@rvdsteege
Copy link
Member

I just came across a broken block editor (Classic block) because a fatal error occurred during JSON serialization of a field.

/**
* Serialize to JSON.
*
* @return array
*/
public function jsonSerialize() {
$data = parent::jsonSerialize();
$data['type'] = 'select';
$data['options'] = $this->get_flat_options();
return $data;
}

The gateway configuration had invalid credentials and the option were retrieved through a IDealIssuerSelectField class.

The issue was triggered from WooCommerce via the JSON encode of $this->data in https://github.com/woocommerce/woocommerce-blocks/blob/2ff8573d5426ebee180e50539f113c211362d1b9/src/Assets/AssetDataRegistry.php#L348-L373

Internal Help Scout ticket: https://secure.helpscout.net/conversation/2036688566/24611

@remcotolsma
Copy link
Member

A possible solution is:

$data['options'] = [];

try {
	$data['options'] = $this->get_flat_options(); 
} catch ( \Exception $e ) {
	$data['error'] = $e->getMessage();
}

return $data; 

But the question is also why is there an IDealIssuerSelectField instance in Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry->data?

I know we use the field JSON for the registerPaymentMethod content property:

https://github.com/woocommerce/woocommerce-blocks/blob/060f63c04f0f34f645200b5d4da9212125c49177/docs/third-party-developers/extensibility/checkout-payment-methods/payment-method-integration.md#payment-methods---registerpaymentmethod-options-

The field.options property is directly injected in the WordPress <SelectControl options={ field.options } /> component:

We currently also use <BaseControl />, is there a default way to display errors for these control components? Should we add a <Notice status="error">An unknown error occurred.</Notice> when field.error is set? Is field.error a common property for field errors?

Apart from this I think it is correct to add a try catch in jsonSerialize?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants