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 );
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 ) => {
if ( pluginErrors ) {
Object.keys( pluginErrors ).forEach( ( plugin ) => {
createNotice(
'error',
// Replace the slug with a plugin name if a constant exists.
pluginNames[ plugin ]
? errors[ plugin ][ 0 ].replace(
? pluginErrors[ plugin ][ 0 ].replace(
`\`${ plugin }\``,
pluginNames[ plugin ]
)
: errors[ plugin ][ 0 ]
: pluginErrors[ plugin ][ 0 ]
);
} );
} else if ( errors.message ) {
createNotice( 'error', errors.message );
}
this.setState( { hasErrors: true } );
onError( errors );

View File

@ -103,10 +103,12 @@ describe( 'Installing and activating', () => {
describe( 'Installing and activating errors', () => {
let pluginsWrapper;
const errors = {
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 ]
);
} );

View File

@ -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
);
} );
}
};
}
}