Commit 2e01ced4435 for woocommerce

commit 2e01ced4435ad012e84b2e6e76414ce22ff439f6
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date:   Tue Dec 1 09:46:03 2026 -0300

    Keep internal errors out of write ability responses when the change step fails (#71517)

    * Keep ability change exceptions out of REST responses

    Pre-payment listeners now run while an extension ability stages the order. Log unexpected exceptions at the change step and return the same generic error that callers received when the hook ran during save. Preserve the existing validation errors for WC_Data_Exception.

    * Test the change step catch with an ability whose change() throws

    The test that came with the catch made a pre-payment listener throw. On
    trunk that listener runs inside save(), which already catches the
    exception, so the test passed without the catch. The new test throws from
    change() itself and checks the fixed error, that the exception text stays
    out of the response, and the log line.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * Keep a fixed ability response when change or logging throws

    Type errors during a change and failures from the WooCommerce logger could reach clients through the WordPress callback error. Catch both at the change step and preserve validation errors for callers.

    * Clarify the scope of the ability error changelog entry

    The handler protects failures in the change step of a write ability. Describe that boundary in the release note.

    ---------

    Co-authored-by: Aarón Fas <aaron.fas@automattic.com>
    Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/wooai-1860-keep-the-text-of-an-internal-exception-out-of-an b/plugins/woocommerce/changelog/wooai-1860-keep-the-text-of-an-internal-exception-out-of-an
new file mode 100644
index 00000000000..adb5803bd64
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooai-1860-keep-the-text-of-an-internal-exception-out-of-an
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Keep internal error details out of the response when a WooCommerce ability fails during its change step.
diff --git a/plugins/woocommerce/src/Abilities/ActionableAbility.php b/plugins/woocommerce/src/Abilities/ActionableAbility.php
index b97674bebe2..d4f9897e903 100644
--- a/plugins/woocommerce/src/Abilities/ActionableAbility.php
+++ b/plugins/woocommerce/src/Abilities/ActionableAbility.php
@@ -230,6 +230,17 @@ abstract class ActionableAbility extends Ability {
 			$changed = $this->change( $subject, $input );
 		} catch ( \WC_Data_Exception $e ) {
 			$changed = new \WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
+		} catch ( \Throwable $e ) {
+			try {
+				wc_get_logger()->error(
+					sprintf( 'Ability "%s" failed during change: %s', $this->get_name(), $e->getMessage() ),
+					array( 'source' => 'woocommerce-abilities' )
+				);
+			} catch ( \Throwable $logging_error ) {
+				// A logging failure must not replace the fixed error response.
+				unset( $logging_error );
+			}
+			return self::save_failed();
 		}
 		$core_refused = is_wp_error( $changed );
 		if ( $core_refused ) {
diff --git a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
index 515dd1093bf..cc4642fa96b 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
@@ -5252,6 +5252,53 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 		$this->assertSame( 'Pen', wc_get_product( $product->get_id() )->get_name() );
 	}

+	/**
+	 * Change failures with a working or failing logger.
+	 *
+	 * @return array
+	 */
+	public static function provider_change_failures(): array {
+		return array(
+			'exception'      => array( new \RuntimeException( 'Ledger table is locked.' ), false ),
+			'type error'     => array( new \TypeError( 'Invalid ledger type.' ), false ),
+			'logger failure' => array( new \RuntimeException( 'Ledger table is locked.' ), true ),
+		);
+	}
+
+	/**
+	 * @testdox Should return a fixed 500 error for a failed change even if logging fails.
+	 * @dataProvider provider_change_failures
+	 *
+	 * @param \Throwable $failure      Failure from the change step.
+	 * @param bool       $logger_fails Whether the logger throws.
+	 */
+	public function test_a_change_that_throws_returns_a_fixed_error( \Throwable $failure, bool $logger_fails ): void {
+		$this->register_order_status_ability( TestThrowingOrderAbility::class );
+		$ability = wp_get_ability( 'test-ext/order-status' );
+		$this->assertInstanceOf( TestThrowingOrderAbility::class, $ability );
+		$ability->failure = $failure;
+
+		$order  = \WC_Helper_Order::create_order();
+		$logger = $this->getMockBuilder( \WC_Logger_Interface::class )->getMock();
+		$log    = $logger->expects( $this->once() )->method( 'error' )->with(
+			'Ability "test-ext/order-status" failed during change: ' . $failure->getMessage(),
+			array( 'source' => 'woocommerce-abilities' )
+		);
+		if ( $logger_fails ) {
+			$log->willThrowException( new \RuntimeException( 'Logger broke' ) );
+		}
+		add_filter( 'woocommerce_logging_class', static fn() => $logger );
+
+		$response = $this->run_ability_response( 'test-ext/order-status', array( 'id' => $order->get_id() ) );
+
+		$this->assertSame( 500, $response->get_status() );
+		$this->assertSame( 'woocommerce_ability_save_failed', $response->get_data()['code'] );
+		$this->assertSame( 'The change could not be saved.', $response->get_data()['message'] );
+		$data = wp_json_encode( $response->get_data() );
+		$this->assertStringNotContainsString( $failure->getMessage(), $data );
+		$this->assertStringNotContainsString( 'Logger broke', $data );
+	}
+
 	/**
 	 * @testdox Should return a fixed 500 error when the order write fails, though the order's save() catches the exception.
 	 */
diff --git a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/TestThrowingOrderAbility.php b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/TestThrowingOrderAbility.php
new file mode 100644
index 00000000000..ed2df506554
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/TestThrowingOrderAbility.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * TestThrowingOrderAbility class file.
+ */
+
+declare( strict_types=1 );
+
+namespace Automattic\WooCommerce\Tests\Internal\AbilitiesApi;
+
+/**
+ * An extension's write ability whose change step throws.
+ */
+class TestThrowingOrderAbility extends TestOrderStatusAbility {
+
+	/**
+	 * Failure to throw during the change step.
+	 *
+	 * @var \Throwable
+	 */
+	public $failure;
+
+	/**
+	 * Throw the configured failure.
+	 *
+	 * @param \WC_Order $subject Order.
+	 * @param array     $input   Ability input.
+	 * @return never
+	 * @throws \Throwable Configured failure.
+	 */
+	protected function change( $subject, array $input ) {
+		throw $this->failure;
+	}
+}