mirror of
https://github.com/flarum/framework.git
synced 2024-12-04 00:03:37 +08:00
Catch Throwables so that we handle internal PHP errors too
This commit is contained in:
parent
91e8d6820a
commit
2df5be7bcb
|
@ -12,6 +12,7 @@
|
||||||
namespace Flarum\Api;
|
namespace Flarum\Api;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Throwable;
|
||||||
use Tobscure\JsonApi\Document;
|
use Tobscure\JsonApi\Document;
|
||||||
use Tobscure\JsonApi\ErrorHandler as JsonApiErrorHandler;
|
use Tobscure\JsonApi\ErrorHandler as JsonApiErrorHandler;
|
||||||
|
|
||||||
|
@ -34,8 +35,12 @@ class ErrorHandler
|
||||||
* @param Exception $e
|
* @param Exception $e
|
||||||
* @return JsonApiResponse
|
* @return JsonApiResponse
|
||||||
*/
|
*/
|
||||||
public function handle(Exception $e)
|
public function handle(Throwable $e)
|
||||||
{
|
{
|
||||||
|
if (! $e instanceof Exception) {
|
||||||
|
$e = new Exception($e->getMessage(), $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
|
||||||
$response = $this->errorHandler->handle($e);
|
$response = $this->errorHandler->handle($e);
|
||||||
|
|
||||||
$document = new Document;
|
$document = new Document;
|
||||||
|
|
|
@ -11,12 +11,12 @@
|
||||||
|
|
||||||
namespace Flarum\Api\Middleware;
|
namespace Flarum\Api\Middleware;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Flarum\Api\ErrorHandler;
|
use Flarum\Api\ErrorHandler;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
class HandleErrors implements Middleware
|
class HandleErrors implements Middleware
|
||||||
{
|
{
|
||||||
|
@ -40,7 +40,7 @@ class HandleErrors implements Middleware
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
} catch (Exception $e) {
|
} catch (Throwable $e) {
|
||||||
return $this->errorHandler->handle($e);
|
return $this->errorHandler->handle($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@
|
||||||
|
|
||||||
namespace Flarum\Http\Middleware;
|
namespace Flarum\Http\Middleware;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
use Illuminate\Contracts\View\Factory as ViewFactory;
|
use Illuminate\Contracts\View\Factory as ViewFactory;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
|
@ -20,6 +19,7 @@ use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
|
use Throwable;
|
||||||
use Zend\Diactoros\Response\HtmlResponse;
|
use Zend\Diactoros\Response\HtmlResponse;
|
||||||
|
|
||||||
class HandleErrorsWithView implements Middleware
|
class HandleErrorsWithView implements Middleware
|
||||||
|
@ -65,12 +65,12 @@ class HandleErrorsWithView implements Middleware
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
} catch (Exception $e) {
|
} catch (Throwable $e) {
|
||||||
return $this->formatException($e);
|
return $this->formatException($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function formatException(Exception $error)
|
protected function formatException(Throwable $error)
|
||||||
{
|
{
|
||||||
$status = 500;
|
$status = 500;
|
||||||
$errorCode = $error->getCode();
|
$errorCode = $error->getCode();
|
||||||
|
|
|
@ -11,12 +11,12 @@
|
||||||
|
|
||||||
namespace Flarum\Http\Middleware;
|
namespace Flarum\Http\Middleware;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Franzl\Middleware\Whoops\WhoopsRunner;
|
use Franzl\Middleware\Whoops\WhoopsRunner;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
class HandleErrorsWithWhoops implements Middleware
|
class HandleErrorsWithWhoops implements Middleware
|
||||||
{
|
{
|
||||||
|
@ -27,7 +27,7 @@ class HandleErrorsWithWhoops implements Middleware
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
} catch (Exception $e) {
|
} catch (Throwable $e) {
|
||||||
return WhoopsRunner::handle($e, $request);
|
return WhoopsRunner::handle($e, $request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user