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