diff --git a/framework/core/src/Api/ApiServiceProvider.php b/framework/core/src/Api/ApiServiceProvider.php index 50fbfd12d..d49398eae 100644 --- a/framework/core/src/Api/ApiServiceProvider.php +++ b/framework/core/src/Api/ApiServiceProvider.php @@ -25,7 +25,6 @@ use Flarum\Http\RouteCollection; use Flarum\Http\RouteHandlerFactory; use Flarum\Http\UrlGenerator; use Tobscure\JsonApi\ErrorHandler; -use Tobscure\JsonApi\Exception\Handler\FallbackExceptionHandler; use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler; use Zend\Stratigility\MiddlewarePipe; @@ -80,7 +79,7 @@ class ApiServiceProvider extends AbstractServiceProvider $handler->registerHandler(new ExceptionHandler\TokenMismatchExceptionHandler); $handler->registerHandler(new ExceptionHandler\ValidationExceptionHandler); $handler->registerHandler(new InvalidParameterExceptionHandler); - $handler->registerHandler(new FallbackExceptionHandler($this->app->inDebugMode())); + $handler->registerHandler(new ExceptionHandler\FallbackExceptionHandler($this->app->inDebugMode(), $this->app->make('log'))); return $handler; }); diff --git a/framework/core/src/Api/ErrorHandler.php b/framework/core/src/Api/ErrorHandler.php index 3f6125adc..21ea56e3b 100644 --- a/framework/core/src/Api/ErrorHandler.php +++ b/framework/core/src/Api/ErrorHandler.php @@ -12,6 +12,7 @@ namespace Flarum\Api; use Exception; +use Throwable; use Tobscure\JsonApi\Document; use Tobscure\JsonApi\ErrorHandler as JsonApiErrorHandler; @@ -34,8 +35,12 @@ class ErrorHandler * @param Exception $e * @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); $document = new Document; diff --git a/framework/core/src/Api/ExceptionHandler/FallbackExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/FallbackExceptionHandler.php new file mode 100644 index 000000000..1087dbe89 --- /dev/null +++ b/framework/core/src/Api/ExceptionHandler/FallbackExceptionHandler.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Api\ExceptionHandler; + +use Exception; +use Psr\Log\LoggerInterface; +use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; +use Tobscure\JsonApi\Exception\Handler\ResponseBag; + +class FallbackExceptionHandler implements ExceptionHandlerInterface +{ + /** + * @var bool + */ + protected $debug; + + /** + * @var LoggerInterface + */ + protected $logger; + + /** + * @param bool $debug + * @param LoggerInterface $logger + */ + public function __construct($debug, LoggerInterface $logger) + { + $this->debug = $debug; + $this->logger = $logger; + } + + /** + * {@inheritdoc} + */ + public function manages(Exception $e) + { + return true; + } + + /** + * {@inheritdoc} + */ + public function handle(Exception $e) + { + $status = 500; + $error = $this->constructError($e, $status); + + $this->logger->error($e); + + return new ResponseBag($status, [$error]); + } + + /** + * @param Exception $e + * @param $status + * @return array + */ + private function constructError(Exception $e, $status) + { + $error = ['code' => $status, 'title' => 'Internal server error']; + + if ($this->debug) { + $error['detail'] = (string) $e; + } + + return $error; + } +} diff --git a/framework/core/src/Api/Middleware/HandleErrors.php b/framework/core/src/Api/Middleware/HandleErrors.php index bba861ac2..cfcc54068 100644 --- a/framework/core/src/Api/Middleware/HandleErrors.php +++ b/framework/core/src/Api/Middleware/HandleErrors.php @@ -11,12 +11,12 @@ namespace Flarum\Api\Middleware; -use Exception; use Flarum\Api\ErrorHandler; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\RequestHandlerInterface as Handler; +use Throwable; class HandleErrors implements Middleware { @@ -40,7 +40,7 @@ class HandleErrors implements Middleware { try { return $handler->handle($request); - } catch (Exception $e) { + } catch (Throwable $e) { return $this->errorHandler->handle($e); } } diff --git a/framework/core/src/Http/Middleware/HandleErrorsWithView.php b/framework/core/src/Http/Middleware/HandleErrorsWithView.php index 0ef7f5139..960074f39 100644 --- a/framework/core/src/Http/Middleware/HandleErrorsWithView.php +++ b/framework/core/src/Http/Middleware/HandleErrorsWithView.php @@ -11,7 +11,6 @@ namespace Flarum\Http\Middleware; -use Exception; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\View\Factory as ViewFactory; 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\Log\LoggerInterface; use Symfony\Component\Translation\TranslatorInterface; +use Throwable; use Zend\Diactoros\Response\HtmlResponse; class HandleErrorsWithView implements Middleware @@ -65,12 +65,12 @@ class HandleErrorsWithView implements Middleware { try { return $handler->handle($request); - } catch (Exception $e) { + } catch (Throwable $e) { return $this->formatException($e); } } - protected function formatException(Exception $error) + protected function formatException(Throwable $error) { $status = 500; $errorCode = $error->getCode(); @@ -81,11 +81,10 @@ class HandleErrorsWithView implements Middleware $status = $errorCode; } - // Log the exception (with trace) - $this->logger->debug($error); - if (! $this->view->exists($name = "flarum.forum::error.$status")) { $name = 'flarum.forum::error.default'; + + $this->logger->error($error); } $view = $this->view->make($name) diff --git a/framework/core/src/Http/Middleware/HandleErrorsWithWhoops.php b/framework/core/src/Http/Middleware/HandleErrorsWithWhoops.php index 57d56b2ad..83df56af9 100644 --- a/framework/core/src/Http/Middleware/HandleErrorsWithWhoops.php +++ b/framework/core/src/Http/Middleware/HandleErrorsWithWhoops.php @@ -11,15 +11,29 @@ namespace Flarum\Http\Middleware; -use Exception; use Franzl\Middleware\Whoops\WhoopsRunner; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\RequestHandlerInterface as Handler; +use Psr\Log\LoggerInterface; +use Throwable; class HandleErrorsWithWhoops implements Middleware { + /** + * @var LoggerInterface + */ + protected $logger; + + /** + * @param LoggerInterface $logger + */ + public function __construct(LoggerInterface $logger) + { + $this->logger = $logger; + } + /** * Catch all errors that happen during further middleware execution. */ @@ -27,7 +41,11 @@ class HandleErrorsWithWhoops implements Middleware { try { return $handler->handle($request); - } catch (Exception $e) { + } catch (Throwable $e) { + if ($e->getCode() !== 404) { + $this->logger->error($e); + } + return WhoopsRunner::handle($e, $request); } }