Files
pterodactyl-panel/app/Exceptions/Handler.php
T

108 lines
3.6 KiB
PHP
Raw Normal View History

2015-12-06 13:58:49 -05:00
<?php
namespace Pterodactyl\Exceptions;
use Exception;
2017-07-22 20:15:01 -05:00
use Prologue\Alerts\Facades\Alert;
2017-08-05 17:26:30 -05:00
use Illuminate\Auth\AuthenticationException;
2017-08-12 15:29:01 -05:00
use Illuminate\Session\TokenMismatchException;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
2017-07-22 20:15:01 -05:00
use Pterodactyl\Exceptions\Model\DataValidationException;
2017-08-12 15:29:01 -05:00
use Symfony\Component\HttpKernel\Exception\HttpException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
2017-08-05 17:26:30 -05:00
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
2015-12-06 13:58:49 -05:00
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
2017-08-12 15:29:01 -05:00
AuthenticationException::class,
AuthorizationException::class,
2017-07-22 20:15:01 -05:00
DisplayException::class,
DataValidationException::class,
2017-08-12 15:29:01 -05:00
DisplayValidationException::class,
HttpException::class,
ModelNotFoundException::class,
RecordNotFoundException::class,
2017-08-12 15:29:01 -05:00
TokenMismatchException::class,
ValidationException::class,
2015-12-06 13:58:49 -05:00
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
2017-08-21 22:10:48 -05:00
* @param \Exception $exception
*
* @throws \Exception
2015-12-06 13:58:49 -05:00
*/
2016-09-03 17:09:00 -04:00
public function report(Exception $exception)
2015-12-06 13:58:49 -05:00
{
parent::report($exception);
2015-12-06 13:58:49 -05:00
}
/**
* Render an exception into an HTTP response.
*
2017-08-21 22:10:48 -05:00
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
*
* @throws \Exception
2015-12-06 13:58:49 -05:00
*/
public function render($request, Exception $exception)
2015-12-06 13:58:49 -05:00
{
if ($request->expectsJson() || $request->isJson() || $request->is(...config('pterodactyl.json_routes'))) {
$exception = $this->prepareException($exception);
2017-04-02 00:11:52 -04:00
2017-07-22 20:15:01 -05:00
if (config('app.debug') || $this->isHttpException($exception) || $exception instanceof DisplayException) {
2017-05-22 19:25:26 -05:00
$displayError = $exception->getMessage();
} else {
$displayError = 'An unhandled exception was encountered with this request.';
2017-04-02 00:11:52 -04:00
}
$response = response()->json(
[
'error' => $displayError,
'http_code' => (method_exists($exception, 'getStatusCode')) ? $exception->getStatusCode() : 500,
'trace' => (! config('app.debug')) ? null : $exception->getTrace(),
],
$this->isHttpException($exception) ? $exception->getStatusCode() : 500,
$this->isHttpException($exception) ? $exception->getHeaders() : [],
JSON_UNESCAPED_SLASHES
);
2015-12-06 13:58:49 -05:00
2017-01-12 13:44:23 -05:00
parent::report($exception);
2017-07-22 20:15:01 -05:00
} elseif ($exception instanceof DisplayException) {
Alert::danger($exception->getMessage())->flash();
return redirect()->back()->withInput();
2015-12-06 13:58:49 -05:00
}
2016-09-07 18:25:11 -04:00
return (isset($response)) ? $response : parent::render($request, $exception);
2015-12-06 13:58:49 -05:00
}
2016-09-03 17:09:00 -04:00
/**
* Convert an authentication exception into an unauthenticated response.
*
2017-08-21 22:10:48 -05:00
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
2016-09-03 17:09:00 -04:00
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
2016-12-07 22:46:38 +00:00
2017-04-01 17:59:43 -04:00
return redirect()->guest(route('auth.login'));
2016-09-03 17:09:00 -04:00
}
2015-12-06 13:58:49 -05:00
}