diff --git a/plugins/woocommerce-admin/packages/components/src/plugins/index.js b/plugins/woocommerce-admin/packages/components/src/plugins/index.js index c9bbc61093b..bc8455e8fbd 100644 --- a/plugins/woocommerce-admin/packages/components/src/plugins/index.js +++ b/plugins/woocommerce-admin/packages/components/src/plugins/index.js @@ -55,7 +55,7 @@ export class Plugins extends Component { const installs = await installPlugins( pluginSlugs ); if ( installs.errors && Object.keys( installs.errors.errors ).length ) { - this.handleErrors( installs.errors.errors ); + this.handleErrors( installs.errors ); return; } @@ -66,24 +66,31 @@ export class Plugins extends Component { return; } - this.handleErrors( activations.errors.errors ); + if ( activations.errors ) { + this.handleErrors( activations.errors ); + } } handleErrors( errors ) { const { onError, createNotice } = this.props; + const { errors: pluginErrors } = errors; - Object.keys( errors ).forEach( ( plugin ) => { - createNotice( - 'error', - // Replace the slug with a plugin name if a constant exists. - pluginNames[ plugin ] - ? errors[ plugin ][ 0 ].replace( - `\`${ plugin }\``, - pluginNames[ plugin ] - ) - : errors[ plugin ][ 0 ] - ); - } ); + if ( pluginErrors ) { + Object.keys( pluginErrors ).forEach( ( plugin ) => { + createNotice( + 'error', + // Replace the slug with a plugin name if a constant exists. + pluginNames[ plugin ] + ? pluginErrors[ plugin ][ 0 ].replace( + `\`${ plugin }\``, + pluginNames[ plugin ] + ) + : pluginErrors[ plugin ][ 0 ] + ); + } ); + } else if ( errors.message ) { + createNotice( 'error', errors.message ); + } this.setState( { hasErrors: true } ); onError( errors ); diff --git a/plugins/woocommerce-admin/packages/components/src/plugins/test/index.js b/plugins/woocommerce-admin/packages/components/src/plugins/test/index.js index 546e1f526b0..823cf2828f5 100644 --- a/plugins/woocommerce-admin/packages/components/src/plugins/test/index.js +++ b/plugins/woocommerce-admin/packages/components/src/plugins/test/index.js @@ -103,10 +103,12 @@ describe( 'Installing and activating', () => { describe( 'Installing and activating errors', () => { let pluginsWrapper; const errors = { - 'failed-plugin': [ 'error message' ], + errors: { + 'failed-plugin': [ 'error message' ], + }, }; const installPlugins = jest.fn().mockReturnValue( { - errors: { errors }, + errors, } ); const activatePlugins = jest.fn().mockReturnValue( { success: false, @@ -140,7 +142,7 @@ describe( 'Installing and activating errors', () => { expect( createNotice ).toHaveBeenCalledWith( 'error', - errors[ 'failed-plugin' ][ 0 ] + errors.errors[ 'failed-plugin' ][ 0 ] ); } ); diff --git a/plugins/woocommerce-admin/packages/data/src/plugins/actions.js b/plugins/woocommerce-admin/packages/data/src/plugins/actions.js index 7fa6be9dfc8..c48f7090180 100644 --- a/plugins/woocommerce-admin/packages/data/src/plugins/actions.js +++ b/plugins/woocommerce-admin/packages/data/src/plugins/actions.js @@ -3,14 +3,13 @@ */ import { apiFetch } from '@wordpress/data-controls'; -import { __, sprintf } from '@wordpress/i18n'; +import { __ } from '@wordpress/i18n'; /** * Internal Dependencies */ import TYPES from './action-types'; import { WC_ADMIN_NAMESPACE } from '../constants'; -import { pluginNames } from './constants'; export function updateActivePlugins( active, replace = false ) { return { @@ -85,16 +84,17 @@ export function* installPlugins( plugins ) { return results; } catch ( error ) { - const errorMsg = __( - 'Something went wrong while trying to install your plugins.', - 'woocommerce-admin' - ); - - yield setError( 'installPlugins', errorMsg ); - + yield setError( 'installPlugins', error ); yield setIsRequesting( 'installPlugins', false ); - return errorMsg; + return { + errors: { + message: __( + 'Something went wrong while trying to install your plugins.', + 'woocommerce-admin' + ), + }, + }; } } @@ -110,21 +110,22 @@ export function* activatePlugins( plugins ) { if ( results.success && results.data.activated ) { yield updateActivePlugins( results.data.activated ); + yield setIsRequesting( 'activatePlugins', false ); return results; } throw new Error(); } catch ( error ) { yield setError( 'activatePlugins', error ); - return plugins.map( ( plugin ) => { - const pluginName = pluginNames[ plugin ] || plugin; - return sprintf( - __( - 'There was an error activating %s. Please try again.', + yield setIsRequesting( 'activatePlugins', false ); + + return { + errors: { + message: __( + 'Something went wrong while trying to activate your plugins.', 'woocommerce-admin' ), - pluginName - ); - } ); + } + }; } }