Skip to content
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

Merged
merged 10 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/Error/DefaultErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi\Error;

use Nette\Http\Response;
use Throwable;
use Tomaj\NetteApi\Output\Configurator\ConfiguratorInterface;
use Tomaj\NetteApi\Response\JsonApiResponse;
use Tracy\Debugger;

final class DefaultErrorHandler implements ErrorHandlerInterface
{
/** @var ConfiguratorInterface @inject */
public $outputConfigurator;
Martin-Beranek marked this conversation as resolved.
Show resolved Hide resolved

public function handle(Throwable $exception): 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;
}
}
13 changes: 13 additions & 0 deletions src/Error/ErrorHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi\Error;

use Tomaj\NetteApi\Response\JsonApiResponse;
use Throwable;

interface ErrorHandlerInterface
{
public function handle(Throwable $error): JsonApiResponse;
}
13 changes: 6 additions & 7 deletions src/Presenters/ApiPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
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
{
Expand All @@ -37,6 +38,9 @@ final class ApiPresenter implements IPresenter
/** @var ConfiguratorInterface @inject */
public $outputConfigurator;

/** @var ErrorHandlerInterface @inject */
public $errorHandler;

/**
* CORS header settings
*
Expand Down Expand Up @@ -121,13 +125,8 @@ public function run(Request $request): IResponse
}
}
} 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);
$code = $response->getCode();
Debugger::log($exception, Debugger::EXCEPTION);
}

$end = microtime(true);
Expand Down
Loading