From 4758b75903aa15febd0b638e26e792a873a7c82a Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Mon, 23 Sep 2024 15:06:56 +0200 Subject: [PATCH 01/10] Custom error handler --- CHANGELOG.md | 1 + README.md | 9 ++++++++- src/Error/DefaultErrorHandler.php | 28 ++++++++++++++++++++++++++++ src/Error/ErrorHandlerInterface.php | 13 +++++++++++++ src/Presenters/ApiPresenter.php | 13 ++++++------- 5 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 src/Error/DefaultErrorHandler.php create mode 100644 src/Error/ErrorHandlerInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b53b404..43f33e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip * [BC] DefaultHandler response code 404 instead 400 * [BC] Added Container to API Decider * [BC] Output Configurator, Allows different methods for output configuration. Needs to be added to config services. +* [BC] Error handler, Allows for custom error handling of handle method. Needs to be added to config services. * Query configurator rework #### Added diff --git a/README.md b/README.md index f164740..4ea800e 100644 --- a/README.md +++ b/README.md @@ -40,13 +40,20 @@ application: Api: Tomaj\NetteApi\Presenters\*Presenter ``` -Then register your preffered output configurator in *config.neon* services: +Register your preferred output configurator in *config.neon* services: ```neon services: apiOutputConfigurator: Tomaj\NetteApi\Output\Configurator\DebuggerConfigurator ``` +Register your preferred error handler in *config.neon* services: + +```neon +services: + apiErrorHandler: Tomaj\NetteApi\Error\DefaultErrorHandler +``` + And add route to you RouterFactory: ```php diff --git a/src/Error/DefaultErrorHandler.php b/src/Error/DefaultErrorHandler.php new file mode 100644 index 0000000..d77bdea --- /dev/null +++ b/src/Error/DefaultErrorHandler.php @@ -0,0 +1,28 @@ +outputConfigurator->showErrorDetail()) { + $response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error', 'detail' => $exception->getMessage()]); + } else { + $response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error']); + } + return $response; + } +} diff --git a/src/Error/ErrorHandlerInterface.php b/src/Error/ErrorHandlerInterface.php new file mode 100644 index 0000000..7c1b5f1 --- /dev/null +++ b/src/Error/ErrorHandlerInterface.php @@ -0,0 +1,13 @@ +outputConfigurator->showErrorDetail()) { - $response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error', 'detail' => $exception->getMessage()]); - } else { - $response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error']); - } + $response = $this->errorHandler->handle($exception); $code = $response->getCode(); - Debugger::log($exception, Debugger::EXCEPTION); } $end = microtime(true); From 3e21a427ffdb5d020023b2a20c1919ce01d7fca4 Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Thu, 26 Sep 2024 09:26:19 +0200 Subject: [PATCH 02/10] use constructor for error handler --- src/Error/DefaultErrorHandler.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Error/DefaultErrorHandler.php b/src/Error/DefaultErrorHandler.php index d77bdea..f6430f8 100644 --- a/src/Error/DefaultErrorHandler.php +++ b/src/Error/DefaultErrorHandler.php @@ -12,9 +12,14 @@ final class DefaultErrorHandler implements ErrorHandlerInterface { - /** @var ConfiguratorInterface @inject */ + /** @var ConfiguratorInterface */ public $outputConfigurator; + public function __construct(ConfiguratorInterface $outputConfigurator) + { + $this->outputConfigurator = $outputConfigurator; + } + public function handle(Throwable $exception): JsonApiResponse { Debugger::log($exception, Debugger::EXCEPTION); From dc6facc85016efddc6faf24e23ffa8f06ac1b963 Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Thu, 26 Sep 2024 13:25:35 +0200 Subject: [PATCH 03/10] Handle auth, input and schema --- src/Error/DefaultErrorHandler.php | 28 ++++++++++++++++++++++++++++ src/Error/ErrorHandlerInterface.php | 7 +++++++ src/Presenters/ApiPresenter.php | 20 ++++++++------------ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/Error/DefaultErrorHandler.php b/src/Error/DefaultErrorHandler.php index f6430f8..82cedd9 100644 --- a/src/Error/DefaultErrorHandler.php +++ b/src/Error/DefaultErrorHandler.php @@ -6,6 +6,7 @@ use Nette\Http\Response; use Throwable; +use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; use Tomaj\NetteApi\Output\Configurator\ConfiguratorInterface; use Tomaj\NetteApi\Response\JsonApiResponse; use Tracy\Debugger; @@ -30,4 +31,31 @@ public function handle(Throwable $exception): JsonApiResponse } return $response; } + + public function handleInputParams(array $errors): JsonApiResponse + { + if ($this->outputConfigurator->showErrorDetail()) { + $response = new JsonApiResponse(Response::S400_BAD_REQUEST, ['status' => 'error', 'message' => 'wrong input', 'detail' => $errors]); + } else { + $response = new JsonApiResponse(Response::S400_BAD_REQUEST, ['status' => 'error', 'message' => 'wrong input']); + } + return $response; + } + + public function handleSchema(array $errors): JsonApiResponse + { + Debugger::log($errors, Debugger::ERROR); + + if ($this->outputConfigurator->showErrorDetail()) { + $response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error', 'detail' => $errors]); + } else { + $response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error']); + } + return $response; + } + + public function handleAuthorization(ApiAuthorizationInterface $auth): JsonApiResponse + { + return new JsonApiResponse(Response::S403_FORBIDDEN, ['status' => 'error', 'message' => $auth->getErrorMessage()]); + } } diff --git a/src/Error/ErrorHandlerInterface.php b/src/Error/ErrorHandlerInterface.php index 7c1b5f1..e90a2e8 100644 --- a/src/Error/ErrorHandlerInterface.php +++ b/src/Error/ErrorHandlerInterface.php @@ -4,10 +4,17 @@ namespace Tomaj\NetteApi\Error; +use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; use Tomaj\NetteApi\Response\JsonApiResponse; use Throwable; interface ErrorHandlerInterface { public function handle(Throwable $error): JsonApiResponse; + + public function handleInputParams(array $errors): JsonApiResponse; + + public function handleSchema(array $errors): JsonApiResponse; + + public function handleAuthorization(ApiAuthorizationInterface $auth): JsonApiResponse; } diff --git a/src/Presenters/ApiPresenter.php b/src/Presenters/ApiPresenter.php index 81b615b..ffca49d 100644 --- a/src/Presenters/ApiPresenter.php +++ b/src/Presenters/ApiPresenter.php @@ -21,8 +21,6 @@ use Tomaj\NetteApi\Output\OutputInterface; use Tomaj\NetteApi\Params\ParamsProcessor; use Tomaj\NetteApi\RateLimit\RateLimitInterface; -use Tomaj\NetteApi\Response\JsonApiResponse; -use Tracy\Debugger; final class ApiPresenter implements IPresenter { @@ -90,14 +88,11 @@ public function run(Request $request): IResponse $paramsProcessor = new ParamsProcessor($handler->params()); if ($paramsProcessor->isError()) { - $this->response->setCode(Response::S400_BAD_REQUEST); - if ($this->outputConfigurator->showErrorDetail()) { - $response = new JsonResponse(['status' => 'error', 'message' => 'wrong input', 'detail' => $paramsProcessor->getErrors()]); - } else { - $response = new JsonResponse(['status' => 'error', 'message' => 'wrong input']); - } + $response = $this->errorHandler->handleInputParams($paramsProcessor->getErrors()); + $this->response->setCode($response->getCode()); return $response; } + $params = $paramsProcessor->getValues(); try { $response = $handler->handle($params); @@ -120,8 +115,8 @@ public function run(Request $request): IResponse $outputValidatorErrors[] = $validationResult->getErrors(); } if (!$outputValid) { - Debugger::log($outputValidatorErrors, Debugger::ERROR); - $response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error', 'details' => $outputValidatorErrors]); + $response = $this->errorHandler->handleSchema($outputValidatorErrors); + $code = $response->getCode(); } } } catch (Throwable $exception) { @@ -155,8 +150,9 @@ private function getApi(Request $request): Api private function checkAuth(ApiAuthorizationInterface $authorization): ?IResponse { if (!$authorization->authorized()) { - $this->response->setCode(Response::S403_FORBIDDEN); - return new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()]); + $response = $this->errorHandler->handleAuthorization($authorization); + $this->response->setCode($response->getCode()); + return $response; } return null; } From 2cae4f03beae48ef29393db890726c0e76996a9c Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Thu, 26 Sep 2024 13:31:45 +0200 Subject: [PATCH 04/10] fix test --- tests/Presenters/ApiPresenterTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Presenters/ApiPresenterTest.php b/tests/Presenters/ApiPresenterTest.php index 804ace4..2d194d5 100644 --- a/tests/Presenters/ApiPresenterTest.php +++ b/tests/Presenters/ApiPresenterTest.php @@ -12,6 +12,7 @@ use Tomaj\NetteApi\Authorization\BearerTokenAuthorization; use Tomaj\NetteApi\Authorization\NoAuthorization; use Tomaj\NetteApi\EndpointIdentifier; +use Tomaj\NetteApi\Error\DefaultErrorHandler; use Tomaj\NetteApi\Handlers\AlwaysOkHandler; use Tomaj\NetteApi\Handlers\EchoHandler; use Tomaj\NetteApi\Misc\IpDetector; @@ -41,6 +42,7 @@ public function testSimpleResponse() $presenter->response = new HttpResponse(); $presenter->context = new Container(); $presenter->outputConfigurator = new DebuggerConfigurator(); + $presenter->errorHandler = new DefaultErrorHandler($presenter->outputConfigurator); $request = new Request('Api:Api:default', 'GET', ['version' => '1', 'package' => 'test', 'apiAction' => 'api']); $result = $presenter->run($request); @@ -65,6 +67,7 @@ public function testWithAuthorization() $presenter->response = new HttpResponse(); $presenter->context = new Container(); $presenter->outputConfigurator = new DebuggerConfigurator(); + $presenter->errorHandler = new DefaultErrorHandler($presenter->outputConfigurator); $request = new Request('Api:Api:default', 'GET', ['version' => '1', 'package' => 'test', 'apiAction' => 'api']); $result = $presenter->run($request); @@ -87,6 +90,7 @@ public function testWithParams() $presenter->response = new HttpResponse(); $presenter->context = new Container(); $presenter->outputConfigurator = new DebuggerConfigurator(); + $presenter->errorHandler = new DefaultErrorHandler($presenter->outputConfigurator); Debugger::$productionMode = Debugger::PRODUCTION; @@ -117,6 +121,7 @@ public function testWithOutputs() $presenter->response = new HttpResponse(); $presenter->context = new Container(); $presenter->outputConfigurator = new DebuggerConfigurator(); + $presenter->errorHandler = new DefaultErrorHandler($presenter->outputConfigurator); $request = new Request('Api:Api:default', 'GET', ['version' => '1', 'package' => 'test', 'apiAction' => 'api']); $result = $presenter->run($request); From 7747862ae5b2d446534071a83492b9eb98a6ac60 Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Fri, 27 Sep 2024 10:30:30 +0200 Subject: [PATCH 05/10] Handle authorization exception --- src/Error/DefaultErrorHandler.php | 5 +++++ src/Error/ErrorHandlerInterface.php | 2 ++ src/Presenters/ApiPresenter.php | 10 ++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Error/DefaultErrorHandler.php b/src/Error/DefaultErrorHandler.php index 82cedd9..d156e08 100644 --- a/src/Error/DefaultErrorHandler.php +++ b/src/Error/DefaultErrorHandler.php @@ -58,4 +58,9 @@ public function handleAuthorization(ApiAuthorizationInterface $auth): JsonApiRes { return new JsonApiResponse(Response::S403_FORBIDDEN, ['status' => 'error', 'message' => $auth->getErrorMessage()]); } + + public function handleAuthorizationException(Throwable $exception): JsonApiResponse + { + return new JsonApiResponse(Response::S403_FORBIDDEN, ['status' => 'error', 'message' => $exception->getMessage()]); + } } diff --git a/src/Error/ErrorHandlerInterface.php b/src/Error/ErrorHandlerInterface.php index e90a2e8..0bc89b7 100644 --- a/src/Error/ErrorHandlerInterface.php +++ b/src/Error/ErrorHandlerInterface.php @@ -17,4 +17,6 @@ public function handleInputParams(array $errors): JsonApiResponse; public function handleSchema(array $errors): JsonApiResponse; public function handleAuthorization(ApiAuthorizationInterface $auth): JsonApiResponse; + + public function handleAuthorizationException(Throwable $exception): JsonApiResponse; } diff --git a/src/Presenters/ApiPresenter.php b/src/Presenters/ApiPresenter.php index ffca49d..344d46f 100644 --- a/src/Presenters/ApiPresenter.php +++ b/src/Presenters/ApiPresenter.php @@ -149,8 +149,14 @@ private function getApi(Request $request): Api private function checkAuth(ApiAuthorizationInterface $authorization): ?IResponse { - if (!$authorization->authorized()) { - $response = $this->errorHandler->handleAuthorization($authorization); + try { + if (!$authorization->authorized()) { + $response = $this->errorHandler->handleAuthorization($authorization); + $this->response->setCode($response->getCode()); + return $response; + } + } catch (Throwable $exception) { + $response = $this->errorHandler->handleAuthorizationException($exception); $this->response->setCode($response->getCode()); return $response; } From 55a5570c52d56370bfacbbfbbcd31cf2553d2250 Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Fri, 27 Sep 2024 10:32:36 +0200 Subject: [PATCH 06/10] Fix public configurator --- src/Error/DefaultErrorHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Error/DefaultErrorHandler.php b/src/Error/DefaultErrorHandler.php index d156e08..392fa9f 100644 --- a/src/Error/DefaultErrorHandler.php +++ b/src/Error/DefaultErrorHandler.php @@ -14,7 +14,7 @@ final class DefaultErrorHandler implements ErrorHandlerInterface { /** @var ConfiguratorInterface */ - public $outputConfigurator; + private $outputConfigurator; public function __construct(ConfiguratorInterface $outputConfigurator) { From 1407b3c29217cbbe63f904bdfd700aebcac82e69 Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Tue, 1 Oct 2024 13:32:40 +0200 Subject: [PATCH 07/10] change codes --- src/Error/DefaultErrorHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Error/DefaultErrorHandler.php b/src/Error/DefaultErrorHandler.php index 392fa9f..46056f3 100644 --- a/src/Error/DefaultErrorHandler.php +++ b/src/Error/DefaultErrorHandler.php @@ -56,11 +56,11 @@ public function handleSchema(array $errors): JsonApiResponse public function handleAuthorization(ApiAuthorizationInterface $auth): JsonApiResponse { - return new JsonApiResponse(Response::S403_FORBIDDEN, ['status' => 'error', 'message' => $auth->getErrorMessage()]); + return new JsonApiResponse(Response::S401_UNAUTHORIZED, ['status' => 'error', 'message' => $auth->getErrorMessage()]); } public function handleAuthorizationException(Throwable $exception): JsonApiResponse { - return new JsonApiResponse(Response::S403_FORBIDDEN, ['status' => 'error', 'message' => $exception->getMessage()]); + return new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => $exception->getMessage()]); } } From bed8b170db27b6d8270b351ae713d559c607823d Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Tue, 8 Oct 2024 11:43:00 +0200 Subject: [PATCH 08/10] pass request to error handler --- src/Error/DefaultErrorHandler.php | 11 ++++++----- src/Error/ErrorHandlerInterface.php | 11 ++++++----- src/Presenters/ApiPresenter.php | 14 +++++++------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/Error/DefaultErrorHandler.php b/src/Error/DefaultErrorHandler.php index 46056f3..0461d0b 100644 --- a/src/Error/DefaultErrorHandler.php +++ b/src/Error/DefaultErrorHandler.php @@ -4,6 +4,7 @@ namespace Tomaj\NetteApi\Error; +use Nette\Application\Request; use Nette\Http\Response; use Throwable; use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; @@ -21,7 +22,7 @@ public function __construct(ConfiguratorInterface $outputConfigurator) $this->outputConfigurator = $outputConfigurator; } - public function handle(Throwable $exception): JsonApiResponse + public function handle(Throwable $exception, Request $request): JsonApiResponse { Debugger::log($exception, Debugger::EXCEPTION); if ($this->outputConfigurator->showErrorDetail()) { @@ -32,7 +33,7 @@ public function handle(Throwable $exception): JsonApiResponse return $response; } - public function handleInputParams(array $errors): JsonApiResponse + public function handleInputParams(array $errors, Request $request): JsonApiResponse { if ($this->outputConfigurator->showErrorDetail()) { $response = new JsonApiResponse(Response::S400_BAD_REQUEST, ['status' => 'error', 'message' => 'wrong input', 'detail' => $errors]); @@ -42,7 +43,7 @@ public function handleInputParams(array $errors): JsonApiResponse return $response; } - public function handleSchema(array $errors): JsonApiResponse + public function handleSchema(array $errors, Request $request): JsonApiResponse { Debugger::log($errors, Debugger::ERROR); @@ -54,12 +55,12 @@ public function handleSchema(array $errors): JsonApiResponse return $response; } - public function handleAuthorization(ApiAuthorizationInterface $auth): JsonApiResponse + public function handleAuthorization(ApiAuthorizationInterface $auth, Request $request): JsonApiResponse { return new JsonApiResponse(Response::S401_UNAUTHORIZED, ['status' => 'error', 'message' => $auth->getErrorMessage()]); } - public function handleAuthorizationException(Throwable $exception): JsonApiResponse + public function handleAuthorizationException(Throwable $exception, Request $request): JsonApiResponse { return new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => $exception->getMessage()]); } diff --git a/src/Error/ErrorHandlerInterface.php b/src/Error/ErrorHandlerInterface.php index 0bc89b7..6fd6085 100644 --- a/src/Error/ErrorHandlerInterface.php +++ b/src/Error/ErrorHandlerInterface.php @@ -6,17 +6,18 @@ use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; use Tomaj\NetteApi\Response\JsonApiResponse; +use Nette\Application\Request; use Throwable; interface ErrorHandlerInterface { - public function handle(Throwable $error): JsonApiResponse; + public function handle(Throwable $error, Request $request): JsonApiResponse; - public function handleInputParams(array $errors): JsonApiResponse; + public function handleInputParams(array $errors, Request $request): JsonApiResponse; - public function handleSchema(array $errors): JsonApiResponse; + public function handleSchema(array $errors, Request $request): JsonApiResponse; - public function handleAuthorization(ApiAuthorizationInterface $auth): JsonApiResponse; + public function handleAuthorization(ApiAuthorizationInterface $auth, Request $request): JsonApiResponse; - public function handleAuthorizationException(Throwable $exception): JsonApiResponse; + public function handleAuthorizationException(Throwable $exception, Request $request): JsonApiResponse; } diff --git a/src/Presenters/ApiPresenter.php b/src/Presenters/ApiPresenter.php index 344d46f..27c19a4 100644 --- a/src/Presenters/ApiPresenter.php +++ b/src/Presenters/ApiPresenter.php @@ -76,7 +76,7 @@ public function run(Request $request): IResponse $authorization = $api->getAuthorization(); $rateLimit = $api->getRateLimit(); - $authResponse = $this->checkAuth($authorization); + $authResponse = $this->checkAuth($authorization, $request); if ($authResponse !== null) { return $authResponse; } @@ -88,7 +88,7 @@ public function run(Request $request): IResponse $paramsProcessor = new ParamsProcessor($handler->params()); if ($paramsProcessor->isError()) { - $response = $this->errorHandler->handleInputParams($paramsProcessor->getErrors()); + $response = $this->errorHandler->handleInputParams($paramsProcessor->getErrors(), $request); $this->response->setCode($response->getCode()); return $response; } @@ -115,12 +115,12 @@ public function run(Request $request): IResponse $outputValidatorErrors[] = $validationResult->getErrors(); } if (!$outputValid) { - $response = $this->errorHandler->handleSchema($outputValidatorErrors); + $response = $this->errorHandler->handleSchema($outputValidatorErrors, $request); $code = $response->getCode(); } } } catch (Throwable $exception) { - $response = $this->errorHandler->handle($exception); + $response = $this->errorHandler->handle($exception, $request); $code = $response->getCode(); } @@ -147,16 +147,16 @@ private function getApi(Request $request): Api ); } - private function checkAuth(ApiAuthorizationInterface $authorization): ?IResponse + private function checkAuth(ApiAuthorizationInterface $authorization, Request $request): ?IResponse { try { if (!$authorization->authorized()) { - $response = $this->errorHandler->handleAuthorization($authorization); + $response = $this->errorHandler->handleAuthorization($authorization, $request); $this->response->setCode($response->getCode()); return $response; } } catch (Throwable $exception) { - $response = $this->errorHandler->handleAuthorizationException($exception); + $response = $this->errorHandler->handleAuthorizationException($exception, $request); $this->response->setCode($response->getCode()); return $response; } From fc80ab49550dd429f398d2a28c6a1dd1e9a2b2e1 Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Tue, 8 Oct 2024 13:25:52 +0200 Subject: [PATCH 09/10] push params to error handler --- src/Error/DefaultErrorHandler.php | 11 ++++----- src/Error/ErrorHandlerInterface.php | 36 +++++++++++++++++++++-------- src/Presenters/ApiPresenter.php | 25 ++++++++++---------- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/Error/DefaultErrorHandler.php b/src/Error/DefaultErrorHandler.php index 0461d0b..9dcb512 100644 --- a/src/Error/DefaultErrorHandler.php +++ b/src/Error/DefaultErrorHandler.php @@ -4,7 +4,6 @@ namespace Tomaj\NetteApi\Error; -use Nette\Application\Request; use Nette\Http\Response; use Throwable; use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; @@ -22,7 +21,7 @@ public function __construct(ConfiguratorInterface $outputConfigurator) $this->outputConfigurator = $outputConfigurator; } - public function handle(Throwable $exception, Request $request): JsonApiResponse + public function handle(Throwable $exception, array $params): JsonApiResponse { Debugger::log($exception, Debugger::EXCEPTION); if ($this->outputConfigurator->showErrorDetail()) { @@ -33,7 +32,7 @@ public function handle(Throwable $exception, Request $request): JsonApiResponse return $response; } - public function handleInputParams(array $errors, Request $request): JsonApiResponse + public function handleInputParams(array $errors): JsonApiResponse { if ($this->outputConfigurator->showErrorDetail()) { $response = new JsonApiResponse(Response::S400_BAD_REQUEST, ['status' => 'error', 'message' => 'wrong input', 'detail' => $errors]); @@ -43,7 +42,7 @@ public function handleInputParams(array $errors, Request $request): JsonApiRespo return $response; } - public function handleSchema(array $errors, Request $request): JsonApiResponse + public function handleSchema(array $errors, array $params): JsonApiResponse { Debugger::log($errors, Debugger::ERROR); @@ -55,12 +54,12 @@ public function handleSchema(array $errors, Request $request): JsonApiResponse return $response; } - public function handleAuthorization(ApiAuthorizationInterface $auth, Request $request): JsonApiResponse + public function handleAuthorization(ApiAuthorizationInterface $auth, array $params): JsonApiResponse { return new JsonApiResponse(Response::S401_UNAUTHORIZED, ['status' => 'error', 'message' => $auth->getErrorMessage()]); } - public function handleAuthorizationException(Throwable $exception, Request $request): JsonApiResponse + public function handleAuthorizationException(Throwable $exception, array $params): JsonApiResponse { return new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => $exception->getMessage()]); } diff --git a/src/Error/ErrorHandlerInterface.php b/src/Error/ErrorHandlerInterface.php index 6fd6085..632bd16 100644 --- a/src/Error/ErrorHandlerInterface.php +++ b/src/Error/ErrorHandlerInterface.php @@ -6,18 +6,34 @@ use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; use Tomaj\NetteApi\Response\JsonApiResponse; -use Nette\Application\Request; use Throwable; interface ErrorHandlerInterface { - public function handle(Throwable $error, Request $request): JsonApiResponse; - - public function handleInputParams(array $errors, Request $request): JsonApiResponse; - - public function handleSchema(array $errors, Request $request): JsonApiResponse; - - public function handleAuthorization(ApiAuthorizationInterface $auth, Request $request): JsonApiResponse; - - public function handleAuthorizationException(Throwable $exception, Request $request): JsonApiResponse; + /** + * @param array $params + */ + public function handle(Throwable $error, array $params): JsonApiResponse; + + /** + * @param array $errors + * @param array $params + */ + public function handleInputParams(array $errors): JsonApiResponse; + + /** + * @param array $errors + * @param array $params + */ + public function handleSchema(array $errors, array $params): JsonApiResponse; + + /** + * @param array $params + */ + public function handleAuthorization(ApiAuthorizationInterface $auth, array $params): JsonApiResponse; + + /** + * @param array $params + */ + public function handleAuthorizationException(Throwable $exception, array $params): JsonApiResponse; } diff --git a/src/Presenters/ApiPresenter.php b/src/Presenters/ApiPresenter.php index 27c19a4..ea32b33 100644 --- a/src/Presenters/ApiPresenter.php +++ b/src/Presenters/ApiPresenter.php @@ -73,14 +73,10 @@ public function run(Request $request): IResponse $api = $this->getApi($request); $handler = $api->getHandler(); + $authorization = $api->getAuthorization(); $rateLimit = $api->getRateLimit(); - $authResponse = $this->checkAuth($authorization, $request); - if ($authResponse !== null) { - return $authResponse; - } - $rateLimitResponse = $this->checkRateLimit($rateLimit); if ($rateLimitResponse !== null) { return $rateLimitResponse; @@ -88,12 +84,17 @@ public function run(Request $request): IResponse $paramsProcessor = new ParamsProcessor($handler->params()); if ($paramsProcessor->isError()) { - $response = $this->errorHandler->handleInputParams($paramsProcessor->getErrors(), $request); + $response = $this->errorHandler->handleInputParams($paramsProcessor->getErrors()); $this->response->setCode($response->getCode()); return $response; } - $params = $paramsProcessor->getValues(); + + $authResponse = $this->checkAuth($authorization, $params); + if ($authResponse !== null) { + return $authResponse; + } + try { $response = $handler->handle($params); $code = $response->getCode(); @@ -115,12 +116,12 @@ public function run(Request $request): IResponse $outputValidatorErrors[] = $validationResult->getErrors(); } if (!$outputValid) { - $response = $this->errorHandler->handleSchema($outputValidatorErrors, $request); + $response = $this->errorHandler->handleSchema($outputValidatorErrors, $params); $code = $response->getCode(); } } } catch (Throwable $exception) { - $response = $this->errorHandler->handle($exception, $request); + $response = $this->errorHandler->handle($exception, $params); $code = $response->getCode(); } @@ -147,16 +148,16 @@ private function getApi(Request $request): Api ); } - private function checkAuth(ApiAuthorizationInterface $authorization, Request $request): ?IResponse + private function checkAuth(ApiAuthorizationInterface $authorization, array $params): ?IResponse { try { if (!$authorization->authorized()) { - $response = $this->errorHandler->handleAuthorization($authorization, $request); + $response = $this->errorHandler->handleAuthorization($authorization, $params); $this->response->setCode($response->getCode()); return $response; } } catch (Throwable $exception) { - $response = $this->errorHandler->handleAuthorizationException($exception, $request); + $response = $this->errorHandler->handleAuthorizationException($exception, $params); $this->response->setCode($response->getCode()); return $response; } From fa508c05ae7c77fdfc48a3ce7ace337f6be3662e Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Thu, 14 Nov 2024 09:47:41 +0100 Subject: [PATCH 10/10] fix naming --- src/Error/ErrorHandlerInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Error/ErrorHandlerInterface.php b/src/Error/ErrorHandlerInterface.php index 632bd16..91974e4 100644 --- a/src/Error/ErrorHandlerInterface.php +++ b/src/Error/ErrorHandlerInterface.php @@ -13,7 +13,7 @@ interface ErrorHandlerInterface /** * @param array $params */ - public function handle(Throwable $error, array $params): JsonApiResponse; + public function handle(Throwable $exception, array $params): JsonApiResponse; /** * @param array $errors