-
Notifications
You must be signed in to change notification settings - Fork 22
Custom error handler #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom error handler #153
Changes from all commits
4758b75
3e21a42
dc6facc
2cae4f0
7747862
55a5570
1407b3c
bed8b17
fc80ab4
fa508c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tomaj\NetteApi\Error; | ||
|
|
||
| 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; | ||
|
|
||
| final class DefaultErrorHandler implements ErrorHandlerInterface | ||
| { | ||
| /** @var ConfiguratorInterface */ | ||
| private $outputConfigurator; | ||
|
|
||
| public function __construct(ConfiguratorInterface $outputConfigurator) | ||
| { | ||
| $this->outputConfigurator = $outputConfigurator; | ||
| } | ||
|
|
||
| public function handle(Throwable $exception, array $params): JsonApiResponse | ||
| { | ||
| Debugger::log($exception, Debugger::EXCEPTION); | ||
| if ($this->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; | ||
| } | ||
|
|
||
| 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, array $params): 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, array $params): JsonApiResponse | ||
| { | ||
| return new JsonApiResponse(Response::S401_UNAUTHORIZED, ['status' => 'error', 'message' => $auth->getErrorMessage()]); | ||
| } | ||
|
|
||
| public function handleAuthorizationException(Throwable $exception, array $params): JsonApiResponse | ||
| { | ||
| return new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => $exception->getMessage()]); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tomaj\NetteApi\Error; | ||
|
|
||
| use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; | ||
| use Tomaj\NetteApi\Response\JsonApiResponse; | ||
| use Throwable; | ||
|
|
||
| interface ErrorHandlerInterface | ||
| { | ||
| /** | ||
| * @param array<mixed> $params | ||
| */ | ||
| public function handle(Throwable $exception, 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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,14 +14,13 @@ | |
| use Tomaj\NetteApi\Api; | ||
| use Tomaj\NetteApi\ApiDecider; | ||
| use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; | ||
| use Tomaj\NetteApi\Error\ErrorHandlerInterface; | ||
| use Tomaj\NetteApi\Logger\ApiLoggerInterface; | ||
| use Tomaj\NetteApi\Misc\IpDetectorInterface; | ||
| use Tomaj\NetteApi\Output\Configurator\ConfiguratorInterface; | ||
| use Tomaj\NetteApi\Output\OutputInterface; | ||
| use Tomaj\NetteApi\Params\ParamsProcessor; | ||
| use Tomaj\NetteApi\RateLimit\RateLimitInterface; | ||
| use Tomaj\NetteApi\Response\JsonApiResponse; | ||
| use Tracy\Debugger; | ||
| use Tomaj\NetteApi\Output\Configurator\ConfiguratorInterface; | ||
|
|
||
| final class ApiPresenter implements IPresenter | ||
| { | ||
|
|
@@ -37,6 +36,9 @@ final class ApiPresenter implements IPresenter | |
| /** @var ConfiguratorInterface @inject */ | ||
| public $outputConfigurator; | ||
|
|
||
| /** @var ErrorHandlerInterface @inject */ | ||
| public $errorHandler; | ||
|
|
||
| /** | ||
| * CORS header settings | ||
| * | ||
|
|
@@ -71,30 +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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. preco sme presunuli autorizaciu az za parametre a rate limit?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lebo treba spracovat parametre aby som mal dostupny Context do erroru. Nepacilo sa mi to ale lepsie som nevymyslel.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm už si spomínam. Len teraz úplne zmeníme výstupy - ak niečo predtým padlo už na autorizácii, alebo rate limite parametre sa vôbec neriešili. Teraz to bude naopak a neviem povedať ci korektne. |
||
| if ($authResponse !== null) { | ||
| return $authResponse; | ||
| } | ||
|
|
||
| $rateLimitResponse = $this->checkRateLimit($rateLimit); | ||
| if ($rateLimitResponse !== null) { | ||
| return $rateLimitResponse; | ||
| } | ||
|
|
||
| $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(); | ||
|
|
||
| $authResponse = $this->checkAuth($authorization, $params); | ||
| if ($authResponse !== null) { | ||
| return $authResponse; | ||
| } | ||
|
|
||
| try { | ||
| $response = $handler->handle($params); | ||
| $code = $response->getCode(); | ||
|
|
@@ -116,18 +116,13 @@ 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, $params); | ||
| $code = $response->getCode(); | ||
| } | ||
| } | ||
| } catch (Throwable $exception) { | ||
| if ($this->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, $params); | ||
| $code = $response->getCode(); | ||
| Debugger::log($exception, Debugger::EXCEPTION); | ||
| } | ||
|
|
||
| $end = microtime(true); | ||
|
|
@@ -153,11 +148,18 @@ private function getApi(Request $request): Api | |
| ); | ||
| } | ||
|
|
||
| private function checkAuth(ApiAuthorizationInterface $authorization): ?IResponse | ||
| private function checkAuth(ApiAuthorizationInterface $authorization, array $params): ?IResponse | ||
| { | ||
| if (!$authorization->authorized()) { | ||
| $this->response->setCode(Response::S403_FORBIDDEN); | ||
| return new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()]); | ||
| try { | ||
| if (!$authorization->authorized()) { | ||
| $response = $this->errorHandler->handleAuthorization($authorization, $params); | ||
| $this->response->setCode($response->getCode()); | ||
| return $response; | ||
| } | ||
| } catch (Throwable $exception) { | ||
| $response = $this->errorHandler->handleAuthorizationException($exception, $params); | ||
| $this->response->setCode($response->getCode()); | ||
| return $response; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.