Skip to content

Commit

Permalink
push params to error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Beranek committed Oct 8, 2024
1 parent bed8b17 commit fc80ab4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
11 changes: 5 additions & 6 deletions src/Error/DefaultErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Tomaj\NetteApi\Error;

use Nette\Application\Request;
use Nette\Http\Response;
use Throwable;
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
Expand All @@ -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()) {
Expand All @@ -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]);
Expand All @@ -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);

Expand All @@ -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()]);
}
Expand Down
36 changes: 26 additions & 10 deletions src/Error/ErrorHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $params
*/
public function handle(Throwable $error, array $params): JsonApiResponse;

/**
* @param array<string> $errors
* @param array<mixed> $params
*/
public function handleInputParams(array $errors): JsonApiResponse;

/**
* @param array<string> $errors
* @param array<mixed> $params
*/
public function handleSchema(array $errors, array $params): JsonApiResponse;

/**
* @param array<mixed> $params
*/
public function handleAuthorization(ApiAuthorizationInterface $auth, array $params): JsonApiResponse;

/**
* @param array<mixed> $params
*/
public function handleAuthorizationException(Throwable $exception, array $params): JsonApiResponse;
}
25 changes: 13 additions & 12 deletions src/Presenters/ApiPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,28 @@ 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;
}

$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();
Expand All @@ -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();
}

Expand All @@ -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;
}
Expand Down

0 comments on commit fc80ab4

Please sign in to comment.