From e5c59c498494dd2a868b84096cdaf3b330dbde3e Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 10 Mar 2018 13:02:41 -0600 Subject: [PATCH] Change exception handling for display exception --- CHANGELOG.md | 1 + app/Exceptions/DisplayException.php | 33 +++++++++++++++++++++++------ app/Exceptions/Handler.php | 13 +++++++++++- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68d057df8..17ab8f05c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * Sessions handled through redis now use a seperate database (default `1`) to store session database to avoid logging users out when flushing the cache. * File manager UI improved to be clearer with buttons and cleaner on mobile. * reCAPTCHA's secret key position swapped with website key in advanced panel settings to be consistent with Google's reCAPTCHA dashboard. +* Changed DisplayException to handle its own logging correctly and check if the previous exception is marked as one that should not be logged. ## v0.7.5 (Derelict Dermodactylus) ### Fixed diff --git a/app/Exceptions/DisplayException.php b/app/Exceptions/DisplayException.php index e3a839c5e..a00939711 100644 --- a/app/Exceptions/DisplayException.php +++ b/app/Exceptions/DisplayException.php @@ -2,9 +2,11 @@ namespace Pterodactyl\Exceptions; -use Log; +use Exception; use Throwable; +use Psr\Log\LoggerInterface; use Illuminate\Http\Response; +use Illuminate\Container\Container; use Prologue\Alerts\AlertsMessageBag; class DisplayException extends PterodactylException @@ -31,10 +33,6 @@ class DisplayException extends PterodactylException { parent::__construct($message, $code, $previous); - if (! is_null($previous)) { - Log::{$level}($previous); - } - $this->level = $level; } @@ -70,8 +68,31 @@ class DisplayException extends PterodactylException ]), method_exists($this, 'getStatusCode') ? $this->getStatusCode() : Response::HTTP_BAD_REQUEST); } - app()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash(); + Container::getInstance()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash(); return redirect()->back()->withInput(); } + + /** + * Log the exception to the logs using the defined error level only if the previous + * exception is set. + * + * @return mixed + * + * @throws \Exception + */ + public function report() + { + if (! $this->getPrevious() instanceof Exception || ! Handler::isReportable($this->getPrevious())) { + return null; + } + + try { + $logger = Container::getInstance()->make(LoggerInterface::class); + } catch (Exception $ex) { + throw $this->getPrevious(); + } + + return $logger->{$this->getErrorLevel()}($this->getPrevious()); + } } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 500f2d0ca..be8a4e556 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -5,6 +5,7 @@ namespace Pterodactyl\Exceptions; use Exception; use PDOException; use Psr\Log\LoggerInterface; +use Illuminate\Container\Container; use Illuminate\Auth\AuthenticationException; use Illuminate\Session\TokenMismatchException; use Illuminate\Validation\ValidationException; @@ -24,7 +25,6 @@ class Handler extends ExceptionHandler protected $dontReport = [ AuthenticationException::class, AuthorizationException::class, - DisplayException::class, HttpException::class, ModelNotFoundException::class, RecordNotFoundException::class, @@ -201,6 +201,17 @@ class Handler extends ExceptionHandler return ['errors' => [array_merge($error, $override)]]; } + /** + * Return an array of exceptions that should not be reported. + * + * @param \Exception $exception + * @return bool + */ + public static function isReportable(Exception $exception): bool + { + return (new static(Container::getInstance()))->shouldReport($exception); + } + /** * Convert an authentication exception into an unauthenticated response. *