|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flowpack\DecoupledContentStore\BackendUi; |
| 6 | + |
| 7 | +use Neos\Flow\Annotations as Flow; |
| 8 | + |
| 9 | +/** |
| 10 | + * @Flow\Scope("singleton") |
| 11 | + */ |
| 12 | +class RenderingErrorExtractor |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Extracts ERROR-prefixed lines and exception blocks from a raw log string. |
| 16 | + * |
| 17 | + * Log format produced by ContentReleaseLogger: |
| 18 | + * - error() => single line "[Renderer X] ERROR <message> [<json>]" |
| 19 | + * - logException() => one writeln with "<header>\n\n<msg>\n\n<trace>\n\n<json>" |
| 20 | + * |
| 21 | + * Strategy: split on blank lines into paragraphs; keep paragraphs that |
| 22 | + * contain an ERROR-prefixed line or PHP stack-trace markers, and also |
| 23 | + * include the paragraph immediately before a stack-trace paragraph (that |
| 24 | + * paragraph carries the exception message in logException output). |
| 25 | + * |
| 26 | + * @return string[] |
| 27 | + */ |
| 28 | + public function extractErrorBlocks(string $log): array |
| 29 | + { |
| 30 | + $log = trim($log); |
| 31 | + if ($log === '') { |
| 32 | + return []; |
| 33 | + } |
| 34 | + |
| 35 | + $paragraphs = preg_split('/\n\s*\n/', $log) ?: []; |
| 36 | + $hits = []; |
| 37 | + foreach ($paragraphs as $index => $paragraph) { |
| 38 | + $hasError = (bool)preg_match('/(?:^|] )ERROR /m', $paragraph); |
| 39 | + $hasTrace = (bool)preg_match('/^#\d+ /m', $paragraph); |
| 40 | + if (!$hasError && !$hasTrace) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + if ($hasTrace && $index > 0 && !isset($hits[$index - 1])) { |
| 44 | + $previous = $paragraphs[$index - 1]; |
| 45 | + if (trim($previous) !== '') { |
| 46 | + $hits[$index - 1] = $previous; |
| 47 | + } |
| 48 | + } |
| 49 | + $hits[$index] = $paragraph; |
| 50 | + } |
| 51 | + |
| 52 | + if ($hits !== []) { |
| 53 | + return $hits; |
| 54 | + } |
| 55 | + |
| 56 | + // No structured ERROR/trace matched. Drop INFO/DEBUG/NOTICE/WARN lines |
| 57 | + // (with or without an optional "[prefix] " section) plus a few known |
| 58 | + // operational status messages, and return whatever remains as a single |
| 59 | + // block — covers logs that use unfamiliar formats but still mark their |
| 60 | + // noise levels. |
| 61 | + $kept = []; |
| 62 | + foreach (explode("\n", $log) as $line) { |
| 63 | + if (preg_match('/^\s*(?:\[[^]]+]\s*)?(?:INFO|DEBUG|NOTICE|WARN(?:ING)?)\b/', $line)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + if (preg_match('/Restarting render worker\.?/', $line)) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + $kept[] = $line; |
| 70 | + } |
| 71 | + $remaining = trim(implode("\n", $kept)); |
| 72 | + return $remaining === '' ? [] : [$remaining]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the last node reference found in a worker log — the one the |
| 77 | + * worker was rendering when it failed. |
| 78 | + * |
| 79 | + * NodeRenderer logs a DEBUG line "Rendering document node variant" before |
| 80 | + * each render and a logException(...) entry on failure; both include the |
| 81 | + * node identifier in the JSON payload. The very last "node":"..." match in |
| 82 | + * the log is therefore the most likely failing node, whether the worker |
| 83 | + * threw an exception or got killed mid-render. |
| 84 | + */ |
| 85 | + public function extractLastAttemptedNode(string $log): ?string |
| 86 | + { |
| 87 | + if (trim($log) === '') { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + if (!preg_match_all( |
| 92 | + '/"node"\s*:\s*"((?:\\\\.|[^"\\\\])*)"(?:\s*,\s*"nodeUri"\s*:\s*"((?:\\\\.|[^"\\\\])*)")?/', |
| 93 | + $log, |
| 94 | + $matches, |
| 95 | + PREG_SET_ORDER |
| 96 | + )) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + $last = end($matches); |
| 101 | + $node = $this->decodeJsonString($last[1]); |
| 102 | + $uri = isset($last[2]) ? $this->decodeJsonString($last[2]) : ''; |
| 103 | + return $uri !== '' ? sprintf('%s | %s', $node, $uri) : $node; |
| 104 | + } |
| 105 | + |
| 106 | + private function decodeJsonString(string $raw): string |
| 107 | + { |
| 108 | + $decoded = json_decode('"' . $raw . '"'); |
| 109 | + return is_string($decoded) ? $decoded : str_replace('\\/', '/', $raw); |
| 110 | + } |
| 111 | +} |
0 commit comments