From f73a39d3f4bb28f7ce9fce1163d5830a54f75c46 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 9 Aug 2019 20:49:09 +0200 Subject: [PATCH] Remove old error handler, middleware and tests --- framework/core/src/Api/ErrorHandler.php | 51 -------- .../FallbackExceptionHandler.php | 77 ------------ .../FloodingExceptionHandler.php | 42 ------- .../IlluminateValidationExceptionHandler.php | 58 --------- .../InvalidAccessTokenExceptionHandler.php | 42 ------- ...validConfirmationTokenExceptionHandler.php | 42 ------- .../MethodNotAllowedExceptionHandler.php | 42 ------- .../ModelNotFoundExceptionHandler.php | 42 ------- .../PermissionDeniedExceptionHandler.php | 42 ------- .../RouteNotFoundExceptionHandler.php | 42 ------- .../TokenMismatchExceptionHandler.php | 42 ------- .../ValidationExceptionHandler.php | 53 --------- .../core/src/Api/Middleware/HandleErrors.php | 47 -------- .../Http/Middleware/HandleErrorsWithView.php | 111 ------------------ .../Middleware/HandleErrorsWithWhoops.php | 60 ---------- .../FloodingExceptionHandlerTest.php | 46 -------- ...luminateValidationExceptionHandlerTest.php | 63 ---------- ...InvalidAccessTokenExceptionHandlerTest.php | 46 -------- ...dConfirmationTokenExceptionHandlerTest.php | 46 -------- .../MethodNotAllowedExceptionHandlerTest.php | 46 -------- .../ModelNotFoundExceptionHandlerTest.php | 46 -------- .../PermissionDeniedExceptionHandlerTest.php | 46 -------- .../RouteNotFoundExceptionHandlerTest.php | 46 -------- .../TokenMismatchExceptionHandlerTest.php | 46 -------- .../ValidationExceptionHandlerTest.php | 57 --------- 25 files changed, 1281 deletions(-) delete mode 100644 framework/core/src/Api/ErrorHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/FallbackExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/FloodingExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/IlluminateValidationExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/InvalidAccessTokenExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/MethodNotAllowedExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/ModelNotFoundExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/PermissionDeniedExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/RouteNotFoundExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/TokenMismatchExceptionHandler.php delete mode 100644 framework/core/src/Api/ExceptionHandler/ValidationExceptionHandler.php delete mode 100644 framework/core/src/Api/Middleware/HandleErrors.php delete mode 100644 framework/core/src/Http/Middleware/HandleErrorsWithView.php delete mode 100644 framework/core/src/Http/Middleware/HandleErrorsWithWhoops.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php delete mode 100644 framework/core/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php diff --git a/framework/core/src/Api/ErrorHandler.php b/framework/core/src/Api/ErrorHandler.php deleted file mode 100644 index 21ea56e3b..000000000 --- a/framework/core/src/Api/ErrorHandler.php +++ /dev/null @@ -1,51 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Api; - -use Exception; -use Throwable; -use Tobscure\JsonApi\Document; -use Tobscure\JsonApi\ErrorHandler as JsonApiErrorHandler; - -class ErrorHandler -{ - /** - * @var JsonApiErrorHandler - */ - protected $errorHandler; - - /** - * @param JsonApiErrorHandler $errorHandler - */ - public function __construct(JsonApiErrorHandler $errorHandler) - { - $this->errorHandler = $errorHandler; - } - - /** - * @param Exception $e - * @return JsonApiResponse - */ - 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; - $document->setErrors($response->getErrors()); - - return new JsonApiResponse($document, $response->getStatus()); - } -} diff --git a/framework/core/src/Api/ExceptionHandler/FallbackExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/FallbackExceptionHandler.php deleted file mode 100644 index 1087dbe89..000000000 --- a/framework/core/src/Api/ExceptionHandler/FallbackExceptionHandler.php +++ /dev/null @@ -1,77 +0,0 @@ - - * - * 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/ExceptionHandler/FloodingExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/FloodingExceptionHandler.php deleted file mode 100644 index 0a55870dc..000000000 --- a/framework/core/src/Api/ExceptionHandler/FloodingExceptionHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 Flarum\Post\Exception\FloodingException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class FloodingExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof FloodingException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $status = 429; - $error = [ - 'status' => (string) $status, - 'code' => 'too_many_requests' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/framework/core/src/Api/ExceptionHandler/IlluminateValidationExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/IlluminateValidationExceptionHandler.php deleted file mode 100644 index afc496c1d..000000000 --- a/framework/core/src/Api/ExceptionHandler/IlluminateValidationExceptionHandler.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * 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 Illuminate\Validation\ValidationException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class IlluminateValidationExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof ValidationException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $status = 422; - - $errors = $this->formatErrors($e->errors()); - - return new ResponseBag($status, $errors); - } - - /** - * @param array $errors - * @return array - */ - protected function formatErrors(array $errors) - { - $errors = array_map(function ($field, $messages) { - return [ - 'status' => '422', - 'code' => 'validation_error', - 'detail' => implode("\n", $messages), - 'source' => ['pointer' => "/data/attributes/$field"] - ]; - }, array_keys($errors), $errors); - - return $errors; - } -} diff --git a/framework/core/src/Api/ExceptionHandler/InvalidAccessTokenExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/InvalidAccessTokenExceptionHandler.php deleted file mode 100644 index c91964f17..000000000 --- a/framework/core/src/Api/ExceptionHandler/InvalidAccessTokenExceptionHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 Flarum\Api\Exception\InvalidAccessTokenException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class InvalidAccessTokenExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof InvalidAccessTokenException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $status = 401; - $error = [ - 'status' => (string) $status, - 'code' => 'invalid_access_token' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/framework/core/src/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandler.php deleted file mode 100644 index 541b620f7..000000000 --- a/framework/core/src/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 Flarum\User\Exception\InvalidConfirmationTokenException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class InvalidConfirmationTokenExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof InvalidConfirmationTokenException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $status = 403; - $error = [ - 'status' => (string) $status, - 'code' => 'invalid_confirmation_token' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/framework/core/src/Api/ExceptionHandler/MethodNotAllowedExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/MethodNotAllowedExceptionHandler.php deleted file mode 100644 index 729815e95..000000000 --- a/framework/core/src/Api/ExceptionHandler/MethodNotAllowedExceptionHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 Flarum\Http\Exception\MethodNotAllowedException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class MethodNotAllowedExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof MethodNotAllowedException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $status = 405; - $error = [ - 'status' => (string) $status, - 'code' => 'method_not_allowed' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/framework/core/src/Api/ExceptionHandler/ModelNotFoundExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/ModelNotFoundExceptionHandler.php deleted file mode 100644 index e921eb166..000000000 --- a/framework/core/src/Api/ExceptionHandler/ModelNotFoundExceptionHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 Illuminate\Database\Eloquent\ModelNotFoundException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class ModelNotFoundExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof ModelNotFoundException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $status = 404; - $error = [ - 'status' => (string) $status, - 'code' => 'resource_not_found' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/framework/core/src/Api/ExceptionHandler/PermissionDeniedExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/PermissionDeniedExceptionHandler.php deleted file mode 100644 index a4b4f9d53..000000000 --- a/framework/core/src/Api/ExceptionHandler/PermissionDeniedExceptionHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 Flarum\User\Exception\PermissionDeniedException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class PermissionDeniedExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof PermissionDeniedException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $status = 401; - $error = [ - 'status' => (string) $status, - 'code' => 'permission_denied' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/framework/core/src/Api/ExceptionHandler/RouteNotFoundExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/RouteNotFoundExceptionHandler.php deleted file mode 100644 index 67c25d6a1..000000000 --- a/framework/core/src/Api/ExceptionHandler/RouteNotFoundExceptionHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 Flarum\Http\Exception\RouteNotFoundException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class RouteNotFoundExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof RouteNotFoundException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $status = 404; - $error = [ - 'status' => (string) $status, - 'code' => 'route_not_found' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/framework/core/src/Api/ExceptionHandler/TokenMismatchExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/TokenMismatchExceptionHandler.php deleted file mode 100644 index 2258b4cd6..000000000 --- a/framework/core/src/Api/ExceptionHandler/TokenMismatchExceptionHandler.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 Flarum\Http\Exception\TokenMismatchException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class TokenMismatchExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof TokenMismatchException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $status = 400; - $error = [ - 'status' => (string) $status, - 'code' => 'csrf_token_mismatch' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/framework/core/src/Api/ExceptionHandler/ValidationExceptionHandler.php b/framework/core/src/Api/ExceptionHandler/ValidationExceptionHandler.php deleted file mode 100644 index f36b253fd..000000000 --- a/framework/core/src/Api/ExceptionHandler/ValidationExceptionHandler.php +++ /dev/null @@ -1,53 +0,0 @@ - - * - * 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 Flarum\Foundation\ValidationException; -use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface; -use Tobscure\JsonApi\Exception\Handler\ResponseBag; - -class ValidationExceptionHandler implements ExceptionHandlerInterface -{ - /** - * {@inheritdoc} - */ - public function manages(Exception $e) - { - return $e instanceof ValidationException; - } - - /** - * {@inheritdoc} - */ - public function handle(Exception $e) - { - $errors = array_merge( - $this->buildErrors($e->getAttributes(), '/data/attributes'), - $this->buildErrors($e->getRelationships(), '/data/relationships') - ); - - return new ResponseBag(422, $errors); - } - - private function buildErrors(array $messages, $pointer) - { - return array_map(function ($path, $detail) use ($pointer) { - return [ - 'status' => '422', - 'code' => 'validation_error', - 'detail' => $detail, - 'source' => ['pointer' => $pointer.'/'.$path] - ]; - }, array_keys($messages), $messages); - } -} diff --git a/framework/core/src/Api/Middleware/HandleErrors.php b/framework/core/src/Api/Middleware/HandleErrors.php deleted file mode 100644 index cfcc54068..000000000 --- a/framework/core/src/Api/Middleware/HandleErrors.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Api\Middleware; - -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 -{ - /** - * @var ErrorHandler - */ - protected $errorHandler; - - /** - * @param ErrorHandler $errorHandler - */ - public function __construct(ErrorHandler $errorHandler) - { - $this->errorHandler = $errorHandler; - } - - /** - * Catch all errors that happen during further middleware execution. - */ - public function process(Request $request, Handler $handler): Response - { - try { - return $handler->handle($request); - } 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 deleted file mode 100644 index 960074f39..000000000 --- a/framework/core/src/Http/Middleware/HandleErrorsWithView.php +++ /dev/null @@ -1,111 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Http\Middleware; - -use Flarum\Settings\SettingsRepositoryInterface; -use Illuminate\Contracts\View\Factory as ViewFactory; -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 Symfony\Component\Translation\TranslatorInterface; -use Throwable; -use Zend\Diactoros\Response\HtmlResponse; - -class HandleErrorsWithView implements Middleware -{ - /** - * @var ViewFactory - */ - protected $view; - - /** - * @var LoggerInterface - */ - protected $logger; - - /** - * @var TranslatorInterface - */ - protected $translator; - - /** - * @var SettingsRepositoryInterface - */ - protected $settings; - - /** - * @param ViewFactory $view - * @param LoggerInterface $logger - * @param TranslatorInterface $translator - * @param SettingsRepositoryInterface $settings - */ - public function __construct(ViewFactory $view, LoggerInterface $logger, TranslatorInterface $translator, SettingsRepositoryInterface $settings) - { - $this->view = $view; - $this->logger = $logger; - $this->translator = $translator; - $this->settings = $settings; - } - - /** - * Catch all errors that happen during further middleware execution. - */ - public function process(Request $request, Handler $handler): Response - { - try { - return $handler->handle($request); - } catch (Throwable $e) { - return $this->formatException($e); - } - } - - protected function formatException(Throwable $error) - { - $status = 500; - $errorCode = $error->getCode(); - - // If it seems to be a valid HTTP status code, we pass on the - // exception's status. - if (is_int($errorCode) && $errorCode >= 400 && $errorCode < 600) { - $status = $errorCode; - } - - if (! $this->view->exists($name = "flarum.forum::error.$status")) { - $name = 'flarum.forum::error.default'; - - $this->logger->error($error); - } - - $view = $this->view->make($name) - ->with('error', $error) - ->with('message', $this->getMessage($status)); - - return new HtmlResponse($view->render(), $status); - } - - private function getMessage($status) - { - return $this->getTranslationIfExists($status) - ?? $this->getTranslationIfExists(500) - ?? 'An error occurred while trying to load this page.'; - } - - private function getTranslationIfExists($status) - { - $key = "core.views.error.${status}_message"; - $translation = $this->translator->trans($key, ['{forum}' => $this->settings->get('forum_title')]); - - return $translation === $key ? null : $translation; - } -} diff --git a/framework/core/src/Http/Middleware/HandleErrorsWithWhoops.php b/framework/core/src/Http/Middleware/HandleErrorsWithWhoops.php deleted file mode 100644 index 317d3dd2d..000000000 --- a/framework/core/src/Http/Middleware/HandleErrorsWithWhoops.php +++ /dev/null @@ -1,60 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Http\Middleware; - -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. - */ - public function process(Request $request, Handler $handler): Response - { - try { - return $handler->handle($request); - } catch (Throwable $e) { - $status = 500; - $errorCode = $e->getCode(); - - if ($errorCode !== 404) { - $this->logger->error($e); - } - - if (is_int($errorCode) && $errorCode >= 400 && $errorCode < 600) { - $status = $errorCode; - } - - return WhoopsRunner::handle($e, $request) - ->withStatus($status); - } - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php deleted file mode 100644 index e2043b493..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\ExceptionHandler\FloodingExceptionHandler; -use Flarum\Post\Exception\FloodingException; -use PHPUnit\Framework\TestCase; - -class FloodingExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new FloodingExceptionHandler; - } - - public function test_it_handles_recognisable_exceptions() - { - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages(new FloodingException)); - } - - public function test_it_provides_expected_output() - { - $result = $this->handler->handle(new FloodingException); - - $this->assertEquals(429, $result->getStatus()); - $this->assertEquals([ - [ - 'status' => '429', - 'code' => 'too_many_requests' - ] - ], $result->getErrors()); - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php deleted file mode 100644 index ed3d799f6..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php +++ /dev/null @@ -1,63 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\ExceptionHandler\IlluminateValidationExceptionHandler; -use Illuminate\Translation\ArrayLoader; -use Illuminate\Translation\Translator; -use Illuminate\Validation\Factory; -use Illuminate\Validation\ValidationException; -use PHPUnit\Framework\TestCase; - -class IlluminateValidationExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new IlluminateValidationExceptionHandler; - } - - public function test_it_handles_familiar_exceptions() - { - $validException = new ValidationException($this->makeValidator()); - - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages($validException)); - } - - public function test_it_creates_the_desired_output() - { - $exception = new ValidationException($this->makeValidator(['foo' => ''], ['foo' => 'required'])); - - $response = $this->handler->handle($exception); - - $this->assertEquals(422, $response->getStatus()); - $this->assertEquals([ - [ - 'status' => '422', - 'code' => 'validation_error', - 'detail' => 'validation.required', - 'source' => ['pointer' => '/data/attributes/foo'] - ] - ], $response->getErrors()); - } - - private function makeValidator($data = [], $rules = []) - { - $translator = new Translator(new ArrayLoader(), 'en'); - $factory = new Factory($translator); - - return $factory->make($data, $rules); - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php deleted file mode 100644 index 3d7355eac..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\Exception\InvalidAccessTokenException; -use Flarum\Api\ExceptionHandler\InvalidAccessTokenExceptionHandler; -use PHPUnit\Framework\TestCase; - -class InvalidAccessTokenExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new InvalidAccessTokenExceptionHandler; - } - - public function test_it_handles_recognisable_exceptions() - { - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages(new InvalidAccessTokenException)); - } - - public function test_output() - { - $response = $this->handler->handle(new InvalidAccessTokenException); - - $this->assertEquals(401, $response->getStatus()); - $this->assertEquals([ - [ - 'status' => '401', - 'code' => 'invalid_access_token' - ] - ], $response->getErrors()); - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php deleted file mode 100644 index 1f289fe73..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\ExceptionHandler\InvalidConfirmationTokenExceptionHandler; -use Flarum\User\Exception\InvalidConfirmationTokenException; -use PHPUnit\Framework\TestCase; - -class InvalidConfirmationTokenExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new InvalidConfirmationTokenExceptionHandler; - } - - public function test_it_handles_recognisable_exceptions() - { - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages(new InvalidConfirmationTokenException)); - } - - public function test_output() - { - $response = $this->handler->handle(new InvalidConfirmationTokenException); - - $this->assertEquals(403, $response->getStatus()); - $this->assertEquals([ - [ - 'status' => '403', - 'code' => 'invalid_confirmation_token' - ] - ], $response->getErrors()); - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php deleted file mode 100644 index 23ce0761f..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\ExceptionHandler\MethodNotAllowedExceptionHandler; -use Flarum\Http\Exception\MethodNotAllowedException; -use PHPUnit\Framework\TestCase; - -class MethodNotAllowedExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new MethodNotAllowedExceptionHandler(); - } - - public function test_it_handles_recognisable_exceptions() - { - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages(new MethodNotAllowedException())); - } - - public function test_managing_exceptions() - { - $response = $this->handler->handle(new MethodNotAllowedException); - - $this->assertEquals(405, $response->getStatus()); - $this->assertEquals([ - [ - 'status' => '405', - 'code' => 'method_not_allowed' - ] - ], $response->getErrors()); - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php deleted file mode 100644 index c1eb2efcc..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\ExceptionHandler\ModelNotFoundExceptionHandler; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use PHPUnit\Framework\TestCase; - -class ModelNotFoundExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new ModelNotFoundExceptionHandler; - } - - public function test_it_handles_recognisable_exceptions() - { - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages(new ModelNotFoundException)); - } - - public function test_managing_exceptions() - { - $response = $this->handler->handle(new ModelNotFoundException); - - $this->assertEquals(404, $response->getStatus()); - $this->assertEquals([ - [ - 'status' => '404', - 'code' => 'resource_not_found' - ] - ], $response->getErrors()); - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php deleted file mode 100644 index dcfdb33f8..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\ExceptionHandler\PermissionDeniedExceptionHandler; -use Flarum\User\Exception\PermissionDeniedException; -use PHPUnit\Framework\TestCase; - -class PermissionDeniedExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new PermissionDeniedExceptionHandler; - } - - public function test_it_handles_recognisable_exceptions() - { - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages(new PermissionDeniedException)); - } - - public function test_managing_exceptions() - { - $response = $this->handler->handle(new PermissionDeniedException); - - $this->assertEquals(401, $response->getStatus()); - $this->assertEquals([ - [ - 'status' => '401', - 'code' => 'permission_denied' - ] - ], $response->getErrors()); - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php deleted file mode 100644 index 51c82cb70..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\ExceptionHandler\RouteNotFoundExceptionHandler; -use Flarum\Http\Exception\RouteNotFoundException; -use PHPUnit\Framework\TestCase; - -class RouteNotFoundExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new RouteNotFoundExceptionHandler(); - } - - public function test_it_handles_recognisable_exceptions() - { - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages(new RouteNotFoundException())); - } - - public function test_managing_exceptions() - { - $response = $this->handler->handle(new RouteNotFoundException); - - $this->assertEquals(404, $response->getStatus()); - $this->assertEquals([ - [ - 'status' => '404', - 'code' => 'route_not_found' - ] - ], $response->getErrors()); - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php deleted file mode 100644 index eebb1bff8..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\ExceptionHandler\TokenMismatchExceptionHandler; -use Flarum\Http\Exception\TokenMismatchException; -use PHPUnit\Framework\TestCase; - -class TokenMismatchExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new TokenMismatchExceptionHandler; - } - - public function test_it_handles_recognisable_exceptions() - { - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages(new TokenMismatchException())); - } - - public function test_managing_exceptions() - { - $response = $this->handler->handle(new TokenMismatchException); - - $this->assertEquals(400, $response->getStatus()); - $this->assertEquals([ - [ - 'status' => '400', - 'code' => 'csrf_token_mismatch' - ] - ], $response->getErrors()); - } -} diff --git a/framework/core/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php deleted file mode 100644 index a014a96e5..000000000 --- a/framework/core/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php +++ /dev/null @@ -1,57 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\unit\Api\ExceptionHandler; - -use Exception; -use Flarum\Api\ExceptionHandler\ValidationExceptionHandler; -use Flarum\Foundation\ValidationException; -use PHPUnit\Framework\TestCase; - -class ValidationExceptionHandlerTest extends TestCase -{ - private $handler; - - public function setUp() - { - $this->handler = new ValidationExceptionHandler; - } - - public function test_it_handles_recognisable_exceptions() - { - $this->assertFalse($this->handler->manages(new Exception)); - $this->assertTrue($this->handler->manages(new ValidationException([]))); - } - - public function test_managing_exceptions() - { - $response = $this->handler->handle(new ValidationException( - ['foo' => 'Attribute error'], - ['bar' => 'Relationship error'] - )); - - $this->assertEquals(422, $response->getStatus()); - $this->assertEquals([ - [ - 'status' => '422', - 'code' => 'validation_error', - 'detail' => 'Attribute error', - 'source' => ['pointer' => '/data/attributes/foo'] - ], - [ - 'status' => '422', - 'code' => 'validation_error', - 'detail' => 'Relationship error', - 'source' => ['pointer' => '/data/relationships/bar'] - ] - ], $response->getErrors()); - } -}