fix: typed write failures, template rollback, parent and term cleanup
WriteFailed maps to lit_write_failed / lit_partial_write (partial writes are logged with result partial); old template read before the update; row marker set before wp_update_post; created terms deleted on rollback; parent must be a readable non-descendant of a hierarchical type; featured image must be readable; snapshot keeps empty values; meta comparison after sanitize_meta; follow-up failures (listener, change log) only add warnings. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -71,6 +71,10 @@ final class AbilityRegistrar
|
||||
'execute_callback' => static function (mixed $input = null) use ($ability): mixed {
|
||||
try {
|
||||
return $ability->execute(is_array($input) ? $input : []);
|
||||
} catch (WriteFailed $e) {
|
||||
$message = sprintf('[wp-lit-mcp] %s failed: %s', $ability->name(), $e);
|
||||
error_log($message); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
|
||||
return $e->toError();
|
||||
} catch (Throwable $e) {
|
||||
$message = sprintf('[wp-lit-mcp] %s failed: %s', $ability->name(), $e);
|
||||
error_log($message); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
|
||||
|
||||
@@ -98,6 +98,44 @@ final class Errors
|
||||
return self::make('unsupported', $message, $hint, 400);
|
||||
}
|
||||
|
||||
/** A write failed and was rolled back completely. */
|
||||
public static function writeFailed(string $reason): WP_Error
|
||||
{
|
||||
return self::make(
|
||||
'write_failed',
|
||||
trim('Speichern fehlgeschlagen: ' . $reason) . ' Nichts wurde gespeichert.',
|
||||
'Eingaben prüfen, ggf. korrigieren und erneut versuchen; tritt der Fehler wieder auf, den Administrator '
|
||||
. 'informieren (Details im PHP-Error-Log der Website).',
|
||||
500
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A write failed and could not be rolled back completely – something may have been saved.
|
||||
*/
|
||||
public static function partialWrite(string $reason, ?int $postId = null, ?int $revisionId = null): WP_Error
|
||||
{
|
||||
$state = $postId === null
|
||||
? 'Möglicherweise wurde trotzdem ein Entwurf angelegt.'
|
||||
: sprintf('Eintrag %d ist möglicherweise teilweise geändert bzw. angelegt worden', $postId)
|
||||
. ($revisionId === null ? '.' : sprintf(' (Stand vorher: Revision %d).', $revisionId));
|
||||
$details = array_filter(
|
||||
['post_id' => $postId, 'revision_id' => $revisionId],
|
||||
static fn (?int $id): bool => $id !== null
|
||||
);
|
||||
|
||||
return self::make(
|
||||
'partial_write',
|
||||
trim('Speichern fehlgeschlagen: ' . $reason) . ' Die Änderung konnte nicht vollständig zurückgenommen '
|
||||
. 'werden. ' . $state,
|
||||
'Den Aufruf nicht einfach wiederholen. Zuerst den aktuellen Stand prüfen (lit-content-get, bei einem '
|
||||
. 'neuen Entwurf lit-content-query) und ggf. über lit-revisions-list den vorherigen Stand '
|
||||
. 'wiederherstellen; dem Nutzer Bescheid geben.',
|
||||
500,
|
||||
$details
|
||||
);
|
||||
}
|
||||
|
||||
public static function internal(): WP_Error
|
||||
{
|
||||
return self::make(
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Abilities;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Runs a step after a write has already succeeded (listeners, change log): a failure
|
||||
* there must not turn the saved change into an error. It is logged and reported as warning.
|
||||
*/
|
||||
final class FollowUp
|
||||
{
|
||||
/**
|
||||
* @param list<string> $warnings Receives $warning when the step fails.
|
||||
*/
|
||||
public static function run(callable $step, string $context, string $warning, array &$warnings): mixed
|
||||
{
|
||||
try {
|
||||
return $step();
|
||||
} catch (Throwable $e) {
|
||||
$message = sprintf('[wp-lit-mcp] %s failed: %s', $context, $e);
|
||||
error_log($message); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
|
||||
$warnings[] = $warning;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Abilities;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
use WP_Error;
|
||||
|
||||
/**
|
||||
* Thrown by write paths when saving failed after something may already have been
|
||||
* written. getMessage() is the internal detail for the PHP error log; $reason is the
|
||||
* client-safe German text. The AbilityRegistrar logs the detail and returns toError():
|
||||
* lit_write_failed when everything was rolled back, lit_partial_write otherwise.
|
||||
*/
|
||||
final class WriteFailed extends RuntimeException
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $reason,
|
||||
public readonly bool $rolledBack,
|
||||
public readonly ?int $postId = null,
|
||||
public readonly ?int $revisionId = null,
|
||||
?Throwable $previous = null,
|
||||
string $detail = ''
|
||||
) {
|
||||
$internal = $detail !== '' ? $detail : ($previous !== null ? $previous->getMessage() : $reason);
|
||||
parent::__construct(
|
||||
sprintf('%s [%s]', $internal, $rolledBack ? 'rolled back' : 'rollback incomplete'),
|
||||
0,
|
||||
$previous
|
||||
);
|
||||
}
|
||||
|
||||
public function toError(): WP_Error
|
||||
{
|
||||
return $this->rolledBack
|
||||
? Errors::writeFailed($this->reason)
|
||||
: Errors::partialWrite($this->reason, $this->postId, $this->revisionId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user