2017-06-09 02:28:26 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Exceptions;
|
|
|
|
|
2019-11-19 23:06:07 +08:00
|
|
|
use App\Exceptions\Converters\GenericExceptionAbstract;
|
2019-08-21 20:17:44 +08:00
|
|
|
use App\Exceptions\Converters\SymfonyException;
|
|
|
|
use App\Exceptions\Converters\ValidationException;
|
2020-05-23 20:58:07 +08:00
|
|
|
use App\Http\Middleware\SetActiveTheme;
|
2017-06-09 02:28:26 +08:00
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2019-09-13 20:05:02 +08:00
|
|
|
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;
|
2019-08-21 20:17:44 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Session\TokenMismatchException;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Validation\ValidationException as IlluminateValidationException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException as SymfonyHttpException;
|
2018-02-23 00:14:12 +08:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2020-05-16 06:20:16 +08:00
|
|
|
use Throwable;
|
2019-09-13 20:05:02 +08:00
|
|
|
use Whoops\Handler\HandlerInterface;
|
2017-06-09 02:28:26 +08:00
|
|
|
|
2018-02-23 05:15:00 +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 = [
|
2020-02-09 02:29:34 +08:00
|
|
|
//AuthenticationException::class,
|
|
|
|
//AuthorizationException::class,
|
2019-11-19 23:06:07 +08:00
|
|
|
AbstractHttpException::class,
|
2019-08-21 20:17:44 +08:00
|
|
|
IlluminateValidationException::class,
|
|
|
|
ModelNotFoundException::class,
|
|
|
|
SymfonyHttpException::class,
|
|
|
|
TokenMismatchException::class,
|
2017-06-09 02:28:26 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
2020-05-16 06:23:16 +08:00
|
|
|
* @param Request $request
|
2020-05-16 06:20:16 +08:00
|
|
|
* @param \Throwable $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
|
|
|
*/
|
2020-05-16 06:20:16 +08:00
|
|
|
public function render($request, Throwable $exception)
|
2017-06-09 02:28:26 +08:00
|
|
|
{
|
2018-02-25 05:38:25 +08:00
|
|
|
if ($request->is('api/*')) {
|
2020-02-09 02:29:34 +08:00
|
|
|
return $this->handleApiError($request, $exception);
|
2018-02-23 00:14:12 +08:00
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:07 +08:00
|
|
|
(new SetActiveTheme())->setTheme($request);
|
|
|
|
if ($exception instanceof AbstractHttpException && $exception->getStatusCode() === 403) {
|
2018-02-23 00:14:12 +08:00
|
|
|
return redirect()->guest('login');
|
2017-12-30 23:03:45 +08:00
|
|
|
}
|
|
|
|
|
2017-06-09 02:28:26 +08:00
|
|
|
return parent::render($request, $exception);
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
/**
|
|
|
|
* Handle errors in the API
|
|
|
|
*
|
|
|
|
* @param $request
|
2020-05-16 06:20:16 +08:00
|
|
|
* @param \Throwable $exception
|
2020-02-09 02:29:34 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
|
|
|
|
*/
|
2020-05-16 06:20:16 +08:00
|
|
|
private function handleApiError($request, Throwable $exception)
|
2020-02-09 02:29:34 +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 || $exception instanceof NotFoundHttpException) {
|
|
|
|
$error = new AssetNotFound($exception);
|
|
|
|
|
|
|
|
return $error->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom exceptions should be extending HttpException
|
|
|
|
if ($exception instanceof SymfonyHttpException) {
|
|
|
|
$error = new SymfonyException($exception);
|
|
|
|
|
|
|
|
return $error->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the detailed errors from the validation errors
|
|
|
|
if ($exception instanceof IlluminateValidationException) {
|
|
|
|
$error = new ValidationException($exception);
|
|
|
|
|
|
|
|
return $error->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$error = new GenericExceptionAbstract($exception);
|
|
|
|
|
|
|
|
return $error->getResponse();
|
|
|
|
}
|
|
|
|
|
2017-06-09 02:28:26 +08:00
|
|
|
/**
|
|
|
|
* Convert an authentication exception into an unauthenticated response.
|
|
|
|
*
|
2019-08-21 20:17:44 +08:00
|
|
|
* @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)
|
|
|
|
{
|
2018-02-23 05:15:00 +08:00
|
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
2019-08-21 20:17:44 +08:00
|
|
|
$error = new Unauthenticated();
|
2020-05-23 20:58:07 +08:00
|
|
|
|
2019-08-21 20:17:44 +08:00
|
|
|
return $error->getResponse();
|
2017-06-09 02:28:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->guest('login');
|
|
|
|
}
|
2017-08-23 22:23:48 +08:00
|
|
|
|
2019-09-13 20:05:02 +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
|
|
|
}
|