Skip to content

Commit e5c59c4

Browse files
committed
Change exception handling for display exception
1 parent ef371a5 commit e5c59c4

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1515
* Sessions handled through redis now use a seperate database (default `1`) to store session database to avoid logging users out when flushing the cache.
1616
* File manager UI improved to be clearer with buttons and cleaner on mobile.
1717
* reCAPTCHA's secret key position swapped with website key in advanced panel settings to be consistent with Google's reCAPTCHA dashboard.
18+
* Changed DisplayException to handle its own logging correctly and check if the previous exception is marked as one that should not be logged.
1819

1920
## v0.7.5 (Derelict Dermodactylus)
2021
### Fixed

app/Exceptions/DisplayException.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Pterodactyl\Exceptions;
44

5-
use Log;
5+
use Exception;
66
use Throwable;
7+
use Psr\Log\LoggerInterface;
78
use Illuminate\Http\Response;
9+
use Illuminate\Container\Container;
810
use Prologue\Alerts\AlertsMessageBag;
911

1012
class DisplayException extends PterodactylException
@@ -31,10 +33,6 @@ public function __construct($message, Throwable $previous = null, $level = self:
3133
{
3234
parent::__construct($message, $code, $previous);
3335

34-
if (! is_null($previous)) {
35-
Log::{$level}($previous);
36-
}
37-
3836
$this->level = $level;
3937
}
4038

@@ -70,8 +68,31 @@ public function render($request)
7068
]), method_exists($this, 'getStatusCode') ? $this->getStatusCode() : Response::HTTP_BAD_REQUEST);
7169
}
7270

73-
app()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash();
71+
Container::getInstance()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash();
7472

7573
return redirect()->back()->withInput();
7674
}
75+
76+
/**
77+
* Log the exception to the logs using the defined error level only if the previous
78+
* exception is set.
79+
*
80+
* @return mixed
81+
*
82+
* @throws \Exception
83+
*/
84+
public function report()
85+
{
86+
if (! $this->getPrevious() instanceof Exception || ! Handler::isReportable($this->getPrevious())) {
87+
return null;
88+
}
89+
90+
try {
91+
$logger = Container::getInstance()->make(LoggerInterface::class);
92+
} catch (Exception $ex) {
93+
throw $this->getPrevious();
94+
}
95+
96+
return $logger->{$this->getErrorLevel()}($this->getPrevious());
97+
}
7798
}

app/Exceptions/Handler.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use PDOException;
77
use Psr\Log\LoggerInterface;
8+
use Illuminate\Container\Container;
89
use Illuminate\Auth\AuthenticationException;
910
use Illuminate\Session\TokenMismatchException;
1011
use Illuminate\Validation\ValidationException;
@@ -24,7 +25,6 @@ class Handler extends ExceptionHandler
2425
protected $dontReport = [
2526
AuthenticationException::class,
2627
AuthorizationException::class,
27-
DisplayException::class,
2828
HttpException::class,
2929
ModelNotFoundException::class,
3030
RecordNotFoundException::class,
@@ -201,6 +201,17 @@ public static function convertToArray(Exception $exception, array $override = []
201201
return ['errors' => [array_merge($error, $override)]];
202202
}
203203

204+
/**
205+
* Return an array of exceptions that should not be reported.
206+
*
207+
* @param \Exception $exception
208+
* @return bool
209+
*/
210+
public static function isReportable(Exception $exception): bool
211+
{
212+
return (new static(Container::getInstance()))->shouldReport($exception);
213+
}
214+
204215
/**
205216
* Convert an authentication exception into an unauthenticated response.
206217
*

0 commit comments

Comments
 (0)