From 4d4caee3416c089e30beccd3be67ac67e04aec0a Mon Sep 17 00:00:00 2001 From: ildyria Date: Thu, 24 Aug 2023 20:28:00 +0200 Subject: [PATCH 1/3] fix no log write access inifinite loop --- app/Exceptions/Handler.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 0c9a152feb..4925c42cfe 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -19,12 +19,14 @@ use Illuminate\Http\Response; use Illuminate\Session\TokenMismatchException; use Illuminate\Support\Arr; +use Illuminate\Support\Str; use Illuminate\Support\Facades\Log; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; +use UnexpectedValueException; /** * Lychee's custom exception handler. @@ -94,6 +96,7 @@ class Handler extends ExceptionHandler protected $dontReport = [ TokenMismatchException::class, SessionExpiredException::class, + ]; /** @@ -229,6 +232,9 @@ protected function prepareResponse($request, \Throwable $e): RedirectResponse|Re return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e); } + if (Str::contains($e->getMessage(), 'could not be opened in append mode')){ + dd($e); + } if (!$this->isHttpException($e)) { $e = new HttpException(500, $e->getMessage(), $e); } @@ -399,7 +405,13 @@ public function report(\Throwable $e): void } $msg = $msg_ . PHP_EOL . $msg; } while ($e = $e->getPrevious()); - Log::log($severity->value, $msg); + try { + Log::log($severity->value, $msg); + } + catch (UnexpectedValueException $e) { + // This is thrown when we don't have write access to the logs. + abort(507, "Could not write in the logs. Check that storage/logs/ and containing files have proper permissions."); + } } /** From 7e9df3815e94c0f803cb53101ec48d82b8802a52 Mon Sep 17 00:00:00 2001 From: ildyria Date: Thu, 24 Aug 2023 20:44:53 +0200 Subject: [PATCH 2/3] If you forgot to adjust the write rights you may face some infinite loops --- app/Exceptions/Handler.php | 14 ++++---------- .../NoWriteAccessOnLogsExceptions.php | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 app/Exceptions/NoWriteAccessOnLogsExceptions.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 4925c42cfe..f2c4339e9e 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -19,14 +19,12 @@ use Illuminate\Http\Response; use Illuminate\Session\TokenMismatchException; use Illuminate\Support\Arr; -use Illuminate\Support\Str; use Illuminate\Support\Facades\Log; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; -use UnexpectedValueException; /** * Lychee's custom exception handler. @@ -96,7 +94,7 @@ class Handler extends ExceptionHandler protected $dontReport = [ TokenMismatchException::class, SessionExpiredException::class, - + NoWriteAccessOnLogsExceptions::class, ]; /** @@ -232,9 +230,6 @@ protected function prepareResponse($request, \Throwable $e): RedirectResponse|Re return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e); } - if (Str::contains($e->getMessage(), 'could not be opened in append mode')){ - dd($e); - } if (!$this->isHttpException($e)) { $e = new HttpException(500, $e->getMessage(), $e); } @@ -407,10 +402,9 @@ public function report(\Throwable $e): void } while ($e = $e->getPrevious()); try { Log::log($severity->value, $msg); - } - catch (UnexpectedValueException $e) { - // This is thrown when we don't have write access to the logs. - abort(507, "Could not write in the logs. Check that storage/logs/ and containing files have proper permissions."); + } catch (\UnexpectedValueException $e2) { + throw new NoWriteAccessOnLogsExceptions($e2); + // abort(507, 'Could not write in the logs. Check that storage/logs/ and containing files have proper permissions.'); } } diff --git a/app/Exceptions/NoWriteAccessOnLogsExceptions.php b/app/Exceptions/NoWriteAccessOnLogsExceptions.php new file mode 100644 index 0000000000..a571497e05 --- /dev/null +++ b/app/Exceptions/NoWriteAccessOnLogsExceptions.php @@ -0,0 +1,19 @@ + Date: Thu, 24 Aug 2023 21:28:53 +0200 Subject: [PATCH 3/3] fix phpstan --- app/Exceptions/Handler.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index f2c4339e9e..20ad4de790 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -402,6 +402,7 @@ public function report(\Throwable $e): void } while ($e = $e->getPrevious()); try { Log::log($severity->value, $msg); + /** @phpstan-ignore-next-line // Yes it is thrown, trust me.... */ } catch (\UnexpectedValueException $e2) { throw new NoWriteAccessOnLogsExceptions($e2); // abort(507, 'Could not write in the logs. Check that storage/logs/ and containing files have proper permissions.');