mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 17:39:06 +02:00
Show a proper error message table when encountering an invalid regex statement on SQLite
This is related to #289
This commit is contained in:
parent
2c33b381c1
commit
b0ab43c39a
3 changed files with 99 additions and 13 deletions
|
@ -31,6 +31,7 @@ use App\Entity\Parts\Footprint;
|
||||||
use App\Entity\Parts\Manufacturer;
|
use App\Entity\Parts\Manufacturer;
|
||||||
use App\Entity\Parts\Storelocation;
|
use App\Entity\Parts\Storelocation;
|
||||||
use App\Entity\Parts\Supplier;
|
use App\Entity\Parts\Supplier;
|
||||||
|
use App\Exceptions\InvalidRegexException;
|
||||||
use App\Form\Filters\PartFilterType;
|
use App\Form\Filters\PartFilterType;
|
||||||
use App\Services\Parts\PartsTableActionHandler;
|
use App\Services\Parts\PartsTableActionHandler;
|
||||||
use App\Services\Trees\NodesListBuilder;
|
use App\Services\Trees\NodesListBuilder;
|
||||||
|
@ -150,21 +151,21 @@ class PartListsController extends AbstractController
|
||||||
->handleRequest($request);
|
->handleRequest($request);
|
||||||
|
|
||||||
if ($table->isCallback()) {
|
if ($table->isCallback()) {
|
||||||
|
try {
|
||||||
try {
|
try {
|
||||||
return $table->getResponse();
|
return $table->getResponse();
|
||||||
} catch (DriverException $driverException) {
|
} catch (DriverException $driverException) {
|
||||||
if ($driverException->getCode() === 1139) {
|
if ($driverException->getCode() === 1139) {
|
||||||
|
//Convert the driver exception to InvalidRegexException so it has the same hanlder as for SQLite
|
||||||
//Show only the part after "1139"
|
throw InvalidRegexException::fromDriverException($driverException);
|
||||||
$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 {
|
} else {
|
||||||
throw $driverException;
|
throw $driverException;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (InvalidRegexException $exception) {
|
||||||
|
$errors = $this->translator->trans('part.table.invalid_regex').': '.$exception->getReason();
|
||||||
|
return ErrorDataTable::errorTable($this->dataTableFactory, $request, $errors);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->render($template, array_merge([
|
return $this->render($template, array_merge([
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
|
|
||||||
namespace App\Doctrine;
|
namespace App\Doctrine;
|
||||||
|
|
||||||
|
use App\Exceptions\InvalidRegexException;
|
||||||
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
|
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
|
||||||
use Doctrine\DBAL\Event\ConnectionEventArgs;
|
use Doctrine\DBAL\Event\ConnectionEventArgs;
|
||||||
use Doctrine\DBAL\Events;
|
use Doctrine\DBAL\Events;
|
||||||
|
@ -43,7 +44,11 @@ class SQLiteRegexExtension implements EventSubscriberInterface
|
||||||
//Ensure that the function really exists on the connection, as it is marked as experimental according to PHP documentation
|
//Ensure that the function really exists on the connection, as it is marked as experimental according to PHP documentation
|
||||||
if($native_connection instanceof \PDO && method_exists($native_connection, 'sqliteCreateFunction' )) {
|
if($native_connection instanceof \PDO && method_exists($native_connection, 'sqliteCreateFunction' )) {
|
||||||
$native_connection->sqliteCreateFunction('REGEXP', function ($pattern, $value) {
|
$native_connection->sqliteCreateFunction('REGEXP', function ($pattern, $value) {
|
||||||
|
try {
|
||||||
return (false !== mb_ereg($pattern, $value)) ? 1 : 0;
|
return (false !== mb_ereg($pattern, $value)) ? 1 : 0;
|
||||||
|
} catch (\ErrorException $e) {
|
||||||
|
throw InvalidRegexException::fromMBRegexError($e);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
80
src/Exceptions/InvalidRegexException.php
Normal file
80
src/Exceptions/InvalidRegexException.php
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
<?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\Exceptions;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Exception\DriverException;
|
||||||
|
use ErrorException;
|
||||||
|
|
||||||
|
class InvalidRegexException extends \RuntimeException
|
||||||
|
{
|
||||||
|
private ?string $reason;
|
||||||
|
|
||||||
|
public function __construct(string $reason = null)
|
||||||
|
{
|
||||||
|
$this->reason = $reason;
|
||||||
|
parent::__construct('Invalid regular expression');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the reason for the exception (what the regex driver deemed invalid)
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getReason(): ?string
|
||||||
|
{
|
||||||
|
return $this->reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new exception from a driver exception happening, when MySQL encounters an invalid regex
|
||||||
|
* @param DriverException $exception
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function fromDriverException(DriverException $exception): self
|
||||||
|
{
|
||||||
|
//1139 means invalid regex error
|
||||||
|
if ($exception->getCode() !== 1139) {
|
||||||
|
throw new \InvalidArgumentException('The given exception is not a driver exception', 0, $exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Reason is the part after the erorr code
|
||||||
|
$reason = preg_replace('/^.*1139 /', '', $exception->getMessage());
|
||||||
|
|
||||||
|
return new self($reason);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new exception from the errorException thrown by mb_ereg
|
||||||
|
* @param ErrorException $ex
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function fromMBRegexError(ErrorException $ex): self
|
||||||
|
{
|
||||||
|
//Ensure that the error is really a mb_ereg error
|
||||||
|
if ($ex->getSeverity() !== E_WARNING || !strpos($ex->getMessage(), 'mb_ereg()') !== false) {
|
||||||
|
throw new \InvalidArgumentException('The given exception is not a mb_ereg error', 0, $ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Reason is the part after the erorr code
|
||||||
|
$reason = preg_replace('/^.*mb_ereg\(\): /', '', $ex->getMessage());
|
||||||
|
|
||||||
|
return new self($reason);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue