Files
pterodactyl-panel/app/Exceptions/DisplayException.php
T

90 lines
2.6 KiB
PHP
Raw Normal View History

2015-12-06 13:58:49 -05:00
<?php
2016-12-07 22:46:38 +00:00
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2015-12-06 13:58:49 -05:00
namespace Pterodactyl\Exceptions;
2022-10-14 10:59:20 -06:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Exceptions;
use Illuminate\Support\Facades\Log;
use Pterodactyl\Facades\Alert;
2022-05-22 18:21:38 -04:00
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2026-10-05 18:10:18 -04:00
use Throwable;
2022-05-22 18:21:38 -04:00
class DisplayException extends PterodactylException implements HttpExceptionInterface
2015-12-06 13:58:49 -05:00
{
2026-10-05 18:10:18 -04:00
public const string LEVEL_DEBUG = 'debug';
public const string LEVEL_INFO = 'info';
public const string LEVEL_WARNING = 'warning';
public const string LEVEL_ERROR = 'error';
2017-10-06 00:16:22 -05:00
2017-09-26 22:54:34 -05:00
/**
2022-10-14 10:59:20 -06:00
* DisplayException constructor.
2017-09-26 22:54:34 -05:00
*/
2026-10-05 18:10:18 -04:00
public function __construct(string $message, ?Throwable $previous = null, protected string $level = self::LEVEL_ERROR, int $code = 0)
{
parent::__construct($message, $code, $previous);
}
2017-09-26 22:54:34 -05:00
2022-10-14 10:59:20 -06:00
public function getErrorLevel(): string
2017-09-26 22:54:34 -05:00
{
return $this->level;
}
2022-10-14 10:59:20 -06:00
public function getStatusCode(): int
{
return Response::HTTP_BAD_REQUEST;
}
2026-10-05 18:10:18 -04:00
/**
* @return array<string, string>
*/
2022-10-14 10:59:20 -06:00
public function getHeaders(): array
2022-05-22 18:21:38 -04:00
{
return [];
}
/**
* Render the exception to the user by adding a flashed message to the session
* and then redirecting them back to the page that they came from. If the
* request originated from an API hit, return the error in JSONAPI spec format.
*/
2022-10-14 10:59:20 -06:00
public function render(Request $request): JsonResponse|RedirectResponse
{
if ($request->expectsJson() || $request->is('api/*')) {
2026-10-05 18:10:18 -04:00
return response()->json(ApiErrorResponse::toArray($this), $this->getStatusCode(), $this->getHeaders());
2022-05-22 18:21:38 -04:00
}
2026-10-05 18:10:18 -04:00
Alert::danger($this->getMessage())->flash();
2026-10-05 18:10:18 -04:00
return back()->withInput();
}
/**
* Log the exception to the logs using the defined error level only if the previous
* exception is set.
*/
2026-10-05 18:10:18 -04:00
public function report(): void
{
2026-10-05 18:10:18 -04:00
$previous = $this->getPrevious();
if (! $previous instanceof Throwable || ! Exceptions::shouldReport($previous)) {
return;
}
2026-10-05 18:10:18 -04:00
$context = ['exception' => $previous];
2026-10-05 18:10:18 -04:00
match ($this->getErrorLevel()) {
self::LEVEL_DEBUG => Log::debug($previous->getMessage(), $context),
self::LEVEL_INFO => Log::info($previous->getMessage(), $context),
self::LEVEL_WARNING => Log::warning($previous->getMessage(), $context),
default => Log::error($previous->getMessage(), $context),
};
}
2015-12-06 13:58:49 -05:00
}