mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-26 03:38:47 +02:00
Show an error message in table instead of a 500 error when MySQL encounters an invalid Regex expression
This fixes issue #289
This commit is contained in:
parent
bafbd63610
commit
c50a80e8df
5 changed files with 146 additions and 29 deletions
|
@ -22,6 +22,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\DataTables\ErrorDataTable;
|
||||
use App\DataTables\Filters\PartFilter;
|
||||
use App\DataTables\Filters\PartSearchFilter;
|
||||
use App\DataTables\PartsDataTable;
|
||||
|
@ -33,6 +34,7 @@ use App\Entity\Parts\Supplier;
|
|||
use App\Form\Filters\PartFilterType;
|
||||
use App\Services\Parts\PartsTableActionHandler;
|
||||
use App\Services\Trees\NodesListBuilder;
|
||||
use Doctrine\DBAL\Exception\DriverException;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Omines\DataTablesBundle\DataTableFactory;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
@ -41,6 +43,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
|
|||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class PartListsController extends AbstractController
|
||||
{
|
||||
|
@ -48,11 +51,14 @@ class PartListsController extends AbstractController
|
|||
private NodesListBuilder $nodesListBuilder;
|
||||
private DataTableFactory $dataTableFactory;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, NodesListBuilder $nodesListBuilder, DataTableFactory $dataTableFactory)
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, NodesListBuilder $nodesListBuilder, DataTableFactory $dataTableFactory, TranslatorInterface $translator)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->nodesListBuilder = $nodesListBuilder;
|
||||
$this->dataTableFactory = $dataTableFactory;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -144,7 +150,21 @@ class PartListsController extends AbstractController
|
|||
->handleRequest($request);
|
||||
|
||||
if ($table->isCallback()) {
|
||||
return $table->getResponse();
|
||||
try {
|
||||
return $table->getResponse();
|
||||
} catch (DriverException $driverException) {
|
||||
if ($driverException->getCode() === 1139) {
|
||||
|
||||
//Show only the part after "1139"
|
||||
$regex_message = preg_replace('/^.*1139 /', '', $driverException->getMessage());
|
||||
|
||||
$errors = $this->translator->trans('part.table.invalid_regex') . ': ' . $regex_message;
|
||||
|
||||
return ErrorDataTable::errorTable($this->dataTableFactory, $request, $errors);
|
||||
} else {
|
||||
throw $driverException;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($template, array_merge([
|
||||
|
|
85
src/DataTables/ErrorDataTable.php
Normal file
85
src/DataTables/ErrorDataTable.php
Normal file
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\DataTables;
|
||||
|
||||
use App\DataTables\Column\RowClassColumn;
|
||||
use App\Entity\Parts\Part;
|
||||
use Omines\DataTablesBundle\Adapter\ArrayAdapter;
|
||||
use Omines\DataTablesBundle\Column\TextColumn;
|
||||
use Omines\DataTablesBundle\DataTable;
|
||||
use Omines\DataTablesBundle\DataTableFactory;
|
||||
use Omines\DataTablesBundle\DataTableTypeInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class ErrorDataTable implements DataTableTypeInterface
|
||||
{
|
||||
public function configureOptions(OptionsResolver $optionsResolver): void
|
||||
{
|
||||
$optionsResolver->setRequired('errors');
|
||||
$optionsResolver->setAllowedTypes('errors', ['array', 'string']);
|
||||
$optionsResolver->setNormalizer('errors', function (OptionsResolver $optionsResolver, $errors) {
|
||||
if (is_string($errors)) {
|
||||
$errors = [$errors];
|
||||
}
|
||||
|
||||
return $errors;
|
||||
});
|
||||
}
|
||||
|
||||
public function configure(DataTable $dataTable, array $options)
|
||||
{
|
||||
$optionsResolver = new OptionsResolver();
|
||||
$this->configureOptions($optionsResolver);
|
||||
$options = $optionsResolver->resolve($options);
|
||||
|
||||
$dataTable
|
||||
->add('dont_matter_we_only_set_color', RowClassColumn::class, [
|
||||
'render' => function ($value, $context) {
|
||||
return 'table-warning';
|
||||
},
|
||||
])
|
||||
|
||||
->add('error', TextColumn::class, [
|
||||
'label' => 'error_table.error',
|
||||
'render' => function ($value, $context) {
|
||||
return '<i class="fa-solid fa-triangle-exclamation fa-fw"></i> ' . $value;
|
||||
},
|
||||
])
|
||||
;
|
||||
|
||||
//Build the array containing data
|
||||
$data = [];
|
||||
foreach ($options['errors'] as $error) {
|
||||
$data[] = ['error' => $error];
|
||||
}
|
||||
|
||||
$dataTable->createAdapter(ArrayAdapter::class, $data);
|
||||
}
|
||||
|
||||
public static function errorTable(DataTableFactory $dataTableFactory, Request $request, $errors): Response
|
||||
{
|
||||
$error_table = $dataTableFactory->createFromType(self::class, ['errors' => $errors]);
|
||||
$error_table->handleRequest($request);
|
||||
return $error_table->getResponse();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue