Manejo de excepciones

This commit is contained in:
Juan Pablo Vial
2025-04-22 13:07:31 -04:00
parent ed96f25475
commit 5147450ed6
5 changed files with 105 additions and 26 deletions

View File

@ -4,9 +4,11 @@ namespace Incoviba\Controller\API\Ventas;
use PDOException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Controller\API\{withJson, emptyBody};
use Incoviba\Controller\withRedis;
use Incoviba\Common\Implement\Exception\{EmptyResult,EmptyRedis};
use Incoviba\Exception\ServiceAction\{Read, Create, Update};
use Incoviba\Repository;
use Incoviba\Service;
@ -14,7 +16,9 @@ class PropiedadesUnidades
{
use emptyBody, withJson, withRedis;
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface
public function add(ServerRequestInterface $request, ResponseInterface $response,
LoggerInterface $logger,
Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
@ -26,8 +30,8 @@ class PropiedadesUnidades
$pu = $propiedadUnidadService->add($body);
$output['propiedad_unidad'] = $pu;
$output['added'] = true;
} catch (EmptyResult $exception) {
error_log($exception);
} catch (Read | Create $exception) {
$logger->notice($exception);
}
return $this->withJson($response, $output);
}
@ -44,7 +48,7 @@ class PropiedadesUnidades
$pu = $propiedadUnidadService->getById($pu_id);
$propiedadUnidadService->edit($pu, (array) $body);
$output['edited'] = true;
} catch (PDOException | EmptyResult) {}
} catch (PDOException | Read | EmptyResult | Update) {}
return $this->withJson($response, $output);
}
public function remove(ServerRequestInterface $request, ResponseInterface $response,
@ -58,7 +62,7 @@ class PropiedadesUnidades
$pu = $propiedadUnidadRepository->fetchById($pu_id);
$propiedadUnidadRepository->remove($pu);
$output['removed'] = true;
} catch (EmptyResult) {}
} catch (PDOException | EmptyResult) {}
return $this->withJson($response, $output);
}
}