diff --git a/.github/scripts/error_handler_test.php b/.github/scripts/error_handler_test.php new file mode 100644 index 00000000..a7ed48e5 --- /dev/null +++ b/.github/scripts/error_handler_test.php @@ -0,0 +1,52 @@ +&1', $out, $rc); +check('the plugin was copied', $rc === 0 && is_file("$docroot/plugins/folder.view3/server/lib.php"), $out); +// Each warns on its line 2: an FV3 file, Unraid's VM code, and a folder whose name only starts like FV3's +$warns = [ + 'fv3-own' => "$docroot/plugins/folder.view3/server/fv3t_warn.php", + 'unraid-own' => "$docroot/plugins/dynamix.vm.manager/include/fv3t_warn.php", + 'name-prefix' => "$docroot/plugins/folder.view3-copy/fv3t_warn.php", +]; +// lib.php requires these two Unraid host files at load time +$files = ['webGui/include/Helpers.php' => ' ' $path) $files[substr($path, strlen("$docroot/"))] = " $src) { + @mkdir(dirname("$docroot/$rel"), 0777, true); + file_put_contents("$docroot/$rel", $src); +} + +// A process of its own, as an endpoint request runs: lib.php's handler and the log settings stay out of this test's PHP +$request = '$_SERVER["DOCUMENT_ROOT"] = ' . var_export($docroot, true) . '; require ' . var_export("$docroot/plugins/folder.view3/server/lib.php", true) . ';' + . ' $configDir = ' . var_export("$fv3tTmp/config", true) . '; foreach (' . var_export(array_values($warns), true) . ' as $f) include $f;'; +$out = []; +exec(escapeshellarg(PHP_BINARY) . ' -d error_reporting=-1 -d display_errors=0 -d log_errors=1 -d error_log=' . escapeshellarg("$fv3tTmp/php.log") + . ' -r ' . escapeshellarg($request) . ' 2>&1', $out, $rc); +check('the request ran with no output', $rc === 0 && $out === [], ['rc' => $rc, 'out' => $out]); + +$logged = @file("$fv3tTmp/config/error.log", FILE_IGNORE_NEW_LINES) ?: []; +check("error.log holds the FV3 file's warning and nothing else", + count($logged) === 1 && preg_match('/^\[\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\] php-error: Undefined array key "fv3-own" at fv3t_warn\.php:2$/', $logged[0]), $logged); +$phplog = (string)@file_get_contents("$fv3tTmp/php.log"); +foreach ($warns as $key => $path) { + check("PHP's own log still has the $key warning", str_contains($phplog, "PHP Warning: Undefined array key \"$key\" in $path on line 2"), $phplog); +} + +exec('rm -rf ' . escapeshellarg($fv3tTmp)); +echo $fv3tFailed ? "\n$fv3tFailed FAILED\n" : "\nall passed\n"; +exit($fv3tFailed ? 1 : 0); diff --git a/.github/unraid-compat/fake_logs.php b/.github/unraid-compat/fake_logs.php index 916309c3..0e3528c4 100644 --- a/.github/unraid-compat/fake_logs.php +++ b/.github/unraid-compat/fake_logs.php @@ -21,12 +21,10 @@ function fv3_fake_log_checks(): array { // No script died or failed an include: in Unraid's own code, a sign of state the fakes lack $broken = preg_grep('/PHP Fatal error|Failed opening .* for inclusion/', $phplog); $checks[] = [!$broken, "PHP died or missed a file:\n " . implode("\n ", array_slice(array_unique($broken), 0, 20))]; - // FV3's error log copies every warning raised in its requests, Unraid's too, by basename. With VM debug on, a real Unraid's - // libvirt.php adds one when it keys an array by a domain handle; any other line fails - $handleWarning = '/^\[[^]]+\] php-error: Resource ID#\d+ used as offset, casting to integer \(\d+\) at libvirt\.php:\d+$/'; + // Any line in FV3's error log fails: a clean run logs nothing there, and Unraid's own warnings stay in its phplog // No error.log means nothing logged only while its folder is there $checks[] = [is_dir(FV3T_FLASH), FV3T_FLASH . ' is missing, so its error.log was never read']; - $reported = preg_grep($handleWarning, $read(FV3T_FLASH . '/error.log', true), PREG_GREP_INVERT); + $reported = $read(FV3T_FLASH . '/error.log', true); $checks[] = [!$reported, "FV3's error.log:\n " . implode("\n ", $reported)]; $unknown = preg_grep('/UNKNOWN/', $read('/var/log/fv3-fakes.log')); $checks[] = [!$unknown, "the fakes were asked what they do not answer:\n " . implode("\n ", array_unique($unknown))]; diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.php b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.php index 0118c9a1..a34941c2 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.php +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.php @@ -164,9 +164,11 @@ function fv3_read_error_log_tail(): ?string { if (!headers_sent()) { http_response_code(500); header('Content-Type: application/json'); } echo json_encode(['error' => 'Internal error']); }); - // Non-fatal PHP errors don't end the request, so only log them; deprecations are noise, not failures + // Non-fatal PHP errors don't end the request, so only log them; deprecations are noise, not failures. + // Only files under FV3's folder: a warning raised in Unraid's code (libvirt.php...) stays in Unraid's phplog alone set_error_handler(function(int $severity, string $message, string $file, int $line): bool { if (!(error_reporting() & $severity) || ($severity & (E_DEPRECATED | E_USER_DEPRECATED))) return false; + if (!str_starts_with($file, dirname(__DIR__) . '/')) return false; fv3_error_log('php-error', "$message at " . basename($file) . ":$line"); return false; });