2015-12-06 19:58:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Exceptions;
|
|
|
|
|
|
|
|
use Exception;
|
2017-08-06 00:26:30 +02:00
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2017-08-12 22:29:01 +02:00
|
|
|
use Illuminate\Session\TokenMismatchException;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2017-09-24 19:32:29 +02:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2017-08-06 00:26:30 +02:00
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
2015-12-06 19:58:49 +01:00
|
|
|
|
|
|
|
class Handler extends ExceptionHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A list of the exception types that should not be reported.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dontReport = [
|
2017-08-12 22:29:01 +02:00
|
|
|
AuthenticationException::class,
|
|
|
|
AuthorizationException::class,
|
2017-07-23 03:15:01 +02:00
|
|
|
DisplayException::class,
|
2017-08-12 22:29:01 +02:00
|
|
|
HttpException::class,
|
|
|
|
ModelNotFoundException::class,
|
2017-09-24 19:32:29 +02:00
|
|
|
RecordNotFoundException::class,
|
2017-08-12 22:29:01 +02:00
|
|
|
TokenMismatchException::class,
|
|
|
|
ValidationException::class,
|
2015-12-06 19:58:49 +01:00
|
|
|
];
|
|
|
|
|
2017-12-17 21:57:05 +01:00
|
|
|
/**
|
|
|
|
* A list of the inputs that are never flashed for validation exceptions.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dontFlash = [
|
|
|
|
'password',
|
|
|
|
'password_confirmation',
|
|
|
|
];
|
|
|
|
|
2015-12-06 19:58:49 +01:00
|
|
|
/**
|
|
|
|
* Report or log an exception.
|
|
|
|
*
|
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
|
|
|
|
*
|
2017-08-22 05:10:48 +02:00
|
|
|
* @param \Exception $exception
|
2017-07-22 04:17:42 +02:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
2015-12-06 19:58:49 +01:00
|
|
|
*/
|
2016-09-03 23:09:00 +02:00
|
|
|
public function report(Exception $exception)
|
2015-12-06 19:58:49 +01:00
|
|
|
{
|
2017-07-22 04:17:42 +02:00
|
|
|
parent::report($exception);
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
2017-08-22 05:10:48 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Exception $exception
|
2017-12-17 21:57:05 +01:00
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
2017-07-22 04:17:42 +02:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
2015-12-06 19:58:49 +01:00
|
|
|
*/
|
2016-09-07 22:12:06 +02:00
|
|
|
public function render($request, Exception $exception)
|
2015-12-06 19:58:49 +01:00
|
|
|
{
|
2017-12-17 21:57:05 +01:00
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
2017-04-02 06:11:52 +02:00
|
|
|
|
2017-12-17 21:57:05 +01:00
|
|
|
/**
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Illuminate\Validation\ValidationException $exception
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function invalidJson($request, ValidationException $exception)
|
|
|
|
{
|
|
|
|
$codes = collect($exception->validator->failed())->mapWithKeys(function ($reasons, $field) {
|
|
|
|
$cleaned = [];
|
|
|
|
foreach ($reasons as $reason => $attrs) {
|
|
|
|
$cleaned[] = snake_case($reason);
|
2017-04-02 06:11:52 +02:00
|
|
|
}
|
|
|
|
|
2017-12-17 21:57:05 +01:00
|
|
|
return [$field => $cleaned];
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
$errors = collect($exception->errors())->map(function ($errors, $field) use ($codes) {
|
|
|
|
$response = [];
|
|
|
|
foreach ($errors as $key => $error) {
|
|
|
|
$response[] = [
|
|
|
|
'code' => array_get($codes, $field . '.' . $key),
|
|
|
|
'detail' => $error,
|
|
|
|
'source' => ['field' => $field],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
})->flatMap(function ($errors) {
|
|
|
|
return $errors;
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
return response()->json(['errors' => $errors], $exception->status);
|
|
|
|
}
|
2015-12-06 19:58:49 +01:00
|
|
|
|
2017-12-17 21:57:05 +01:00
|
|
|
/**
|
|
|
|
* Return the exception as a JSONAPI representation for use on API requests.
|
|
|
|
*
|
|
|
|
* @param \Exception $exception
|
|
|
|
* @param array $override
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function convertToArray(Exception $exception, array $override = []): array
|
|
|
|
{
|
|
|
|
$error = [
|
|
|
|
'code' => class_basename($exception),
|
|
|
|
'status' => method_exists($exception, 'getStatusCode') ? strval($exception->getStatusCode()) : '500',
|
|
|
|
'detail' => 'An error was encountered while processing this request.',
|
|
|
|
];
|
2017-07-23 03:15:01 +02:00
|
|
|
|
2017-12-17 21:57:05 +01:00
|
|
|
if (config('app.debug')) {
|
|
|
|
$error = array_merge($error, [
|
|
|
|
'detail' => $exception->getMessage(),
|
|
|
|
'source' => [
|
|
|
|
'line' => $exception->getLine(),
|
|
|
|
'file' => str_replace(base_path(), '', $exception->getFile()),
|
|
|
|
],
|
|
|
|
'meta' => [
|
|
|
|
'trace' => explode("\n", $exception->getTraceAsString()),
|
|
|
|
],
|
|
|
|
]);
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 21:57:05 +01:00
|
|
|
return ['errors' => [array_merge($error, $override)]];
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|
2016-09-03 23:09:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an authentication exception into an unauthenticated response.
|
|
|
|
*
|
2017-08-22 05:10:48 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Illuminate\Auth\AuthenticationException $exception
|
2016-09-03 23:09:00 +02:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
protected function unauthenticated($request, AuthenticationException $exception)
|
|
|
|
{
|
|
|
|
if ($request->expectsJson()) {
|
|
|
|
return response()->json(['error' => 'Unauthenticated.'], 401);
|
|
|
|
}
|
2016-12-07 23:46:38 +01:00
|
|
|
|
2017-04-01 23:59:43 +02:00
|
|
|
return redirect()->guest(route('auth.login'));
|
2016-09-03 23:09:00 +02:00
|
|
|
}
|
2017-12-17 21:57:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param \Exception $exception
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function convertExceptionToArray(Exception $exception)
|
|
|
|
{
|
|
|
|
return self::convertToArray($exception);
|
|
|
|
}
|
2015-12-06 19:58:49 +01:00
|
|
|
}
|