Simplify transformer logic

This commit is contained in:
DaneEveritt 2022-05-22 16:23:22 -04:00
parent f7fc67344e
commit 33bafe9277
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 32 additions and 44 deletions

View file

@ -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();
}

View file

@ -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;
}
}