2015-12-06 18:58:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Exceptions;
|
|
|
|
|
|
|
|
use Exception;
|
2018-02-04 19:31:39 +00:00
|
|
|
use PDOException;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2017-08-12 20:29:01 +00: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 17:32:29 +00:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
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 = [
|
2017-08-12 20:29:01 +00:00
|
|
|
AuthenticationException::class,
|
|
|
|
AuthorizationException::class,
|
2017-07-23 01:15:01 +00:00
|
|
|
DisplayException::class,
|
2017-08-12 20:29:01 +00:00
|
|
|
HttpException::class,
|
|
|
|
ModelNotFoundException::class,
|
2017-09-24 17:32:29 +00:00
|
|
|
RecordNotFoundException::class,
|
2017-08-12 20:29:01 +00:00
|
|
|
TokenMismatchException::class,
|
|
|
|
ValidationException::class,
|
2015-12-06 18:58:49 +00:00
|
|
|
];
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
|
|
|
* A list of the inputs that are never flashed for validation exceptions.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dontFlash = [
|
2018-01-21 22:02:03 +00:00
|
|
|
'token',
|
|
|
|
'secret',
|
2017-12-17 20:57:05 +00:00
|
|
|
'password',
|
|
|
|
'password_confirmation',
|
|
|
|
];
|
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
/**
|
2018-02-04 19:31:39 +00:00
|
|
|
* Report or log an exception. Skips Laravel's internal reporter since we
|
|
|
|
* don't need or want the user information in our logs by default.
|
2015-12-06 18:58:49 +00:00
|
|
|
*
|
2018-02-04 19:31:39 +00:00
|
|
|
* If you want to implement logging in a different format to integrate with
|
|
|
|
* services such as AWS Cloudwatch or other monitoring you can replace the
|
|
|
|
* contents of this function with a call to the parent reporter.
|
2015-12-06 18:58:49 +00:00
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Exception $exception
|
2018-02-04 19:31:39 +00:00
|
|
|
* @return mixed
|
2017-07-22 02:17:42 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
{
|
2018-02-04 19:31:39 +00:00
|
|
|
if (! config('app.exceptions.report_all', false) && $this->shouldntReport($exception)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method_exists($exception, 'report')) {
|
|
|
|
return $exception->report();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$logger = $this->container->make(LoggerInterface::class);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $logger->error($exception instanceof PDOException ? $exception->getMessage() : $exception);
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Exception $exception
|
2017-12-17 20:57:05 +00:00
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
2017-07-22 02:17:42 +00:00
|
|
|
*
|
|
|
|
* @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-12-17 20:57:05 +00:00
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
2017-04-02 04:11:52 +00:00
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
2018-02-04 19:31:39 +00:00
|
|
|
* Transform a validation exception into a consistent format to be returned for
|
|
|
|
* calls to the API.
|
|
|
|
*
|
2017-12-17 20:57:05 +00: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 04:11:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 19:26:43 +00:00
|
|
|
return [str_replace('.', '_', $field) => $cleaned];
|
2017-12-17 20:57:05 +00:00
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
$errors = collect($exception->errors())->map(function ($errors, $field) use ($codes) {
|
|
|
|
$response = [];
|
|
|
|
foreach ($errors as $key => $error) {
|
|
|
|
$response[] = [
|
2018-01-27 19:26:43 +00:00
|
|
|
'code' => array_get($codes, str_replace('.', '_', $field) . '.' . $key),
|
2017-12-17 20:57:05 +00:00
|
|
|
'detail' => $error,
|
|
|
|
'source' => ['field' => $field],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
})->flatMap(function ($errors) {
|
|
|
|
return $errors;
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
return response()->json(['errors' => $errors], $exception->status);
|
|
|
|
}
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2017-12-17 20:57:05 +00: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 01:15:01 +00:00
|
|
|
|
2017-12-17 20:57:05 +00: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 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
return ['errors' => [array_merge($error, $override)]];
|
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-08-22 03:10:48 +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
|
|
|
}
|
2017-12-17 20:57:05 +00: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 18:58:49 +00:00
|
|
|
}
|