tinytinyrss/include/errorhandler.php

89 lines
2.2 KiB
PHP
Raw Normal View History

2013-04-16 17:41:31 +02:00
<?php
/**
* @param array<int, array<string, mixed>> $trace
*/
function format_backtrace($trace): string {
2015-12-04 13:58:20 +01:00
$rv = "";
$idx = 1;
if (is_array($trace)) {
foreach ($trace as $e) {
if (isset($e["file"]) && isset($e["line"])) {
$fmt_args = [];
if (is_array($e["args"] ?? false)) {
2015-12-04 13:58:20 +01:00
foreach ($e["args"] as $a) {
if (is_object($a)) {
2021-02-06 08:10:54 +01:00
array_push($fmt_args, "{" . get_class($a) . "}");
} else if (is_array($a)) {
2021-02-06 08:10:54 +01:00
array_push($fmt_args, "[" . truncate_string(json_encode($a), 256, "...")) . "]";
} else if (is_resource($a)) {
array_push($fmt_args, truncate_string(get_resource_type($a), 256, "..."));
} else if (is_string($a)) {
2021-02-06 08:10:54 +01:00
array_push($fmt_args, truncate_string($a, 256, "..."));
2015-12-04 13:58:20 +01:00
}
}
}
$filename = str_replace(dirname(__DIR__) . "/", "", $e["file"]);
$rv .= sprintf("%d. %s(%s): %s(%s)\n",
$idx,
$filename,
$e["line"],
$e["function"],
implode(", ", $fmt_args));
2015-12-04 13:58:20 +01:00
$idx++;
}
}
}
return $rv;
}
function ttrss_error_handler(int $errno, string $errstr, string $file, int $line): bool {
// return true in order to avoid default error handling by PHP
2022-09-29 16:46:33 +02:00
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
if (error_reporting() == 0 || !$errno) return true;
} else {
if (!(error_reporting() & $errno)) return true;
}
2021-02-22 15:38:46 +01:00
$file = substr(str_replace(dirname(__DIR__), "", $file), 1);
2013-04-16 17:41:31 +02:00
2015-12-04 13:58:20 +01:00
$context = format_backtrace(debug_backtrace());
$errstr = truncate_middle($errstr, 16384, " (...) ");
if (class_exists("Logger"))
return Logger::log_error((int)$errno, $errstr, $file, (int)$line, $context);
else
return false;
2013-04-16 17:41:31 +02:00
}
function ttrss_fatal_handler(): bool {
2013-04-16 17:41:31 +02:00
$error = error_get_last();
if ($error !== NULL) {
2013-04-17 06:32:45 +02:00
$errno = $error["type"];
2013-04-16 17:41:31 +02:00
$file = $error["file"];
$line = $error["line"];
$errstr = $error["message"];
2013-04-17 15:00:24 +02:00
if (!$errno) return false;
2015-12-04 13:58:20 +01:00
$context = format_backtrace(debug_backtrace());
2013-04-16 17:41:31 +02:00
2021-02-22 15:38:46 +01:00
$file = substr(str_replace(dirname(__DIR__), "", $file), 1);
2013-04-16 17:41:31 +02:00
if (class_exists("Logger"))
return Logger::log_error((int)$errno, $errstr, $file, (int)$line, $context);
2013-04-16 17:41:31 +02:00
}
return false;
2013-04-16 17:41:31 +02:00
}
2013-04-17 22:22:34 +02:00
register_shutdown_function('ttrss_fatal_handler');
set_error_handler('ttrss_error_handler');
2017-04-26 19:24:18 +02:00