", the * message carries the client JSON {code, message, hint, fields?, …}). */ trait AssertsToolErrors { /** * Asserts a tool error with $code as the client sees it (without "lit_"): the WP_Error code * and the code in the JSON payload. * * @return array The client payload. */ public static function assertToolError(mixed $result, string $code, string $message = ''): array { Assert::assertInstanceOf(WP_Error::class, $result, $message); Assert::assertSame('lit_' . $code, $result->get_error_code(), $message); $payload = self::clientError($result); Assert::assertSame($code, $payload['code'] ?? null, $message); return $payload; } /** @return array The structured error the client sees (message JSON, spec §8.2). */ public static function clientError(WP_Error $error): array { return (array) json_decode($error->get_error_message(), true); } /** @return array The error's fields (input path => problem); asserts it has some. */ public static function errorFields(WP_Error $error): array { $data = $error->get_error_data(); Assert::assertIsArray($data, 'error data'); Assert::assertIsArray($data['fields'] ?? null, 'error fields'); return $data['fields']; } }