Skip to content

Commit

Permalink
Merge pull request #1 from kerneldemon/changed-type-for-invoke-method
Browse files Browse the repository at this point in the history
Changed type for invoke method
  • Loading branch information
dopesong authored Aug 16, 2016
2 parents c21c897 + dd56b41 commit 0fd6b51
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,40 @@ include "vendor/autoload.php";
$app = new Slim\App();
$container = $app->getContainer();

$container['errorHandler'] = function($c) {
$container['phpErrorHandler'] = $container['errorHandler'] = function($c) {
return new WhoopsError($c->get('settings')['displayErrorDetails']);
};

$app->run();
```
```

## Additional handlers

Custom handlers can be added to execute additional tasks.
For example, you might want to log the error like so:

```php
include "vendor/autoload.php";

use Whoops\Handler\Handler;
use Dopesong\Slim\Error\Whoops as WhoopsError;

$app = new Slim\App();
$container = $app->getContainer();

$container['phpErrorHandler'] = $container['errorHandler'] = function ($container) {
$logger = $container['logger'];
$whoopsHandler = new WhoopsError();

$whoopsHandler->pushHandler(
function ($exception) use ($logger) {
/** @var \Exception $exception */
$logger->error($exception->getMessage(), ['exception' => $exception]);
return Handler::DONE;
}
);

return $whoopsHandler;
};
```

7 changes: 3 additions & 4 deletions src/Whoops.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Exception;

/**
* Whoops Error Handler
Expand Down Expand Up @@ -68,19 +67,19 @@ public function pushHandler($handler)
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param Exception $exception
* @param \Throwable $throwable
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $throwable)
{
$contentType = $this->determineContentType($request);

$this->pushHandlerByContentType($contentType);

$output = null;

$output = $this->whoops->handleException($exception);
$output = $this->whoops->handleException($throwable);

$body = $response->getBody();
$body->write($output);
Expand Down

0 comments on commit 0fd6b51

Please sign in to comment.