mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-25 03:08:51 +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
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