Skip to content

Commit 3f99b00

Browse files
committed
Fix display exception handling
1 parent dca5361 commit 3f99b00

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

app/Exceptions/DisplayException.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use Illuminate\Http\Response;
99
use Illuminate\Container\Container;
1010
use Prologue\Alerts\AlertsMessageBag;
11+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
1112

12-
class DisplayException extends PterodactylException
13+
class DisplayException extends PterodactylException implements HttpExceptionInterface
1314
{
1415
public const LEVEL_DEBUG = 'debug';
1516
public const LEVEL_INFO = 'info';
@@ -51,17 +52,29 @@ public function getStatusCode()
5152
return Response::HTTP_BAD_REQUEST;
5253
}
5354

55+
/**
56+
* @return array
57+
*/
58+
public function getHeaders()
59+
{
60+
return [];
61+
}
62+
5463
/**
5564
* Render the exception to the user by adding a flashed message to the session
5665
* and then redirecting them back to the page that they came from. If the
5766
* request originated from an API hit, return the error in JSONAPI spec format.
5867
*
5968
* @param \Illuminate\Http\Request $request
6069
*
61-
* @return \Illuminate\Http\RedirectResponse
70+
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
6271
*/
6372
public function render($request)
6473
{
74+
if ($request->expectsJson()) {
75+
return response()->json(Handler::toArray($this), $this->getStatusCode(), $this->getHeaders());
76+
}
77+
6578
app(AlertsMessageBag::class)->danger($this->getMessage())->flash();
6679

6780
return redirect()->back()->withInput();

app/Exceptions/Handler.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,13 @@ protected function extractPrevious(Throwable $e): array
284284

285285
return $previous;
286286
}
287+
288+
/**
289+
* Helper method to allow reaching into the handler to convert an exception
290+
* into the expected array response type.
291+
*/
292+
public static function toArray(Throwable $e): array
293+
{
294+
return (new self(app()))->convertExceptionToArray($e);
295+
}
287296
}

0 commit comments

Comments
 (0)