From 33bafe9277dfea8684db5848b15fa8607a452ace Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sun, 22 May 2022 16:23:22 -0400 Subject: [PATCH] Simplify transformer logic --- app/Exceptions/DisplayException.php | 10 ++---- app/Exceptions/Handler.php | 34 ++++++++++++++----- .../Api/Client/ClientApiController.php | 10 ++---- .../Api/Client/Servers/FileController.php | 2 +- .../Client}/FileObjectTransformer.php | 11 ++---- .../Daemon/BaseDaemonTransformer.php | 9 ----- 6 files changed, 32 insertions(+), 44 deletions(-) rename app/Transformers/{Daemon => Api/Client}/FileObjectTransformer.php (80%) delete mode 100644 app/Transformers/Daemon/BaseDaemonTransformer.php diff --git a/app/Exceptions/DisplayException.php b/app/Exceptions/DisplayException.php index 063340a72..636b9dfc9 100644 --- a/app/Exceptions/DisplayException.php +++ b/app/Exceptions/DisplayException.php @@ -58,17 +58,11 @@ class DisplayException extends PterodactylException * * @param \Illuminate\Http\Request $request * - * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse + * @return \Illuminate\Http\RedirectResponse */ public function render($request) { - if ($request->expectsJson()) { - return response()->json(Handler::convertToArray($this, [ - 'detail' => $this->getMessage(), - ]), method_exists($this, 'getStatusCode') ? $this->getStatusCode() : Response::HTTP_BAD_REQUEST); - } - - Container::getInstance()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash(); + app(AlertsMessageBag::class)->danger($this->getMessage())->flash(); return redirect()->back()->withInput(); } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 55c1d8831..6ec987c99 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -197,7 +197,7 @@ class Handler extends ExceptionHandler /** * Return the exception as a JSONAPI representation for use on API requests. */ - public static function convertToArray(Throwable $exception, array $override = []): array + protected function convertExceptionToArray(Throwable $exception, array $override = []): array { $match = self::$exceptionResponseCodes[get_class($exception)] ?? null; @@ -226,7 +226,13 @@ class Handler extends ExceptionHandler 'file' => str_replace(Application::getInstance()->basePath(), '', $exception->getFile()), ], 'meta' => [ - 'trace' => explode("\n", $exception->getTraceAsString()), + 'trace' => Collection::make($exception->getTrace()) + ->map(fn ($trace) => Arr::except($trace, ['args'])) + ->all(), + 'previous' => Collection::make($this->extractPrevious($exception)) + ->map(fn ($exception) => $exception->getTrace()) + ->map(fn ($trace) => Arr::except($trace, ['args'])) + ->all(), ], ]); } @@ -252,20 +258,30 @@ class Handler extends ExceptionHandler protected function unauthenticated($request, AuthenticationException $exception) { if ($request->expectsJson()) { - return new JsonResponse(self::convertToArray($exception), JsonResponse::HTTP_UNAUTHORIZED); + return new JsonResponse($this->convertExceptionToArray($exception), JsonResponse::HTTP_UNAUTHORIZED); } - return $this->container->make('redirect')->guest('/auth/login'); + return redirect()->guest('/auth/login'); } /** - * Converts an exception into an array to render in the response. Overrides - * Laravel's built-in converter to output as a JSONAPI spec compliant object. + * Extracts all of the previous exceptions that lead to the one passed into this + * function being thrown. * - * @return array + * @param \Throwable $e + * @return \Throwable[] */ - protected function convertExceptionToArray(Throwable $exception) + protected function extractPrevious(Throwable $e): array { - return self::convertToArray($exception); + $previous = []; + while ($value = $e->getPrevious()) { + if (!$value instanceof Throwable) { + break; + } + $previous[] = $value; + $e = $value; + } + + return $previous; } } diff --git a/app/Http/Controllers/Api/Client/ClientApiController.php b/app/Http/Controllers/Api/Client/ClientApiController.php index 210489dea..56e6a1714 100644 --- a/app/Http/Controllers/Api/Client/ClientApiController.php +++ b/app/Http/Controllers/Api/Client/ClientApiController.php @@ -3,7 +3,6 @@ namespace Pterodactyl\Http\Controllers\Api\Client; use Webmozart\Assert\Assert; -use Pterodactyl\Transformers\Daemon\BaseDaemonTransformer; use Pterodactyl\Transformers\Api\Client\BaseClientTransformer; use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController; @@ -55,13 +54,8 @@ abstract class ClientApiController extends ApplicationApiController */ public function getTransformer(string $abstract) { - Assert::methodExists($abstract, 'fromRequest'); + Assert::subclassOf($abstract, BaseClientTransformer::class); - /** @var T $transformer */ - $transformer = $abstract::fromRequest($this->request); - - Assert::isInstanceOfAny($transformer, [BaseClientTransformer::class, BaseDaemonTransformer::class]); - - return $transformer; + return $abstract::fromRequest($this->request); } } diff --git a/app/Http/Controllers/Api/Client/Servers/FileController.php b/app/Http/Controllers/Api/Client/Servers/FileController.php index 9ff503fd7..5a106e037 100644 --- a/app/Http/Controllers/Api/Client/Servers/FileController.php +++ b/app/Http/Controllers/Api/Client/Servers/FileController.php @@ -10,7 +10,7 @@ use Illuminate\Http\JsonResponse; use Pterodactyl\Services\Nodes\NodeJWTService; use Illuminate\Contracts\Routing\ResponseFactory; use Pterodactyl\Repositories\Wings\DaemonFileRepository; -use Pterodactyl\Transformers\Daemon\FileObjectTransformer; +use Pterodactyl\Transformers\Api\Client\FileObjectTransformer; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest; use Pterodactyl\Http\Requests\Api\Client\Servers\Files\PullFileRequest; diff --git a/app/Transformers/Daemon/FileObjectTransformer.php b/app/Transformers/Api/Client/FileObjectTransformer.php similarity index 80% rename from app/Transformers/Daemon/FileObjectTransformer.php rename to app/Transformers/Api/Client/FileObjectTransformer.php index 1ad86dce8..634a7d46e 100644 --- a/app/Transformers/Daemon/FileObjectTransformer.php +++ b/app/Transformers/Api/Client/FileObjectTransformer.php @@ -1,19 +1,12 @@