Skip to content

Commit

Permalink
TryCatchMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jun 26, 2018
1 parent 5a00eac commit 9efd577
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/TryCatchMiddleware.php
@@ -0,0 +1,39 @@
<?php declare(strict_types = 1);

namespace Contributte\Middlewares;

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

class TryCatchMiddleware extends BaseMiddleware
{

/** @var bool */
private $enable = true;

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
{
if ($this->enable) {
try {
return $next($request, $response);
} catch (Throwable $throwable) {
echo sprintf('Application encountered an internal error with status code "500" and with message "%s".', $throwable->getMessage());
exit($throwable->getCode());
}
}

return $next($request, $response);
}

public function enable(): void
{
$this->enable = true;
}

public function disable(): void
{
$this->enable = false;
}

}

0 comments on commit 9efd577

Please sign in to comment.