Skip to content

Commit 03b4802

Browse files
committed
Optional support for IPub\SlimRouter
1 parent b11a282 commit 03b4802

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/Middleware/JsonApiMiddleware.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use FastyBird\JsonApi\Exceptions;
1919
use FastyBird\JsonApi\JsonApi;
2020
use Fig\Http\Message\StatusCodeInterface;
21+
use IPub;
2122
use Neomerx;
2223
use Neomerx\JsonApi\Contracts;
2324
use Neomerx\JsonApi\Schema;
@@ -27,6 +28,10 @@
2728
use Psr\Log;
2829
use Throwable;
2930

31+
if (!class_exists('IPub\SlimRouter\Exceptions\HttpException')) {
32+
class_alias('IPub\SlimRouter\Exceptions\HttpException', Throwable::class);
33+
}
34+
3035
/**
3136
* {JSON:API} formatting output handling middleware
3237
*
@@ -74,6 +79,7 @@ public function process(Message\ServerRequestInterface $request, Server\RequestH
7479
return $handler->handle($request);
7580

7681
} catch (Throwable $ex) {
82+
var_dump($ex->getMessage());
7783
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_BAD_REQUEST);
7884

7985
if ($ex instanceof Exceptions\IJsonApiException) {
@@ -90,6 +96,22 @@ public function process(Message\ServerRequestInterface $request, Server\RequestH
9096

9197
$response->getBody()->write($content);
9298
}
99+
} elseif ($ex instanceof IPub\SlimRouter\Exceptions\HttpException) {
100+
$response = $response->withStatus($ex->getCode());
101+
102+
$content = $this->getEncoder()
103+
->encodeError(new Schema\Error(
104+
null,
105+
null,
106+
null,
107+
(string) $ex->getCode(),
108+
(string) $ex->getCode(),
109+
$ex->getTitle(),
110+
$ex->getDescription()
111+
));
112+
113+
$response->getBody()
114+
->write($content);
93115
} else {
94116
$this->logger->error('[FB::JSON_API] An error occurred during request handling', [
95117
'exception' => [

0 commit comments

Comments
 (0)