Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Exceptions/DisplayException.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Exceptions;
4
5
use Exception;
6
use Illuminate\Http\Request;
7
use Psr\Log\LoggerInterface;
8
use Illuminate\Http\Response;
9
use Illuminate\Http\JsonResponse;
10
use Illuminate\Container\Container;
11
use Illuminate\Http\RedirectResponse;
12
use Prologue\Alerts\AlertsMessageBag;
13
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
14
15
class DisplayException extends PterodactylException implements HttpExceptionInterface
16
{
17
public const LEVEL_DEBUG = 'debug';
18
public const LEVEL_INFO = 'info';
19
public const LEVEL_WARNING = 'warning';
20
public const LEVEL_ERROR = 'error';
21
22
/**
23
* DisplayException constructor.
24
*/
25
public function __construct(string $message, ?\Throwable $previous = null, protected string $level = self::LEVEL_ERROR, int $code = 0)
26
{
27
parent::__construct($message, $code, $previous);
28
}
29
30
public function getErrorLevel(): string
31
{
32
return $this->level;
33
}
34
35
public function getStatusCode(): int
36
{
37
return Response::HTTP_BAD_REQUEST;
38
}
39
40
public function getHeaders(): array
41
{
42
return [];
43
}
44
45
/**
46
* Render the exception to the user by adding a flashed message to the session
47
* and then redirecting them back to the page that they came from. If the
48
* request originated from an API hit, return the error in JSONAPI spec format.
49
*/
50
public function render(Request $request): JsonResponse|RedirectResponse
51
{
52
if ($request->expectsJson()) {
53
return response()->json(Handler::toArray($this), $this->getStatusCode(), $this->getHeaders());
54
}
55
56
app(AlertsMessageBag::class)->danger($this->getMessage())->flash();
57
58
return redirect()->back()->withInput();
59
}
60
61
/**
62
* Log the exception to the logs using the defined error level only if the previous
63
* exception is set.
64
*
65
* @throws \Throwable
66
*/
67
public function report()
68
{
69
if (!$this->getPrevious() instanceof \Exception || !Handler::isReportable($this->getPrevious())) {
70
return null;
71
}
72
73
try {
74
$logger = Container::getInstance()->make(LoggerInterface::class);
75
} catch (\Exception) {
76
throw $this->getPrevious();
77
}
78
79
return $logger->{$this->getErrorLevel()}($this->getPrevious());
80
}
81
}
82
83