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;
|
2016-08-16 19:20:58 -04:00
|
|
|
|
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)
|
2016-08-16 19:20:58 -04:00
|
|
|
{
|
2017-12-17 14:57:05 -06:00
|
|
|
parent::__construct($message, $code, $previous);
|
2016-08-16 19:20:58 -04:00
|
|
|
}
|
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;
|
|
|
|
|
}
|
2017-12-17 14:57:05 -06:00
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
public function getStatusCode(): int
|
2018-01-25 22:34:53 -06:00
|
|
|
{
|
|
|
|
|
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 [];
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-17 14:57:05 -06:00
|
|
|
/**
|
|
|
|
|
* 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
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
2026-10-06 02:10:17 +01:00
|
|
|
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();
|
2017-12-17 14:57:05 -06:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return back()->withInput();
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
2018-03-10 13:02:41 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2018-03-10 13:02:41 -06:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$previous = $this->getPrevious();
|
|
|
|
|
if (! $previous instanceof Throwable || ! Exceptions::shouldReport($previous)) {
|
|
|
|
|
return;
|
2018-03-10 13:02:41 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$context = ['exception' => $previous];
|
2018-03-10 13:02:41 -06:00
|
|
|
|
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),
|
|
|
|
|
};
|
2018-03-10 13:02:41 -06:00
|
|
|
}
|
2015-12-06 13:58:49 -05:00
|
|
|
}
|