diff --git a/app/src/Middleware/NotAllowed.php b/app/src/Middleware/NotAllowed.php index ae17038..09d192d 100644 --- a/app/src/Middleware/NotAllowed.php +++ b/app/src/Middleware/NotAllowed.php @@ -5,18 +5,25 @@ use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use Psr\Log\LoggerInterface; use Slim\Exception\HttpMethodNotAllowedException; +use Incoviba\Common\Alias\View; class NotAllowed { - public function __construct(protected ResponseFactoryInterface $responseFactory) {} + public function __construct(protected LoggerInterface $logger, protected ResponseFactoryInterface $responseFactory, + protected View $view) {} public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { try { return $handler->handle($request); } catch (HttpMethodNotAllowedException) { - return $this->responseFactory->createResponse(405, 'Method Not Allowed'); + $response = $this->responseFactory->createResponse(405, 'Method Not Allowed'); + if (str_contains($request->getUri()->getPath(), '/api')) { + return $response; + } + return $this->view->render($response, 'not_allowed'); } } } diff --git a/app/src/Middleware/NotFound.php b/app/src/Middleware/NotFound.php index abccf49..53cb4fa 100644 --- a/app/src/Middleware/NotFound.php +++ b/app/src/Middleware/NotFound.php @@ -8,17 +8,21 @@ use Psr\Http\Server\RequestHandlerInterface; use Psr\Log\LoggerInterface; use Slim\Exception\HttpNotFoundException; use Incoviba\Common\Alias\View; +use Incoviba\Common\Implement\Exception\{EmptyRedis,EmptyResult}; +use Incoviba\Exception\ServiceAction\{Create, Delete, Read, Update}; class NotFound { - public function __construct(protected LoggerInterface $logger, protected ResponseFactoryInterface $responseFactory, protected View $view) {} + public function __construct(protected LoggerInterface $logger, protected ResponseFactoryInterface $responseFactory, + protected View $view) {} public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { try { return $handler->handle($request); - } catch (HttpNotFoundException $exception) { + } catch (HttpNotFoundException | + EmptyRedis | EmptyResult | Read | Create | Update | Delete $exception) { $this->logger->notice($exception); - $response = $this->responseFactory->createResponse(404); + $response = $this->responseFactory->createResponse(404, 'Not Found'); if (str_contains($request->getUri()->getPath(), '/api')) { return $response; }