summaryrefslogtreecommitdiff
path: root/src/Middleware
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-11-13 17:48:07 +0100
committermsquare <msquare@notrademark.de>2018-12-02 12:53:31 +0100
commit9788c5095a67a45fe3545ae0fc747b8e629ea4fd (patch)
treeac6edc92fcf08c40d4b517529a76ab9f230829f0 /src/Middleware
parent2588bbf7bc5374830662af7c01df688c42d30dc6 (diff)
Implemented HttpException
Diffstat (limited to 'src/Middleware')
-rw-r--r--src/Middleware/ErrorHandler.php21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/Middleware/ErrorHandler.php b/src/Middleware/ErrorHandler.php
index a7c4cfe6..c99ac24f 100644
--- a/src/Middleware/ErrorHandler.php
+++ b/src/Middleware/ErrorHandler.php
@@ -2,6 +2,7 @@
namespace Engelsystem\Middleware;
+use Engelsystem\Http\Exceptions\HttpException;
use Engelsystem\Http\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@@ -38,7 +39,11 @@ class ErrorHandler implements MiddlewareInterface
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
- $response = $handler->handle($request);
+ try {
+ $response = $handler->handle($request);
+ } catch (HttpException $e) {
+ $response = $this->createResponse($e->getMessage(), $e->getStatusCode(), $e->getHeaders());
+ }
$statusCode = $response->getStatusCode();
if ($statusCode < 400 || !$response instanceof Response) {
@@ -77,4 +82,18 @@ class ErrorHandler implements MiddlewareInterface
return 'default';
}
+
+ /**
+ * Create a new response
+ *
+ * @param string $content
+ * @param int $status
+ * @param array $headers
+ * @return Response
+ * @codeCoverageIgnore
+ */
+ protected function createResponse(string $content = '', int $status = 200, array $headers = [])
+ {
+ return response($content, $status, $headers);
+ }
}