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:
@@ -7,12 +7,14 @@ namespace LIT\MCP\Integrations\WordPress\Content;
|
||||
use LIT\MCP\Abilities\Ability;
|
||||
use LIT\MCP\Abilities\Errors;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\Abilities\Rollback;
|
||||
use LIT\MCP\Abilities\ToolResult;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\PostChangeResult;
|
||||
use LIT\MCP\Integrations\WordPress\PostGuard;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
use WP_Error;
|
||||
use WP_Post;
|
||||
@@ -137,7 +139,7 @@ final class ContentCreateAbility implements Ability
|
||||
// A hook threw – the row may or may not exist and its ID is unknown; nothing is
|
||||
// searched or deleted, the client is told to check (lit_partial_write without id).
|
||||
$this->logPartial($postType, '', 'Anlegen abgebrochen; Entwurf möglicherweise vorhanden');
|
||||
throw new WriteFailed(self::REASON_INSERT, false, null, null, $e);
|
||||
throw WriteFailed::partial(self::REASON_INSERT, $e);
|
||||
}
|
||||
if ($postId instanceof WP_Error) {
|
||||
return Errors::invalidInput('WordPress hat den Eintrag abgelehnt: ' . $postId->get_error_message());
|
||||
@@ -153,7 +155,7 @@ final class ContentCreateAbility implements Ability
|
||||
try {
|
||||
$layout = $withDivi ? $this->divi?->write($postId, $postType, self::sections($input)) : null;
|
||||
} catch (Throwable $e) {
|
||||
$this->discard($postId, $postType, $e);
|
||||
$this->discard($postId, $postType, $e, self::REASON_EXTRAS);
|
||||
}
|
||||
if ($layout instanceof WP_Error) {
|
||||
return $this->refuse($postId, $postType, $layout);
|
||||
@@ -162,7 +164,7 @@ final class ContentCreateAbility implements Ability
|
||||
try {
|
||||
$this->writer->writeExtras($postId, $postType, $prepared, $input);
|
||||
} catch (Throwable $e) {
|
||||
$this->discard($postId, $postType, $e);
|
||||
$this->discard($postId, $postType, $e, self::REASON_EXTRAS);
|
||||
}
|
||||
|
||||
$warnings = array_merge($prepared['warnings'], $layout['warnings'] ?? []);
|
||||
@@ -207,7 +209,7 @@ final class ContentCreateAbility implements Ability
|
||||
return $prepared instanceof WP_Error ? $prepared : $divi;
|
||||
}
|
||||
|
||||
$fields = array_merge(self::fields($prepared), self::fields($divi));
|
||||
$fields = array_merge(Errors::fieldsOf($prepared), Errors::fieldsOf($divi));
|
||||
if ($fields === []) {
|
||||
return $prepared;
|
||||
}
|
||||
@@ -217,14 +219,6 @@ final class ContentCreateAbility implements Ability
|
||||
: Errors::invalidInput('Eingaben sind ungültig; nichts wurde angelegt.', $fields);
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
private static function fields(WP_Error $error): array
|
||||
{
|
||||
$data = $error->get_error_data();
|
||||
|
||||
return is_array($data) && is_array($data['fields'] ?? null) ? $data['fields'] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @param list<string> $warnings
|
||||
@@ -254,42 +248,38 @@ final class ContentCreateAbility implements Ability
|
||||
|
||||
/**
|
||||
* Rolls the brand-new draft back: terms it created, then the draft itself (force –
|
||||
* it cannot have been used anywhere yet).
|
||||
* it cannot have been used anywhere yet). $reason is the client text of the error.
|
||||
*
|
||||
* @throws WriteFailed Always.
|
||||
*/
|
||||
private function discard(int $postId, string $postType, Throwable $cause): never
|
||||
private function discard(int $postId, string $postType, Throwable $cause, string $reason): never
|
||||
{
|
||||
$problems = [];
|
||||
try {
|
||||
$this->writer->deleteCreatedTerms();
|
||||
} catch (Throwable $e) {
|
||||
$problems[] = $e->getMessage();
|
||||
}
|
||||
|
||||
try {
|
||||
if (! wp_delete_post($postId, true)) {
|
||||
$problems[] = sprintf('draft %d could not be deleted', $postId);
|
||||
$problems = Rollback::attempt(
|
||||
fn () => $this->writer->deleteCreatedTerms(),
|
||||
static function () use ($postId): void {
|
||||
try {
|
||||
$deleted = wp_delete_post($postId, true);
|
||||
} catch (Throwable $e) {
|
||||
throw new RuntimeException(
|
||||
sprintf('draft %d could not be deleted: %s', $postId, $e->getMessage()),
|
||||
0,
|
||||
$e
|
||||
);
|
||||
}
|
||||
if (! $deleted) {
|
||||
throw new RuntimeException(sprintf('draft %d could not be deleted', $postId));
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$problems[] = sprintf('draft %d could not be deleted: %s', $postId, $e->getMessage());
|
||||
}
|
||||
);
|
||||
|
||||
$detail = sprintf('Extras of new post %d could not be written: %s', $postId, $cause->getMessage());
|
||||
if ($problems === []) {
|
||||
throw new WriteFailed(self::REASON_EXTRAS, true, null, null, $cause, $detail);
|
||||
throw WriteFailed::rolledBack($reason, $cause, $detail);
|
||||
}
|
||||
|
||||
$summary = sprintf('Entwurf %d: Anlegen nur teilweise zurückgenommen', $postId);
|
||||
$this->logPartial($postType, (string) $postId, $summary);
|
||||
throw new WriteFailed(
|
||||
self::REASON_EXTRAS,
|
||||
false,
|
||||
$postId,
|
||||
null,
|
||||
$cause,
|
||||
$detail . '; rollback: ' . implode('; ', $problems)
|
||||
);
|
||||
throw WriteFailed::partial($reason, $cause, $detail . '; rollback: ' . implode('; ', $problems), $postId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -311,15 +301,12 @@ final class ContentCreateAbility implements Ability
|
||||
if (! $deleted) {
|
||||
$summary = sprintf('Entwurf %d: Anlegen nicht zurückgenommen', $postId);
|
||||
$this->logPartial($postType, (string) $postId, $summary);
|
||||
$fields = $denied instanceof WP_Error ? self::fields($denied) : $denied;
|
||||
throw new WriteFailed(
|
||||
$fields = $denied instanceof WP_Error ? Errors::fieldsOf($denied) : $denied;
|
||||
throw WriteFailed::partial(
|
||||
($denied instanceof WP_Error ? 'Ungültiger Divi-Inhalt: ' : 'Keine Berechtigung für ')
|
||||
. implode(', ', array_keys($fields)) . '.',
|
||||
false,
|
||||
$postId,
|
||||
null,
|
||||
null,
|
||||
sprintf('New post %d refused after the insert; draft could not be deleted', $postId)
|
||||
detail: sprintf('New post %d refused after the insert; draft could not be deleted', $postId),
|
||||
postId: $postId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user