Files
oficial/app/src/Middleware/NotFound.php
2025-06-03 11:52:31 -04:00

33 lines
1.3 KiB
PHP

<?php
namespace Incoviba\Middleware;
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\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 __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
return $handler->handle($request);
} catch (HttpNotFoundException |
EmptyRedis | EmptyResult | Read | Create | Update | Delete $exception) {
$this->logger->notice($exception);
$response = $this->responseFactory->createResponse(404, 'Not Found');
if (str_contains($request->getUri()->getPath(), '/api')) {
return $response;
}
return $this->view->render($response, 'not_found');
}
}
}