Fix error handling for plugins on server error (https://github.com/woocommerce/woocommerce-admin/pull/4462)

* Handle server errors in plugin components

* Show generic server error message for plugin install if no plugin errors exist

* Fix plugin error tests
This commit is contained in:
Joshua T Flowers 2020-06-01 13:39:58 +03:00 committed by GitHub
parent fd020a2f4c
commit 2c9b19521a
3 changed files with 45 additions and 35 deletions

View File

@ -55,7 +55,7 @@ export class Plugins extends Component {
const installs = await installPlugins( pluginSlugs ); const installs = await installPlugins( pluginSlugs );
if ( installs.errors && Object.keys( installs.errors.errors ).length ) { if ( installs.errors && Object.keys( installs.errors.errors ).length ) {
this.handleErrors( installs.errors.errors ); this.handleErrors( installs.errors );
return; return;
} }
@ -66,24 +66,31 @@ export class Plugins extends Component {
return; return;
} }
this.handleErrors( activations.errors.errors ); if ( activations.errors ) {
this.handleErrors( activations.errors );
}
} }
handleErrors( errors ) { handleErrors( errors ) {
const { onError, createNotice } = this.props; const { onError, createNotice } = this.props;
const { errors: pluginErrors } = errors;
Object.keys( errors ).forEach( ( plugin ) => { if ( pluginErrors ) {
createNotice( Object.keys( pluginErrors ).forEach( ( plugin ) => {
'error', createNotice(
// Replace the slug with a plugin name if a constant exists. 'error',
pluginNames[ plugin ] // Replace the slug with a plugin name if a constant exists.
? errors[ plugin ][ 0 ].replace( pluginNames[ plugin ]
`\`${ plugin }\``, ? pluginErrors[ plugin ][ 0 ].replace(
pluginNames[ plugin ] `\`${ plugin }\``,
) pluginNames[ plugin ]
: errors[ plugin ][ 0 ] )
); : pluginErrors[ plugin ][ 0 ]
} ); );
} );
} else if ( errors.message ) {
createNotice( 'error', errors.message );
}
this.setState( { hasErrors: true } ); this.setState( { hasErrors: true } );
onError( errors ); onError( errors );

View File

@ -103,10 +103,12 @@ describe( 'Installing and activating', () => {
describe( 'Installing and activating errors', () => { describe( 'Installing and activating errors', () => {
let pluginsWrapper; let pluginsWrapper;
const errors = { const errors = {
'failed-plugin': [ 'error message' ], errors: {
'failed-plugin': [ 'error message' ],
},
}; };
const installPlugins = jest.fn().mockReturnValue( { const installPlugins = jest.fn().mockReturnValue( {
errors: { errors }, errors,
} ); } );
const activatePlugins = jest.fn().mockReturnValue( { const activatePlugins = jest.fn().mockReturnValue( {
success: false, success: false,
@ -140,7 +142,7 @@ describe( 'Installing and activating errors', () => {
expect( createNotice ).toHaveBeenCalledWith( expect( createNotice ).toHaveBeenCalledWith(
'error', 'error',
errors[ 'failed-plugin' ][ 0 ] errors.errors[ 'failed-plugin' ][ 0 ]
); );
} ); } );

View File

@ -3,14 +3,13 @@
*/ */
import { apiFetch } from '@wordpress/data-controls'; import { apiFetch } from '@wordpress/data-controls';
import { __, sprintf } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
/** /**
* Internal Dependencies * Internal Dependencies
*/ */
import TYPES from './action-types'; import TYPES from './action-types';
import { WC_ADMIN_NAMESPACE } from '../constants'; import { WC_ADMIN_NAMESPACE } from '../constants';
import { pluginNames } from './constants';
export function updateActivePlugins( active, replace = false ) { export function updateActivePlugins( active, replace = false ) {
return { return {
@ -85,16 +84,17 @@ export function* installPlugins( plugins ) {
return results; return results;
} catch ( error ) { } catch ( error ) {
const errorMsg = __( yield setError( 'installPlugins', error );
'Something went wrong while trying to install your plugins.',
'woocommerce-admin'
);
yield setError( 'installPlugins', errorMsg );
yield setIsRequesting( 'installPlugins', false ); 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 ) { if ( results.success && results.data.activated ) {
yield updateActivePlugins( results.data.activated ); yield updateActivePlugins( results.data.activated );
yield setIsRequesting( 'activatePlugins', false );
return results; return results;
} }
throw new Error(); throw new Error();
} catch ( error ) { } catch ( error ) {
yield setError( 'activatePlugins', error ); yield setError( 'activatePlugins', error );
return plugins.map( ( plugin ) => { yield setIsRequesting( 'activatePlugins', false );
const pluginName = pluginNames[ plugin ] || plugin;
return sprintf( return {
__( errors: {
'There was an error activating %s. Please try again.', message: __(
'Something went wrong while trying to activate your plugins.',
'woocommerce-admin' 'woocommerce-admin'
), ),
pluginName }
); };
} );
} }
} }