refactor: WriteFailed named constructors, Rollback runner, Errors read-back helpers (H6)
- WriteFailed::rolledBack()/partial() replace the 11-parameter constructor (now private); the hint/state of either error are plain $hint/$state. - Rollback::attempt(...steps) runs every undo step and collects failure messages; used by ContentCreate::discard, ContentUpdate::rollBack, MediaUpdate::rollBack and DiviSaver::restore. - Errors::clientMessage/codeOf/fieldsOf replace DiviReplaceTextAbility::reason, the JSON digging in MediaUpdateAbility::failure and ContentCreateAbility::fields. - Errors docblock documents the exception/WP_Error split. - ContentCreateAbility::discard takes the client reason as a parameter (both callers still pass REASON_EXTRAS; changing the Divi text needs a ruling). Error payloads unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ namespace LIT\MCP\Integrations\Divi\Pages;
|
||||
|
||||
use LIT\MCP\Abilities\Errors;
|
||||
use LIT\MCP\Abilities\FollowUp;
|
||||
use LIT\MCP\Abilities\Rollback;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\Integrations\WordPress\PostGuard;
|
||||
use LIT\MCP\Integrations\WordPress\RevisionPoint;
|
||||
@@ -78,14 +79,12 @@ class DiviSaver
|
||||
|
||||
if ($result instanceof WP_Error || $result === 0) {
|
||||
$message = $result instanceof WP_Error ? $result->get_error_message() : 'returned 0';
|
||||
throw new WriteFailed(
|
||||
throw WriteFailed::rolledBack(
|
||||
self::REASON,
|
||||
true,
|
||||
$postId,
|
||||
$revisionId,
|
||||
null,
|
||||
sprintf('wp_update_post(%d) refused: %s', $postId, $message),
|
||||
rolledBackState: self::keeping($revisionId)
|
||||
detail: sprintf('wp_update_post(%d) refused: %s', $postId, $message),
|
||||
state: self::keeping($revisionId),
|
||||
postId: $postId,
|
||||
revisionId: $revisionId
|
||||
);
|
||||
}
|
||||
|
||||
@@ -172,7 +171,7 @@ class DiviSaver
|
||||
private function restore(int $postId, string $before, ?int $revisionId, Throwable $cause): never
|
||||
{
|
||||
$detail = sprintf('Saving Divi content of post %d failed: %s', $postId, $cause->getMessage());
|
||||
try {
|
||||
$failures = Rollback::attempt(function () use ($postId, $before): void {
|
||||
$current = get_post($postId);
|
||||
if ($current instanceof WP_Post && PostGuard::missedSchedule($current)) {
|
||||
throw new RuntimeException('restoring would publish a missed schedule');
|
||||
@@ -185,19 +184,19 @@ class DiviSaver
|
||||
if ($restored instanceof WP_Error || $restored === 0) {
|
||||
throw new RuntimeException('previous content could not be written back');
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$detail .= '; rollback: ' . $e->getMessage();
|
||||
throw new WriteFailed(self::REASON, false, $postId, $revisionId, $cause, $detail);
|
||||
});
|
||||
if ($failures !== []) {
|
||||
$detail .= '; rollback: ' . $failures[0];
|
||||
throw WriteFailed::partial(self::REASON, $cause, $detail, $postId, $revisionId);
|
||||
}
|
||||
|
||||
throw new WriteFailed(
|
||||
throw WriteFailed::rolledBack(
|
||||
self::REASON,
|
||||
true,
|
||||
$postId,
|
||||
$revisionId,
|
||||
$cause,
|
||||
$detail,
|
||||
rolledBackState: self::keeping($revisionId)
|
||||
state: self::keeping($revisionId),
|
||||
postId: $postId,
|
||||
revisionId: $revisionId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user