Fix display exception handling

This commit is contained in:
DaneEveritt 2022-05-22 18:21:38 -04:00
parent dca53611ff
commit 3f99b00cf7
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 24 additions and 2 deletions

View file

@ -8,8 +8,9 @@ use Psr\Log\LoggerInterface;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Container\Container; use Illuminate\Container\Container;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
class DisplayException extends PterodactylException class DisplayException extends PterodactylException implements HttpExceptionInterface
{ {
public const LEVEL_DEBUG = 'debug'; public const LEVEL_DEBUG = 'debug';
public const LEVEL_INFO = 'info'; public const LEVEL_INFO = 'info';
@ -51,6 +52,14 @@ class DisplayException extends PterodactylException
return Response::HTTP_BAD_REQUEST; return Response::HTTP_BAD_REQUEST;
} }
/**
* @return array
*/
public function getHeaders()
{
return [];
}
/** /**
* Render the exception to the user by adding a flashed message to the session * 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 * and then redirecting them back to the page that they came from. If the
@ -58,10 +67,14 @@ class DisplayException extends PterodactylException
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* *
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
*/ */
public function render($request) public function render($request)
{ {
if ($request->expectsJson()) {
return response()->json(Handler::toArray($this), $this->getStatusCode(), $this->getHeaders());
}
app(AlertsMessageBag::class)->danger($this->getMessage())->flash(); app(AlertsMessageBag::class)->danger($this->getMessage())->flash();
return redirect()->back()->withInput(); return redirect()->back()->withInput();

View file

@ -284,4 +284,13 @@ class Handler extends ExceptionHandler
return $previous; return $previous;
} }
/**
* Helper method to allow reaching into the handler to convert an exception
* into the expected array response type.
*/
public static function toArray(Throwable $e): array
{
return (new self(app()))->convertExceptionToArray($e);
}
} }