-
Notifications
You must be signed in to change notification settings - Fork 51
Update WP CLI error handling #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d3bec6a
cf3999d
150969a
3336eab
b0dde1e
eacb0ff
ec24a92
85350ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,6 @@ const parseWpCliOutput = ( stdout: string, defaultValue: string[] ): string[] => | |
| const data = JSON.parse( stdout ); | ||
| return data?.map( ( item: { name: string } ) => item.name ) || []; | ||
| } catch ( error ) { | ||
| console.error( error, stdout ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was logging to the browser console, but the user doesn't have access to that. |
||
| Sentry.captureException( error, { | ||
| extra: { stdout }, | ||
| } ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,15 +47,25 @@ export async function exportDatabaseToMultipleFiles( | |
| } | ||
|
|
||
| const tablesResult = await server.executeWpCliCommand( | ||
| `sqlite tables --format=csv --require=/tmp/sqlite-command/command.php` | ||
| `sqlite tables --format=json --require=/tmp/sqlite-command/command.php` | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switching to JSON, we can easily validate the output. If HTML or text content leaks into the command output, it will always produce invalid JSON. |
||
| ); | ||
| if ( tablesResult.stderr ) { | ||
| throw new Error( `Database export failed: ${ tablesResult.stderr }` ); | ||
| } | ||
| if ( tablesResult.exitCode ) { | ||
| throw new Error( 'Database export failed' ); | ||
| } | ||
| const tables = tablesResult.stdout.split( ',' ); | ||
|
|
||
| let tables; | ||
|
|
||
| try { | ||
| tables = JSON.parse( tablesResult.stdout ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After applying Automattic/wp-cli-sqlite-command#9 it worked as expected, producing the output: Very smart! 💡 |
||
| } catch ( error ) { | ||
| console.error( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This goes to the app log. |
||
| `Could not get list of database tables. The WP CLI output: ${ tablesResult.stdout }` | ||
| ); | ||
| throw new Error( 'Could not get list of database tables to export.' ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this is the message that would end up in the dialog displayed to the user. |
||
| } | ||
|
|
||
| const tmpFiles: string[] = []; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,7 +116,7 @@ describe( 'DefaultExporter', () => { | |
| case /theme list/.test( command ): | ||
| return { stdout: '[{"name":"twentytwentyfour","status":"active","version":"1.0"}]' }; | ||
| case /tables/.test( command ): | ||
| return { stdout: defaultTableNames.join( ',' ) }; | ||
| return { stdout: JSON.stringify( defaultTableNames ) }; | ||
| default: | ||
| return { stderr: null }; | ||
| } | ||
|
|
@@ -323,7 +323,7 @@ describe( 'DefaultExporter', () => { | |
| expect( canHandle ).toBe( false ); | ||
| } ); | ||
|
|
||
| it( 'should not fail when can not get plugin or theme details', async () => { | ||
| it( 'should fail when can not get plugin or theme details', async () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| ( SiteServer.get as jest.Mock ).mockReturnValue( { | ||
| details: { path: '/path/to/site' }, | ||
| executeWpCliCommand: jest.fn( function ( command: string ) { | ||
|
|
@@ -338,10 +338,9 @@ describe( 'DefaultExporter', () => { | |
| } ); | ||
|
|
||
| const exporter = new DefaultExporter( mockOptions ); | ||
| await exporter.export(); | ||
|
|
||
| expect( mockArchiver.file ).toHaveBeenCalledWith( '/tmp/studio_export_123/meta.json', { | ||
| name: 'meta.json', | ||
| } ); | ||
| await expect( exporter.export() ).rejects.toThrow( | ||
| 'Could not get information about installed plugins to create meta.json file.' | ||
| ); | ||
| } ); | ||
| } ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good call.
When I was introducing the error message, I was looking at it from the perspective of a site export step (as a part of the whole push process). However, from the user's perspective I think it makes to use the error message you have proposed here.