summaryrefslogtreecommitdiff
path: root/src/Middleware/ExceptionHandler.php
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-03 22:43:19 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-03 22:55:54 +0200
commit36dafdb68acbde2fe42ce36ef50f497c8c06411f (patch)
tree1e420597ae72c979361bf29b66ae7e27c73cf431 /src/Middleware/ExceptionHandler.php
parent9f1ee0c6c6497d43fb275491ec53fda420f64b81 (diff)
parentb0e7bc0df2eb4975223582089c7a928903e8cd14 (diff)
Merge remote-tracking branch 'MyIgel/rebuild-psr7'
Diffstat (limited to 'src/Middleware/ExceptionHandler.php')
-rw-r--r--src/Middleware/ExceptionHandler.php48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/Middleware/ExceptionHandler.php b/src/Middleware/ExceptionHandler.php
new file mode 100644
index 00000000..a5db0337
--- /dev/null
+++ b/src/Middleware/ExceptionHandler.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Engelsystem\Middleware;
+
+use Engelsystem\Exceptions\Handler as ExceptionsHandler;
+use Psr\Container\ContainerInterface;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+class ExceptionHandler implements MiddlewareInterface
+{
+ /** @var ContainerInterface */
+ protected $container;
+
+ /**
+ * @param ContainerInterface $container
+ */
+ public function __construct(ContainerInterface $container)
+ {
+ $this->container = $container;
+ }
+
+ /**
+ * Handles any exceptions that occurred inside other middleware while returning it to the default response handler
+ *
+ * Should be added at the beginning
+ *
+ * @param ServerRequestInterface $request
+ * @param RequestHandlerInterface $handler
+ * @return ResponseInterface
+ */
+ public function process(
+ ServerRequestInterface $request,
+ RequestHandlerInterface $handler
+ ): ResponseInterface {
+ try {
+ return $handler->handle($request);
+ } catch (\Throwable $e) {
+ /** @var ExceptionsHandler $handler */
+ $handler = $this->container->get('error.handler');
+ $content = $handler->exceptionHandler($e, true);
+
+ return response($content, 500);
+ }
+ }
+}