Skip to content

Commit

Permalink
feat: add exception handling for the FlashBagException from araise/co…
Browse files Browse the repository at this point in the history
…re-bundle
  • Loading branch information
Ruesa18 committed Jun 27, 2024
1 parent 7727b00 commit 3caffb6
Showing 1 changed file with 68 additions and 32 deletions.
100 changes: 68 additions & 32 deletions src/Controller/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use araise\CrudBundle\Block\Block;
use araise\CrudBundle\Content\RelationContent;
use araise\CoreBundle\Exception\FlashBagExecption;
use araise\CrudBundle\Definition\DefinitionInterface;
use araise\CrudBundle\Enums\Page;
use araise\CrudBundle\Enums\PageInterface;
Expand Down Expand Up @@ -171,14 +172,22 @@ public function editAction(Request $request): Response

$form = $view->getEditForm();

$form->handleRequest($request);
try {
$form->handleRequest($request);

if ($form->isSubmitted()) {
$this->dispatchEvent(CrudEvent::PRE_VALIDATE_PREFIX, $entity);
if ($form->isValid()) {
return $this->formSubmittedAndValid($entity, $mode, Page::EDIT);
if ($form->isSubmitted()) {
$this->dispatchEvent(CrudEvent::PRE_VALIDATE_PREFIX, $entity);
if ($form->isValid()) {
return $this->formSubmittedAndValid($entity, $mode, Page::EDIT);
}
throw new FlashBagExecption('error', 'araise_crud.save_error');
}
$this->addFlash('error', 'araise_crud.save_error');
} catch (FlashBagExecption $e) {
$this->addFlash($e->getFlashType(), $e->getFlashMessage());
$this->container->get(LoggerInterface::class)->warning('Error while editing: '.$e->getMessage(), [
'entity' => get_class($entity),
'id' => $entity->getId(),
]);
}

$this->definition->buildBreadcrumbs($entity, Page::EDIT);
Expand Down Expand Up @@ -221,14 +230,22 @@ public function createAction(Request $request): Response

$form = $view->getCreateForm();

$form->handleRequest($request);
try {
$form->handleRequest($request);

if ($form->isSubmitted()) {
$this->dispatchEvent(CrudEvent::PRE_VALIDATE_PREFIX, $entity);
if ($form->isValid()) {
return $this->formSubmittedAndValid($entity, $mode, Page::CREATE);
if ($form->isSubmitted()) {
$this->dispatchEvent(CrudEvent::PRE_VALIDATE_PREFIX, $entity);
if ($form->isValid()) {
return $this->formSubmittedAndValid($entity, $mode, Page::CREATE);
}
throw new FlashBagExecption('error', 'araise_crud.save_error');
}
$this->addFlash('error', 'araise_crud.save_error');
} catch (FlashBagExecption $e) {
$this->addFlash($e->getFlashType(), $e->getFlashMessage());
$this->container->get(LoggerInterface::class)->warning('Error while creating: '.$e->getMessage(), [
'entity' => get_class($entity),
'id' => $entity->getId(),
]);
}

$this->definition->buildBreadcrumbs($entity, Page::CREATE);
Expand Down Expand Up @@ -259,11 +276,19 @@ public function deleteAction(Request $request): Response
try {
$this->entityManager->remove($entity);
$this->dispatchEvent(CrudEvent::PRE_DELETE_PREFIX, $entity);
$this->entityManager->flush();
try {
$this->entityManager->flush();
} catch (\Exception $e) {
$this->container->get(LoggerInterface::class)->warning('Error while deleting: '.$e->getMessage(), [
'entity' => get_class($entity),
'id' => $entity->getId(),
]);
throw new FlashBagExecption('error', 'araise_crud.delete_error');
}
$this->dispatchEvent(CrudEvent::POST_DELETE_PREFIX, $entity);
$this->addFlash('success', 'araise_crud.delete_success');
} catch (\Exception $e) {
$this->addFlash('error', 'araise_crud.delete_error');
} catch (FlashBagExecption $e) {
$this->addFlash($e->getFlashType(), $e->getFlashMessage());
$this->container->get(LoggerInterface::class)->warning('Error while deleting: '.$e->getMessage(), [
'entity' => get_class($entity),
'id' => $entity->getId(),
Expand Down Expand Up @@ -329,26 +354,37 @@ public function exportAction(Request $request, TableFactory $tableFactory, Table
if ($request->query->getInt('all', 0) === 1) {
$table->getExtension(PaginationExtension::class)?->setLimit(0);
}
$exporter = $table->getExporter($request->query->getString('exporter', 'table'));
if (!$exporter && count($table->getExporters()) > 0) {
$exporter = $table->getExporter(key($table->getExporters()));
}
if (!$exporter instanceof ExporterInterface) {
$this->addFlash('error', 'araise_crud.export_error');
throw new \RuntimeException('No Exporter found.');
}
$spreadsheet = $exporter->createSpreadsheet($table);
$writer = new Xlsx($spreadsheet);
$response = new StreamedResponse();
$response->setCallback(
function () use ($writer) {
$writer->save('php://output');

try {
$exporter = $table->getExporter($request->query->getString('exporter', 'table'));
if (!$exporter && count($table->getExporters()) > 0) {
$exporter = $table->getExporter(key($table->getExporters()));
}
);
if (!$exporter instanceof ExporterInterface) {
throw new FlashBagExecption('error', 'araise_crud.export_error', 'No Exporter found.');
}
$spreadsheet = $exporter->createSpreadsheet($table);
$writer = new Xlsx($spreadsheet);
$response = new StreamedResponse();
$response->setCallback(
function () use ($writer) {
$writer->save('php://output');
}
);

$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->headers->set('Content-Disposition', 'attachment; filename="'.$this->definition->getExportFilename().'"');
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $this->definition->getExportFilename() . '"');
}catch (FlashBagExecption $e) {
$this->addFlash($e->getFlashType(), $e->getFlashMessage());
$this->container->get(LoggerInterface::class)->warning('Error while exporting: '.$e->getMessage(), [
'entity' => get_class($entity),
'id' => $entity->getId(),
]);

$response = $this->redirectToRoute($this->definition::getRoute(Page::SHOW), [
'id' => $entity->getId(),
]);
}
return $response;
}

Expand Down

0 comments on commit 3caffb6

Please sign in to comment.