Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/scripts/error_handler_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
// php .github/scripts/error_handler_test.php [plugin-dir] — lib.php's error handler copies a warning into FV3's error.log only when a file
// under FV3's folder raised it; PHP's own log (Unraid's phplog) still gets every warning
$fv3tPlugin = $argv[1] ?? dirname(__DIR__, 2) . '/src/folder.view3/usr/local/emhttp/plugins/folder.view3';
$fv3tTmp = realpath(sys_get_temp_dir()) . '/fv3-error-handler-test-' . bin2hex(random_bytes(4));
$docroot = "$fv3tTmp/docroot";

$fv3tFailed = 0;
function check(string $label, bool $ok, $detail = null): void {
global $fv3tFailed;
if ($ok) { echo "ok $label\n"; return; }
$fv3tFailed++;
echo "FAIL $label" . ($detail === null ? '' : ' — ' . json_encode($detail, JSON_UNESCAPED_SLASHES)) . "\n";
}

// The plugin copied to where Unraid installs it, so its files' warnings name paths under that folder
@mkdir("$docroot/plugins", 0777, true);
exec('cp -R ' . escapeshellarg($fv3tPlugin) . ' ' . escapeshellarg("$docroot/plugins/folder.view3") . ' 2>&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' => '<?php', 'plugins/dynamix.docker.manager/include/DockerClient.php' => '<?php class DockerClient {} class DockerUpdate {}'];
foreach ($warns as $key => $path) $files[substr($path, strlen("$docroot/"))] = "<?php\n\$none = []; return \$none[" . var_export($key, true) . "];\n";
foreach ($files as $rel => $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);
6 changes: 2 additions & 4 deletions .github/unraid-compat/fake_logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
Loading