Skip to content

Commit

Permalink
wp-env: no longer show error message twice (#20157)
Browse files Browse the repository at this point in the history
* Handle errors more robustly

- Separate cases for errors we know about
- More straightforward general error case

* Update tests to handle the new error format
  • Loading branch information
noahtallen authored and jorgefilipecosta committed Mar 2, 2020
1 parent e133b67 commit 459aa48
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
29 changes: 25 additions & 4 deletions packages/env/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,33 @@ const withSpinner = ( command ) => ( ...args ) => {
);
},
( error ) => {
spinner.fail( error.message || error.err );
if ( ! ( error instanceof env.ValidationError ) ) {
if ( error instanceof env.ValidationError ) {
// Error is a validation error. That means the user did something wrong.
spinner.fail( error.message );
process.exit( 1 );
} else if (
'exitCode' in error &&
'err' in error &&
'out' in error
) {
// Error is a docker-compose error. That means something docker-related failed.
// https://github.com/PDMLab/docker-compose/blob/master/src/index.ts
spinner.fail( 'Error while running docker-compose command.' );
if ( error.out ) {
process.stdout.write( error.out );
}
if ( error.err ) {
process.stderr.write( error.err );
}
process.exit( error.exitCode );
} else {
// Error is an unknown error. That means there was a bug in our code.
spinner.fail( error.message );
// Disable reason: Using console.error() means we get a stack trace.
// eslint-disable-next-line no-console
console.error( `\n\n${ error.out || error.err }\n\n` );
console.error( error );
process.exit( 1 );
}
process.exit( error.exitCode || 1 );
}
);
};
Expand Down
21 changes: 14 additions & 7 deletions packages/env/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ describe( 'env cli', () => {
/* eslint-disable no-console */
env.start.mockRejectedValueOnce( {
message: 'failure message',
out: 'failure message',
exitCode: 2,
} );
const consoleError = console.error;
console.error = jest.fn();
Expand All @@ -98,28 +96,37 @@ describe( 'env cli', () => {

expect( spinner.fail ).toHaveBeenCalledWith( 'failure message' );
expect( console.error ).toHaveBeenCalled();
expect( process.exit ).toHaveBeenCalledWith( 2 );
expect( process.exit ).toHaveBeenCalledWith( 1 );
console.error = consoleError;
process.exit = processExit;
/* eslint-enable no-console */
} );
it( 'handles failed commands with errors.', async () => {
it( 'handles failed docker commands with errors.', async () => {
/* eslint-disable no-console */
env.start.mockRejectedValueOnce( { err: 'failure error' } );
env.start.mockRejectedValueOnce( {
err: 'failure error',
out: 'message',
exitCode: 1,
} );
const consoleError = console.error;
console.error = jest.fn();
const processExit = process.exit;
process.exit = jest.fn();
const stderr = process.stderr.write;
process.stderr.write = jest.fn();

cli().parse( [ 'start' ] );
const { spinner } = env.start.mock.calls[ 0 ][ 0 ];
await env.start.mock.results[ 0 ].value.catch( () => {} );

expect( spinner.fail ).toHaveBeenCalledWith( 'failure error' );
expect( console.error ).toHaveBeenCalled();
expect( spinner.fail ).toHaveBeenCalledWith(
'Error while running docker-compose command.'
);
expect( process.stderr.write ).toHaveBeenCalledWith( 'failure error' );
expect( process.exit ).toHaveBeenCalledWith( 1 );
console.error = consoleError;
process.exit = processExit;
process.stderr.write = stderr;
/* eslint-enable no-console */
} );
} );

0 comments on commit 459aa48

Please sign in to comment.