Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |