Path: blob/1.0-develop/app/Exceptions/DisplayException.php
7432 views
<?php12namespace Pterodactyl\Exceptions;34use Exception;5use Illuminate\Http\Request;6use Psr\Log\LoggerInterface;7use Illuminate\Http\Response;8use Illuminate\Http\JsonResponse;9use Illuminate\Container\Container;10use Illuminate\Http\RedirectResponse;11use Prologue\Alerts\AlertsMessageBag;12use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;1314class DisplayException extends PterodactylException implements HttpExceptionInterface15{16public const LEVEL_DEBUG = 'debug';17public const LEVEL_INFO = 'info';18public const LEVEL_WARNING = 'warning';19public const LEVEL_ERROR = 'error';2021/**22* DisplayException constructor.23*/24public function __construct(string $message, ?\Throwable $previous = null, protected string $level = self::LEVEL_ERROR, int $code = 0)25{26parent::__construct($message, $code, $previous);27}2829public function getErrorLevel(): string30{31return $this->level;32}3334public function getStatusCode(): int35{36return Response::HTTP_BAD_REQUEST;37}3839public function getHeaders(): array40{41return [];42}4344/**45* Render the exception to the user by adding a flashed message to the session46* and then redirecting them back to the page that they came from. If the47* request originated from an API hit, return the error in JSONAPI spec format.48*/49public function render(Request $request): JsonResponse|RedirectResponse50{51if ($request->expectsJson()) {52return response()->json(Handler::toArray($this), $this->getStatusCode(), $this->getHeaders());53}5455app(AlertsMessageBag::class)->danger($this->getMessage())->flash();5657return redirect()->back()->withInput();58}5960/**61* Log the exception to the logs using the defined error level only if the previous62* exception is set.63*64* @throws \Throwable65*/66public function report()67{68if (!$this->getPrevious() instanceof \Exception || !Handler::isReportable($this->getPrevious())) {69return null;70}7172try {73$logger = Container::getInstance()->make(LoggerInterface::class);74} catch (\Exception) {75throw $this->getPrevious();76}7778return $logger->{$this->getErrorLevel()}($this->getPrevious());79}80}818283