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; }