-
Notifications
You must be signed in to change notification settings - Fork 1
Merge review changes from #8 to trunk #14
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -72,23 +72,7 @@ public static function ingest_post( int $post_id, \WP_Post $post ): void { | |
| ] | ||
| ); | ||
|
|
||
| self::fire_ingestion_failure( | ||
| new Ingestion_Failure( | ||
| [ | ||
| 'failure_code' => Ingestion_Failure::CODE_TRANSFORM_FAILED, | ||
| 'post' => $post, | ||
| 'error' => new \WP_Error( | ||
| 'vip_agentforce_transform_failed', | ||
| 'Post transformation returned null', | ||
| [ | ||
| 'post_id' => $post_id, | ||
| // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Intentional for error tracing. | ||
| 'backtrace' => debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 5 ), | ||
| ] | ||
| ), | ||
| ] | ||
| ) | ||
| ); | ||
| self::fire_ingestion_failure( $post_id, Ingestion_Failure::CODE_TRANSFORM_FAILED ); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -104,24 +88,7 @@ public static function ingest_post( int $post_id, \WP_Post $post ): void { | |
| ] | ||
| ); | ||
|
|
||
| self::fire_ingestion_failure( | ||
| new Ingestion_Failure( | ||
| [ | ||
| 'failure_code' => Ingestion_Failure::CODE_API_ERROR, | ||
| 'post' => $post, | ||
| 'error' => new \WP_Error( | ||
| 'vip_agentforce_api_error', | ||
| $response['error_message'] ?? 'API call failed', | ||
| [ | ||
| 'post_id' => $post_id, | ||
| 'response' => $response, | ||
| // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Intentional for error tracing. | ||
| 'backtrace' => debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 5 ), | ||
| ] | ||
| ), | ||
| ] | ||
| ) | ||
| ); | ||
| self::fire_ingestion_failure( $post_id, Ingestion_Failure::CODE_API_ERROR, [ 'response' => $response ] ); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -140,9 +107,37 @@ public static function ingest_post( int $post_id, \WP_Post $post ): void { | |
| /** | ||
| * Fire the ingestion failure action. | ||
| * | ||
| * @param Ingestion_Failure $failure The ingestion failure. | ||
| * @param int $post_id The post ID that failed ingestion. | ||
| * @param string $failure_code One of the Ingestion_Failure::CODE_* constants. | ||
| * @param array<string, mixed> $details Optional additional details about the failure. | ||
| */ | ||
| private static function fire_ingestion_failure( Ingestion_Failure $failure ): void { | ||
| private static function fire_ingestion_failure( int $post_id, string $failure_code, array $details = [] ): void { | ||
| $post = get_post( $post_id ); | ||
|
|
||
| $error_codes = [ | ||
| Ingestion_Failure::CODE_TRANSFORM_FAILED => 'vip_agentforce_transform_failed', | ||
| Ingestion_Failure::CODE_API_ERROR => 'vip_agentforce_api_error', | ||
| ]; | ||
|
|
||
| $error_messages = [ | ||
| Ingestion_Failure::CODE_TRANSFORM_FAILED => 'Post transformation failed', | ||
| Ingestion_Failure::CODE_API_ERROR => 'API call failed', | ||
| ]; | ||
|
Comment on lines
+122
to
+125
|
||
|
|
||
| $error_data = array_merge( [ 'post_id' => $post_id ], $details ); | ||
|
|
||
| $failure = new Ingestion_Failure( | ||
| [ | ||
| 'failure_code' => $failure_code, | ||
| 'post' => $post, | ||
| 'error' => new \WP_Error( | ||
| $error_codes[ $failure_code ] ?? 'vip_agentforce_ingestion_failed', | ||
| $error_messages[ $failure_code ] ?? 'Ingestion failed', | ||
| $error_data | ||
| ), | ||
| ] | ||
| ); | ||
|
|
||
| /** | ||
| * Fires when a post ingestion fails. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -78,61 +78,6 @@ function ( $failure ) use ( &$action_fired, &$received_failure ) { | |||||
| $this->assertSame( $post->ID, $received_failure->post->ID ); | ||||||
| $this->assertInstanceOf( WP_Error::class, $received_failure->error ); | ||||||
| $this->assertSame( 'vip_agentforce_api_error', $received_failure->error->get_error_code() ); | ||||||
| $this->assertSame( 'Simulated API failure', $received_failure->error->get_error_message() ); | ||||||
| } | ||||||
|
|
||||||
| public function test_api_error_contains_response_in_error_data(): void { | ||||||
| $post = $this->factory()->post->create_and_get( [ 'post_status' => 'publish' ] ); | ||||||
|
|
||||||
| add_filter( 'vip_agentforce_should_ingest_post', '__return_true' ); | ||||||
| add_filter( | ||||||
| 'vip_agentforce_transform_post', | ||||||
| // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed -- Filter callback signature. | ||||||
| function ( $record, $filter_post ) { | ||||||
| return new Ingestion_Post_Record( | ||||||
| [ | ||||||
| 'site_id' => '1', | ||||||
| 'blog_id' => '1', | ||||||
| 'post_id' => (string) $filter_post->ID, | ||||||
| 'site_id_blog_id' => '1_1', | ||||||
| 'site_id_blog_id_post_id' => '1_1_' . $filter_post->ID, | ||||||
| 'published' => true, | ||||||
| 'last_published_at' => '2025-01-01T00:00:00+00:00', | ||||||
| 'last_modified_at' => '2025-01-01T00:00:00+00:00', | ||||||
| 'title' => $filter_post->post_title, | ||||||
| 'content' => $filter_post->post_content, | ||||||
| 'excerpt' => $filter_post->post_excerpt, | ||||||
| 'categories' => '', | ||||||
| 'tags' => '', | ||||||
| 'author' => '', | ||||||
| 'url' => 'https://example.com', | ||||||
| 'post_type' => $filter_post->post_type, | ||||||
| 'post_status' => $filter_post->post_status, | ||||||
| ] | ||||||
| ); | ||||||
| }, | ||||||
| 10, | ||||||
| 2 | ||||||
| ); | ||||||
|
|
||||||
| /** @var Ingestion_Failure|null $received_failure */ | ||||||
| $received_failure = null; | ||||||
|
|
||||||
| add_action( | ||||||
| 'vip_agentforce_post_ingestion_failed', | ||||||
| function ( $failure ) use ( &$received_failure ) { | ||||||
| $received_failure = $failure; | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| Ingestion_With_Failing_Api::ingest_post( $post->ID, $post ); | ||||||
|
|
||||||
| $this->assertInstanceOf( Ingestion_Failure::class, $received_failure ); | ||||||
| $error_data = $received_failure->error->get_error_data(); | ||||||
| $this->assertIsArray( $error_data ); | ||||||
| $this->assertArrayHasKey( 'response', $error_data ); | ||||||
| $this->assertIsArray( $error_data['response'] ); | ||||||
| $this->assertFalse( $error_data['response']['success'] ); | ||||||
| $this->assertArrayHasKey( 'backtrace', $error_data ); | ||||||
| $this->assertSame( 'API call failed', $received_failure->error->get_error_message() ); | ||||||
|
||||||
| $this->assertSame( 'API call failed', $received_failure->error->get_error_message() ); | |
| $this->assertSame( 'Simulated API failure', $received_failure->error->get_error_message() ); |
Copilot
AI
Dec 29, 2025
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 test was removed which verified that error data includes response information and backtrace when an API error occurs. The test ensured that:
- The response array is included in error data (useful for debugging API issues)
- A backtrace is included in error data (useful for tracing where errors originated)
With the refactored fire_ingestion_failure() method, backtraces are no longer automatically added to error data. While the response is still passed through the $details array (line 91 in class-ingestion.php), there's no longer test coverage verifying this behavior. Consider adding back a test to verify that the response is properly included in the error data.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -372,32 +372,6 @@ function () use ( &$action_fired ) { | |
| $this->assertFalse( $action_fired, 'Action should NOT fire on successful ingestion.' ); | ||
| } | ||
|
Comment on lines
372
to
373
|
||
|
|
||
| public function test_failure_error_contains_backtrace(): void { | ||
| $post = $this->factory()->post->create_and_get( [ 'post_status' => 'publish' ] ); | ||
|
|
||
| add_filter( 'vip_agentforce_should_ingest_post', '__return_true' ); | ||
| add_filter( 'vip_agentforce_transform_post', '__return_null' ); | ||
|
|
||
| /** @var Ingestion_Failure|null $received_failure */ | ||
| $received_failure = null; | ||
|
|
||
| add_action( | ||
| 'vip_agentforce_post_ingestion_failed', | ||
| function ( $failure ) use ( &$received_failure ) { | ||
| $received_failure = $failure; | ||
| } | ||
| ); | ||
|
|
||
| Ingestion::ingest_post( $post->ID, $post ); | ||
|
|
||
| $this->assertInstanceOf( Ingestion_Failure::class, $received_failure ); | ||
| $error_data = $received_failure->error->get_error_data(); | ||
| $this->assertIsArray( $error_data ); | ||
| $this->assertArrayHasKey( 'backtrace', $error_data ); | ||
| $this->assertIsArray( $error_data['backtrace'] ); | ||
| $this->assertNotEmpty( $error_data['backtrace'] ); | ||
| } | ||
|
|
||
| public function test_failure_error_contains_post_id(): void { | ||
| $post = $this->factory()->post->create_and_get( [ 'post_status' => 'publish' ] ); | ||
|
|
||
|
|
||
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.
The method now re-fetches the post object using
get_post( $post_id ), but in both calling contexts (lines 75 and 91), the$postobject is already available in theingest_post()method's scope. This causes an unnecessary database query.Consider updating the method signature to accept the
\WP_Postobject directly instead of just the post ID, or make it optional and only callget_post()if the post object isn't provided. For example: