-
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
Conversation
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.
I confirm that after this PR, the export entire site works for sites with slider-revolution-search-replace which failed before, but it produces an empty meta.json . Check the image:
Confirm there is no error related to push
Pushing to a WPcom site is still failing, so I couldn't complete the testing instructions:
Error occurred in handler for 'exportSiteToPush': Error: Cannot execute wp-cli command with arguments: sqlite export <br />
<b>Warning</b>: session_start(): Cannot start session when headers already sent in <b>/var/www/html/wp-content/plugins/slider-revolution-search-replace/admin/class-search-replace.php</b> on line <b>17</b><br />
wp_users.sql --tables=<br />
<b>Warning</b>: session_start(): Cannot start session when headers already sent in <b>/var/www/html/wp-content/plugins/slider-revolution-search-replace/admin/class-search-replace.php</b> on line <b>17</b><br />
wp_users --require=/tmp/sqlite-command/command.php
at SiteServer.<anonymous> (/Users/macbookpro/Documents/projects-m3.nosync/studio/.webpack/main/index.js:223235:23)
at Generator.next (<anonymous>)
at /Users/macbookpro/Documents/projects-m3.nosync/studio/.webpack/main/index.js:223061:71
at new Promise (<anonymous>)
at __webpack_modules__../src/site-server.ts.__awaiter (/Users/macbookpro/Documents/projects-m3.nosync/studio/.webpack/main/index.js:223057:12)
at SiteServer.executeWpCliCommand (/Users/macbookpro/Documents/projects-m3.nosync/studio/.webpack/main/index.js:223224:16)
at /Users/macbookpro/Documents/projects-m3.nosync/studio/.webpack/main/index.js:218221:55
at Generator.next (<anonymous>)
at fulfilled (/Users/macbookpro/Documents/projects-m3.nosync/studio/.webpack/main/index.js:218161:58)
Backup created at: /var/folders/_x/rbv26n3925q_01dbs4jzd8t80000gn/T/com.wordpress.studio/site_0c3e6050-2ab2-42d9-8094-34b35763629c.tar.gz
| try { | ||
| return JSON.parse( stdout ); | ||
| } catch ( error ) { | ||
| return []; | ||
| } |
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.
I'm not totally convinced that making this error silent is the best solution. Maybe we should report it to Sentry?, or to the user? Informing the user we couldn't grab the plugins/themes information.
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.
It wouldn't help to pass it to Sentry, as we couldn't do much without knowing what PHP code caused the incorrect WP CLI response.
Informing the user we couldn't grab the plugins/themes information.
Would you fail the import entirely in such a case?
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.
I thought about that more, and it may make sense to fail the import indeed. If the 3rd party system depended on the correct meta.json file, the package produced in such a case would be considered invalid.
The warning included with WP CLI commands leaks to the following command, and the command that gets a list of database tables is affected. We should improve the error message so that users can look into logs to see what causes the issue. I will take https://github.com/Automattic/dotcom-forge/issues/10143 to improve that part. |
|
@sejas per our discussion, I've changed the approach to fail push/export when Studio can't generate a list of themes or plugins. I've updated the description to account for the changes. The error dialog will bu further improve under https://github.com/Automattic/dotcom-forge/issues/10143. |
| try { | ||
| tables = JSON.parse( tablesResult.stdout ); | ||
| } catch ( error ) { | ||
| console.error( |
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.
This goes to the app log.
| console.error( | ||
| `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.' ); |
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.
And this is the message that would end up in the dialog displayed to the user.
|
|
||
| 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` |
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.
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.
| const data = JSON.parse( stdout ); | ||
| return data?.map( ( item: { name: string } ) => item.name ) || []; | ||
| } catch ( error ) { | ||
| console.error( error, stdout ); |
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.
This was logging to the browser console, but the user doesn't have access to that.
sejas
left a comment
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.
Works as expected and I'm able to export a health site correctly.
I like that now we are stopping the export process if a wp-cli command fails 💪 .
That way we can keep improving edge cases and keep evolving our features.
Great addition of console errors and "popup" messages to the user.
| message: __( | ||
| 'An error occurred while pushing the site. If this problem persists, please contact support.' | ||
| ), |
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.
| let tables; | ||
|
|
||
| try { | ||
| tables = JSON.parse( tablesResult.stdout ); |
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.
After applying Automattic/wp-cli-sqlite-command#9 it worked as expected, producing the output:
[
'wp_users',
'wp_usermeta',
'wp_termmeta',
'wp_terms',
'wp_term_taxonomy',
'wp_term_relationships',
'wp_commentmeta',
'wp_comments',
'wp_links',
'wp_options',
'wp_postmeta',
'wp_posts'
]
Very smart! 💡
| } ); | ||
|
|
||
| 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 () => { |
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.
👍
Thanks for following this approach 🙏 |
Related issues
Proposed Changes
The Studio Assistant uses WP CLI to get the site's context data. When something is broken on the site's side, and the CLI command produces non-JSON output, the error is caught and logged to the browser console and Sentry. It's not helpful for the user as the user doesn't have access to the browser console log and a message itself isn't too useful. I propose to remove this logger line.
We might remove Sentry logging, as those are not really helpful, too:
https://a8c.sentry.io/issues/5243457569/?project=4506612776501248&query=is%3Aunresolved%20issue.priority%3A%5Bhigh%2C%20medium%5D&referrer=issue-stream&statsPeriod=14d&stream_index=16
I also propose to better catch errors received from the WP CLI when Studio fetches theme and plugin lists for meta.json purposes, and mark the export or push as failed with a proper message.
Testing Instructions
Warning
The PR depends on Automattic/wp-cli-sqlite-command#9
Push
Export
Browser console error
Pre-merge Checklist