Skip to content

Conversation

@wojtekn
Copy link
Contributor

@wojtekn wojtekn commented Dec 17, 2024

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

  1. Create a new local Studio site.
  2. Open site in Terminal
  3. Install and activate the plugin:
wp plugin install slider-revolution-search-replace
wp plugin activate slider-revolution-search-replace
  1. Connect the site to one of your testing WPCOM sites.
  2. Initiate the push operation.
  3. Confirm there is a dialog with descriptive error and push is marked as failed

Export

  1. Use the site from previous test
  2. Navigate to Import / Export tab
  3. Export entire site
  4. Confirm there is a dialog with descriptive error

Browser console error

  1. Open Studio's browser console
  2. Switch between sites and tabs
  3. Confirm there is no error in the browser console

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@wojtekn wojtekn requested a review from a team December 17, 2024 07:29
@wojtekn wojtekn self-assigned this Dec 17, 2024
Copy link
Member

@sejas sejas left a 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:

Screenshot 2024-12-17 at 15 15 02

Confirm there is no error related to push

Pushing to a WPcom site is still failing, so I couldn't complete the testing instructions:

Screenshot 2024-12-17 at 15 20 58
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

Comment on lines 279 to 283
try {
return JSON.parse( stdout );
} catch ( error ) {
return [];
}
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

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.

@wojtekn
Copy link
Contributor Author

wojtekn commented Dec 17, 2024

Pushing to a WPcom site is still failing, so I couldn't complete the testing instructions:

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.

@wojtekn
Copy link
Contributor Author

wojtekn commented Dec 18, 2024

@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(
Copy link
Contributor Author

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.' );
Copy link
Contributor Author

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`
Copy link
Contributor Author

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 );
Copy link
Contributor Author

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.

Copy link
Member

@sejas sejas left a 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.

Comment on lines +143 to +145
message: __(
'An error occurred while pushing the site. If this problem persists, please contact support.'
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

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 );
Copy link
Member

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 () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sejas
Copy link
Member

sejas commented Dec 18, 2024

@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.

Thanks for following this approach 🙏

@wojtekn wojtekn merged commit 4533c37 into trunk Dec 18, 2024
9 checks passed
@wojtekn wojtekn deleted the update/wp-cli-error-handling branch December 18, 2024 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants