diff --git a/src/Api/ErrorHandler.php b/src/Api/ErrorHandler.php deleted file mode 100644 index 49bea1dd4..000000000 --- a/src/Api/ErrorHandler.php +++ /dev/null @@ -1,49 +0,0 @@ -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/src/Api/ExceptionHandler/FallbackExceptionHandler.php b/src/Api/ExceptionHandler/FallbackExceptionHandler.php deleted file mode 100644 index c04fe2c3e..000000000 --- a/src/Api/ExceptionHandler/FallbackExceptionHandler.php +++ /dev/null @@ -1,75 +0,0 @@ -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/src/Api/ExceptionHandler/FloodingExceptionHandler.php b/src/Api/ExceptionHandler/FloodingExceptionHandler.php deleted file mode 100644 index a97020318..000000000 --- a/src/Api/ExceptionHandler/FloodingExceptionHandler.php +++ /dev/null @@ -1,40 +0,0 @@ - (string) $status, - 'code' => 'too_many_requests' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/src/Api/ExceptionHandler/IlluminateValidationExceptionHandler.php b/src/Api/ExceptionHandler/IlluminateValidationExceptionHandler.php deleted file mode 100644 index 0f849a762..000000000 --- a/src/Api/ExceptionHandler/IlluminateValidationExceptionHandler.php +++ /dev/null @@ -1,56 +0,0 @@ -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/src/Api/ExceptionHandler/InvalidAccessTokenExceptionHandler.php b/src/Api/ExceptionHandler/InvalidAccessTokenExceptionHandler.php deleted file mode 100644 index e38c20a79..000000000 --- a/src/Api/ExceptionHandler/InvalidAccessTokenExceptionHandler.php +++ /dev/null @@ -1,40 +0,0 @@ - (string) $status, - 'code' => 'invalid_access_token' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/src/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandler.php b/src/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandler.php deleted file mode 100644 index b7fa2fbf6..000000000 --- a/src/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandler.php +++ /dev/null @@ -1,40 +0,0 @@ - (string) $status, - 'code' => 'invalid_confirmation_token' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/src/Api/ExceptionHandler/MethodNotAllowedExceptionHandler.php b/src/Api/ExceptionHandler/MethodNotAllowedExceptionHandler.php deleted file mode 100644 index e93d2e291..000000000 --- a/src/Api/ExceptionHandler/MethodNotAllowedExceptionHandler.php +++ /dev/null @@ -1,40 +0,0 @@ - (string) $status, - 'code' => 'method_not_allowed' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/src/Api/ExceptionHandler/ModelNotFoundExceptionHandler.php b/src/Api/ExceptionHandler/ModelNotFoundExceptionHandler.php deleted file mode 100644 index 9ac08c358..000000000 --- a/src/Api/ExceptionHandler/ModelNotFoundExceptionHandler.php +++ /dev/null @@ -1,40 +0,0 @@ - (string) $status, - 'code' => 'resource_not_found' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/src/Api/ExceptionHandler/PermissionDeniedExceptionHandler.php b/src/Api/ExceptionHandler/PermissionDeniedExceptionHandler.php deleted file mode 100644 index 18de41641..000000000 --- a/src/Api/ExceptionHandler/PermissionDeniedExceptionHandler.php +++ /dev/null @@ -1,40 +0,0 @@ - (string) $status, - 'code' => 'permission_denied' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/src/Api/ExceptionHandler/RouteNotFoundExceptionHandler.php b/src/Api/ExceptionHandler/RouteNotFoundExceptionHandler.php deleted file mode 100644 index 04b927569..000000000 --- a/src/Api/ExceptionHandler/RouteNotFoundExceptionHandler.php +++ /dev/null @@ -1,40 +0,0 @@ - (string) $status, - 'code' => 'route_not_found' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/src/Api/ExceptionHandler/TokenMismatchExceptionHandler.php b/src/Api/ExceptionHandler/TokenMismatchExceptionHandler.php deleted file mode 100644 index f4fa5ac3b..000000000 --- a/src/Api/ExceptionHandler/TokenMismatchExceptionHandler.php +++ /dev/null @@ -1,40 +0,0 @@ - (string) $status, - 'code' => 'csrf_token_mismatch' - ]; - - return new ResponseBag($status, [$error]); - } -} diff --git a/src/Api/ExceptionHandler/ValidationExceptionHandler.php b/src/Api/ExceptionHandler/ValidationExceptionHandler.php deleted file mode 100644 index 6adbb0642..000000000 --- a/src/Api/ExceptionHandler/ValidationExceptionHandler.php +++ /dev/null @@ -1,51 +0,0 @@ -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/src/Api/Middleware/HandleErrors.php b/src/Api/Middleware/HandleErrors.php deleted file mode 100644 index 46111f8c1..000000000 --- a/src/Api/Middleware/HandleErrors.php +++ /dev/null @@ -1,45 +0,0 @@ -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/src/Http/Middleware/HandleErrorsWithView.php b/src/Http/Middleware/HandleErrorsWithView.php deleted file mode 100644 index e898f2390..000000000 --- a/src/Http/Middleware/HandleErrorsWithView.php +++ /dev/null @@ -1,109 +0,0 @@ -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/src/Http/Middleware/HandleErrorsWithWhoops.php b/src/Http/Middleware/HandleErrorsWithWhoops.php deleted file mode 100644 index e07524ca5..000000000 --- a/src/Http/Middleware/HandleErrorsWithWhoops.php +++ /dev/null @@ -1,58 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php deleted file mode 100644 index 6f600c8cb..000000000 --- a/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php deleted file mode 100644 index 499e5164a..000000000 --- a/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php +++ /dev/null @@ -1,61 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php deleted file mode 100644 index 510eb78f8..000000000 --- a/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php deleted file mode 100644 index 89f779fe2..000000000 --- a/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php deleted file mode 100644 index c1166f2ca..000000000 --- a/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php deleted file mode 100644 index ef8e499ae..000000000 --- a/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php deleted file mode 100644 index 74e11ee45..000000000 --- a/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php deleted file mode 100644 index 94b7a56ee..000000000 --- a/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php deleted file mode 100644 index 78f89c5a7..000000000 --- a/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -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/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php b/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php deleted file mode 100644 index cf0a511f2..000000000 --- a/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php +++ /dev/null @@ -1,55 +0,0 @@ -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()); - } -}