Repository navigation
Show the file, line, and stack trace for errors thrown while preparing documents - #4392
Open
anjulahettige wants to merge 6 commits into
Open
anjulahettige wants to merge 6 commits into
anjulahettige wants to merge 6 commits into
Conversation
1 task done
felipeelia
reviewed
Oct 26, 2026
| */ | ||
| protected function handle_prepare_document_error( \Throwable $th, $object_id ): array { | ||
| // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | ||
| error_log( sprintf( 'ElasticPress: could not prepare %s %d for indexing. %s', $this->slug, $object_id, $th ) ); |
Member
There was a problem hiding this comment.
@anjulahettige any chance you can wrap this call with a check of WP_DEBUG being on, please?
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of the Change
Since 5.3.0 (#4210),
Indexable::index(),bulk_index(), andbulk_index_dynamically()catch anyThrowablethrown byprepare_document(), so a sync can continue past a broken object. The catch blocks keep only$th->getMessage(), which drops the file, line, and stack trace:wp elasticpress sync --show-errorsprints only<ID> (Post): [prepare_document_error] <message>.Before 5.3.0, the same error stopped the sync, but PHP printed and logged the full fatal error, so
--show-errorsand the error log were how you found the plugin or theme responsible.This PR keeps the 5.3.0 behavior (the object is skipped and the sync continues) and restores the missing details:
Error data: a new
Indexable::handle_prepare_document_error()is called by all three catch blocks. WhenWP_DEBUGis on, it writes the full error, with its stack trace, to the PHP error log witherror_log(). It returns the sync error with:reason: the message plusin <file>:<line>stack_trace: the stack tracePaths in
reasonandstack_traceare relative to the WordPress root. The error log keeps absolute paths, like PHP's own fatal errors.Passing traces along:
IndexHelpersends each error's stack trace along with its message through a new optional$stack_tracesparameter onqueue_message(),output(), andbuild_message_errors_data().build_message_errors_data()adds the trace to theerrorsdata that the CLI and the Sync page receive.WP-CLI:
--show-errorsnow printsStack trace for: <error>followed by the trace. There is no new flag. With--stop-on-error, the traces print before the sync stops.Sync page: errors that have a trace get a "Stack trace" toggle (
<details>), collapsed by default, in the Errors tab. Nothing else on the page changes, and "Copy log to clipboard" is unchanged.Example output from
wp elasticpress sync --include=4 --show-errors:Notes and drawbacks:
WP_DEBUGis on, one error log entry, with its stack trace, is written for every object that fails. If a plugin breaks every product on a large store, the log grows by about 2 to 3 KB per failed object per sync. WithWP_DEBUGoff, nothing is logged, and the file, line, and trace are only available in WP-CLI and on the Sync page.errorsdata sent to the Sync page and the CLI includes a trace for each failed object, about 2 KB each. Stack traces are never written to theep_index_metaoption, because each message is removed from the queue before the option is saved. With 170 objects failing in one 350-object batch, the largest write to the option was 8 KB.IndexHelpermethods above are protected and gained an optional parameter, so a subclass that overrides one of them with the old signature would cause a fatal error.IndexHelper::factory()always instantiatesnew self(), and I found no subclasses in ElasticPress Labs, Debug Bar ElasticPress, or GitHub code search.countErrors()now builds a new object for the combined row instead of modifying the previous React state.Related: #4210, #4333
How to test the Change
wp-content/mu-plugins/broken.php, replacing123with an existing post ID:wp elasticpress sync --include=123 --show-errors. The warning ends within wp-content/mu-plugins/broken.php:<line>, followed byStack trace for: ...and the trace. Without--show-errors, the output is the same as before.WP_DEBUGandWP_DEBUG_LOGenabled, checkwp-content/debug.log. It has anElasticPress: could not prepare post 123 for indexing.entry with the full stack trace. WithWP_DEBUGoff, no entry is written.wp elasticpress sync --stop-on-error. The trace prints before the sync stops.composer run test-single-site -- --filter 'TestIndexHelper|TestCommands|TestPost'andcomposer run test -- --filter 'TestIndexHelper|TestCommands|TestPost'.Changelog Entry
Credits
Props @anjulahettige
Checklist: