Skip to content

Commit 2e35acc

Browse files
bindlegirlmatticbot
authored andcommitted
Connection: adapt js component for custom errors (#44281)
Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/16465998684 Upstream-Ref: Automattic/jetpack@8c12546
1 parent 7b8da24 commit 2e35acc

13 files changed

+217
-145
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This is an alpha version! The changes listed here are not final.
2424

2525
### Fixed
2626
- Bump wc-calypso-bridge package 2.11.0
27+
- Fixed tests.
2728
- JITM: Fix ineffective caching due to expired plugin sync transient
2829
- Playground Importer: Fix query error generated by SQLite table with no entries in the data types cache table.
2930
- Private site: ensure private sites return a valid cookie expiration value

connection/class-protected-owner-error-handler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ private function build_enhanced_error_data( $raw_error ) {
204204
),
205205
);
206206

207-
// @phan-suppress-next-line PhanUndeclaredMethod -- Method exists in newer connection package versions
208207
return $error_handler->build_action_error_data( $args );
209208
}
210209

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('jetpack-script-data', 'react', 'react-jsx-runtime', 'wp-components', 'wp-compose', 'wp-data', 'wp-date', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => '38952d9d604b8b86a198');
1+
<?php return array('dependencies' => array('jetpack-script-data', 'react', 'react-jsx-runtime', 'wp-components', 'wp-data', 'wp-date', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => '8414cb09f6268b7f83a3');

jetpack_vendor/automattic/jetpack-connection/dist/jetpack-connection.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jetpack_vendor/automattic/jetpack-connection/dist/jetpack-connection.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jetpack_vendor/automattic/jetpack-connection/dist/jetpack-connection.rtl.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jetpack_vendor/automattic/jetpack-connection/src/class-error-handler.php

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,23 +331,42 @@ public function jetpack_react_dashboard_error( $errors ) { // phpcs:ignore Varia
331331
}
332332

333333
$first_user_errors = $displayable_errors[ $first_error_code ];
334-
$first_error = reset( $first_user_errors );
334+
if ( ! is_array( $first_user_errors ) || empty( $first_user_errors ) ) {
335+
return array(); // Invalid error structure
336+
}
337+
338+
$first_error = reset( $first_user_errors );
339+
340+
// Validate error structure
341+
if ( ! is_array( $first_error ) || ! isset( $first_error['error_message'] ) ) {
342+
return array(); // Invalid error structure
343+
}
335344

336345
// Determine the action - use the one from error_data if available, otherwise default to 'reconnect'
337346
$action = 'reconnect'; // Default action for connection errors
338-
if ( isset( $first_error['error_data']['action'] ) ) {
347+
if ( isset( $first_error['error_data']['action'] ) && is_string( $first_error['error_data']['action'] ) ) {
339348
$action = $first_error['error_data']['action'];
340349
}
341350

351+
// Safely merge error data, ensuring we don't overwrite critical fields
352+
$error_data = isset( $first_error['error_data'] ) && is_array( $first_error['error_data'] ) ? $first_error['error_data'] : array();
353+
354+
// Build the data array with safe merging
355+
$dashboard_data = array( 'api_error_code' => $first_error_code );
356+
357+
// Add error_data fields, but be careful not to overwrite api_error_code
358+
foreach ( $error_data as $key => $value ) {
359+
if ( 'api_error_code' !== $key ) {
360+
$dashboard_data[ $key ] = $value;
361+
}
362+
}
363+
342364
$dashboard_error = array(
343365
array(
344366
'code' => 'connection_error',
345367
'message' => $first_error['error_message'],
346368
'action' => $action,
347-
'data' => array_merge(
348-
array( 'api_error_code' => $first_error_code ),
349-
$first_error['error_data'] ?? array()
350-
),
369+
'data' => $dashboard_data,
351370
),
352371
);
353372

@@ -465,13 +484,66 @@ public function store_error( \WP_Error $error ) {
465484
return false;
466485
}
467486

487+
/**
488+
* Builds action error data for generic JavaScript components.
489+
*
490+
* This helper method creates standardized error_data arrays that work with the generic
491+
* JavaScript error handling components. External plugins (like wpcomsh) can use this
492+
* to ensure their error structures are compatible.
493+
*
494+
* @since 6.16.0-alpha
495+
*
496+
* @param array $args Action configuration arguments - only non-empty values will be included.
497+
* @return array Standardized error_data array for JavaScript components.
498+
*/
499+
public function build_action_error_data( array $args = array() ) {
500+
// Set default values for variants
501+
$args = wp_parse_args(
502+
$args,
503+
array(
504+
'action_variant' => 'primary',
505+
'secondary_action_variant' => 'secondary',
506+
)
507+
);
508+
509+
// Start with core data
510+
$error_data = array(
511+
'blog_id' => \Jetpack_Options::get_option( 'id' ),
512+
);
513+
514+
// Validate variant values
515+
$valid_variants = array( 'primary', 'secondary' );
516+
if ( ! in_array( $args['action_variant'], $valid_variants, true ) ) {
517+
$args['action_variant'] = 'primary';
518+
}
519+
if ( ! in_array( $args['secondary_action_variant'], $valid_variants, true ) ) {
520+
$args['secondary_action_variant'] = 'secondary';
521+
}
522+
523+
// Merge extra_data first, then regular args (so args take precedence)
524+
if ( ! empty( $args['extra_data'] ) && is_array( $args['extra_data'] ) ) {
525+
$error_data = array_merge( $error_data, $args['extra_data'] );
526+
unset( $args['extra_data'] ); // Remove from args to avoid duplication
527+
}
528+
529+
// Filter out empty values and merge with error_data
530+
$filtered_args = array_filter(
531+
$args,
532+
function ( $value ) {
533+
return ! empty( $value );
534+
}
535+
);
536+
537+
return array_merge( $error_data, $filtered_args );
538+
}
539+
468540
/**
469541
* Builds a standardized error array for the connection error system.
470542
*
471543
* This method creates a consistent error array structure that can be used
472544
* by both internal error handling and external plugins/customizations.
473545
*
474-
* @since 6.13.10
546+
* @since 1.14.2
475547
*
476548
* @param string $error_code The error code identifier.
477549
* @param string $error_message The human-readable error message.

jetpack_vendor/automattic/jetpack-connection/src/class-package-version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
class Package_Version {
1414

15-
const PACKAGE_VERSION = '6.15.0';
15+
const PACKAGE_VERSION = '6.16.0-alpha';
1616

1717
const PACKAGE_SLUG = 'connection';
1818

jetpack_vendor/i18n-map.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
),
3535
'jetpack-connection' => array(
3636
'path' => 'jetpack_vendor/automattic/jetpack-connection',
37-
'ver' => '6.15.0',
37+
'ver' => '6.16.0-alpha1753260548',
3838
),
3939
'jetpack-google-analytics' => array(
4040
'path' => 'jetpack_vendor/automattic/jetpack-google-analytics',

0 commit comments

Comments
 (0)