feat: improve logging and error handling (#2994)

* feat: improve logging and error handling

* trim absolute path from file name

* fix: suppress php errors from xml parsing

* fix: respect the error reporting level in the custom error handler

* feat: dont log error which is produced by bots

* ignore error about invalid bridge name

* upgrade bridge exception from warning to error

* remove remnants of using phps builin error handler

* move responsibility of printing php error from logger to error handler

* feat: include url in log record context

* fix: always include url in log record contect

Also ignore more non-interesting exceptions.

* more verbose httpexception

* fix

* fix
This commit is contained in:
Dag 2022-09-08 19:07:57 +02:00 committed by GitHub
parent 5578a735d9
commit 27b3d7c34e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 135 additions and 80 deletions

View file

@ -19,13 +19,13 @@ final class Json
}
/**
* Returns e.g. 'https://example.com/' or 'https://example.com/bridge/'
* Get the home page url of rss-bridge e.g. 'https://example.com/' or 'https://example.com/bridge/'
*/
function get_home_page_url(): string
{
$https = $_SERVER['HTTPS'] ?? null;
$host = $_SERVER['HTTP_HOST'] ?? null;
$uri = $_SERVER['REQUEST_URI'] ?? null;
$https = $_SERVER['HTTPS'] ?? '';
$host = $_SERVER['HTTP_HOST'] ?? '';
$uri = $_SERVER['REQUEST_URI'] ?? '';
if (($pos = strpos($uri, '?')) !== false) {
$uri = substr($uri, 0, $pos);
}
@ -33,6 +33,29 @@ function get_home_page_url(): string
return "$scheme://$host$uri";
}
/**
* Get the full current url e.g. 'http://example.com/?action=display&bridge=FooBridge'
*/
function get_current_url(): string
{
$https = $_SERVER['HTTPS'] ?? '';
$host = $_SERVER['HTTP_HOST'] ?? '';
$uri = $_SERVER['REQUEST_URI'] ?? '';
$scheme = $https === 'on' ? 'https' : 'http';
return "$scheme://$host$uri";
}
function create_sane_exception_message(\Throwable $e): string
{
return sprintf(
'Exception %s: %s at %s line %s',
get_class($e),
$e->getMessage(),
trim_path_prefix($e->getFile()),
$e->getLine()
);
}
function create_sane_stacktrace(\Throwable $e): array
{
$frames = array_reverse($e->getTrace());
@ -40,18 +63,18 @@ function create_sane_stacktrace(\Throwable $e): array
'file' => $e->getFile(),
'line' => $e->getLine(),
];
$stackTrace = [];
$trace = [];
foreach ($frames as $i => $frame) {
$file = $frame['file'] ?? '(no file)';
$line = $frame['line'] ?? '(no line)';
$stackTrace[] = sprintf(
$trace[] = sprintf(
'#%s %s:%s',
$i,
trim_path_prefix($file),
$line,
);
}
return $stackTrace;
return $trace;
}
/**
@ -151,3 +174,8 @@ function format_bytes(int $bytes, $precision = 2)
return round($bytes, $precision) . ' ' . $units[$pow];
}
function now(): \DateTimeImmutable
{
return new \DateTimeImmutable();
}