phpvms/app/Exceptions/Handler.php

149 lines
4.6 KiB
PHP
Raw Normal View History

2017-06-09 02:28:26 +08:00
<?php
namespace App\Exceptions;
use App\Exceptions\Converters\GenericExceptionAbstract;
use App\Exceptions\Converters\SymfonyException;
use App\Exceptions\Converters\ValidationException;
2017-06-09 02:28:26 +08:00
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
2017-06-09 02:28:26 +08:00
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Container\BindingResolutionException;
2018-02-21 12:33:09 +08:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2017-06-09 02:28:26 +08:00
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException as IlluminateValidationException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException as SymfonyHttpException;
2019-05-12 23:10:31 +08:00
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2018-02-23 00:14:12 +08:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Whoops\Handler\HandlerInterface;
2017-06-09 02:28:26 +08:00
/**
* Class Handler
*/
2017-06-09 02:28:26 +08:00
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*/
protected $dontReport = [
AuthenticationException::class,
AuthorizationException::class,
AbstractHttpException::class,
IlluminateValidationException::class,
ModelNotFoundException::class,
SymfonyHttpException::class,
TokenMismatchException::class,
2017-06-09 02:28:26 +08:00
];
/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param Exception $exception
2018-08-27 00:40:04 +08:00
*
2018-02-23 00:14:12 +08:00
* @return mixed
2017-06-09 02:28:26 +08:00
*/
public function render($request, Exception $exception)
{
if ($request->is('api/*')) {
2018-02-23 00:14:12 +08:00
Log::error('API Error', $exception->getTrace());
if ($exception instanceof AbstractHttpException) {
return $exception->getResponse();
}
/*
* Not of the HttpException abstract class. Map these into
*/
if ($exception instanceof ModelNotFoundException ||
2018-02-23 00:14:12 +08:00
$exception instanceof NotFoundHttpException) {
$error = new AssetNotFound($exception);
return $error->getResponse();
}
2018-08-27 00:40:04 +08:00
// Custom exceptions should be extending HttpException
if ($exception instanceof SymfonyHttpException) {
$error = new SymfonyException($exception);
return $error->getResponse();
2018-02-23 00:14:12 +08:00
}
2018-08-27 00:40:04 +08:00
// Create the detailed errors from the validation errors
if ($exception instanceof IlluminateValidationException) {
$error = new ValidationException($exception);
return $error->getResponse();
}
2018-01-25 04:14:47 +08:00
$error = new GenericExceptionAbstract($exception);
return $error->getResponse();
2018-02-23 00:14:12 +08:00
}
if ($exception instanceof AbstractHttpException
2018-02-23 00:14:12 +08:00
&& $exception->getStatusCode() === 403) {
return redirect()->guest('login');
}
2017-06-09 02:28:26 +08:00
return parent::render($request, $exception);
}
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param Request $request
* @param AuthenticationException $exception
2018-08-27 00:40:04 +08:00
*
2017-06-09 02:28:26 +08:00
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson() || $request->is('api/*')) {
$error = new Unauthenticated();
return $error->getResponse();
2017-06-09 02:28:26 +08:00
}
return redirect()->guest('login');
}
2017-08-23 22:23:48 +08:00
/**
* Render the given HttpException.
2018-08-27 00:40:04 +08:00
*
* @param AbstractHttpException $e
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Http\Response|Response
2017-08-23 22:23:48 +08:00
*/
2019-05-12 23:10:31 +08:00
protected function renderHttpException(HttpExceptionInterface $e)
2017-08-23 22:23:48 +08:00
{
$status = $e->getStatusCode();
view()->replaceNamespace('errors', [
resource_path('views/layouts/'.setting('general.theme', 'default').'/errors'),
2017-08-23 22:23:48 +08:00
resource_path('views/errors'),
__DIR__.'/views',
2017-08-23 22:23:48 +08:00
]);
if (view()->exists("errors::{$status}")) {
return response()->view("errors::{$status}", [
'exception' => $e,
'SKIN_NAME' => setting('general.theme', 'default'),
], $status, $e->getHeaders());
2017-08-23 22:23:48 +08:00
}
2018-08-20 22:20:03 +08:00
return $this->convertExceptionToResponse($e);
2017-08-23 22:23:48 +08:00
}
/**
* Ignition error page integration
*/
protected function whoopsHandler()
{
try {
return app(HandlerInterface::class);
} catch (BindingResolutionException $e) {
return parent::whoopsHandler();
}
}
2017-06-09 02:28:26 +08:00
}