fix(error): harden logging, i18n, a11y and handler tests

This commit is contained in:
2026-03-24 22:00:14 +01:00
parent 44a357dfc4
commit a0d816caaa
10 changed files with 515 additions and 100 deletions

View File

@@ -7,6 +7,7 @@ final class ErrorLogger
private const LOG_DIR = 'storage/logs/errors';
private const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
private const MAX_ROTATED_FILES = 5;
private const REDACTION_MARKER = '[REDACTED]';
public static function log(\Throwable $exception, array $collectedData): void
{
@@ -77,6 +78,7 @@ final class ErrorLogger
$exceptionData = $collectedData['exception'] ?? [];
$requestData = $collectedData['request'] ?? [];
$envData = $collectedData['environment'] ?? [];
$exceptionMessage = (string) ($exceptionData['message'] ?? $exception->getMessage());
$traceSummary = '';
$frames = $exceptionData['trace'] ?? [];
@@ -93,7 +95,8 @@ final class ErrorLogger
'channel' => $requestData['channel'] ?? 'web',
'exception' => [
'class' => $exceptionData['class'] ?? get_class($exception),
'message' => $exceptionData['message'] ?? $exception->getMessage(),
'message' => self::REDACTION_MARKER,
'message_hash' => self::hashMessage($exceptionMessage),
'code' => $exceptionData['code'] ?? $exception->getCode(),
'file' => self::shortenPath($exceptionData['file'] ?? $exception->getFile()),
'line' => $exceptionData['line'] ?? $exception->getLine(),
@@ -101,7 +104,7 @@ final class ErrorLogger
'trace_summary' => trim($traceSummary),
'request' => [
'method' => $requestData['method'] ?? null,
'url' => $requestData['url'] ?? null,
'path' => self::normalizeRequestPath($requestData['url'] ?? null),
'ip_hash' => self::hashIp($requestData['ip'] ?? null),
'user_id' => $requestData['user_id'] ?? null,
'tenant_id' => $requestData['tenant_id'] ?? null,
@@ -120,6 +123,40 @@ final class ErrorLogger
return 'sha256:' . hash('sha256', $ip);
}
private static function hashMessage(string $message): ?string
{
$message = trim($message);
if ($message === '') {
return null;
}
return 'sha256:' . hash('sha256', $message);
}
private static function normalizeRequestPath(mixed $rawUrl): ?string
{
if (!is_string($rawUrl) || trim($rawUrl) === '') {
return null;
}
$path = parse_url($rawUrl, PHP_URL_PATH);
if (!is_string($path) || trim($path) === '') {
return '/';
}
$path = preg_replace('/[[:cntrl:]]+/', '', $path) ?? '';
$path = trim($path);
if ($path === '') {
return '/';
}
if (!str_starts_with($path, '/')) {
$path = '/' . $path;
}
return substr($path, 0, 500);
}
private static function shortenPath(string $path): string
{
$root = getcwd();