-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,607 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/Bridge/Doctrine/ErrorConverter/DoctrineErrorConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MakinaCorpus\QueryBuilder\Bridge\Doctrine\ErrorConverter; | ||
|
||
use Doctrine\DBAL\ConnectionException; | ||
use Doctrine\DBAL\Exception\ConnectionLost; | ||
use Doctrine\DBAL\Exception\ConstraintViolationException; | ||
use Doctrine\DBAL\Exception\DatabaseDoesNotExist; | ||
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; | ||
use Doctrine\DBAL\Exception\InvalidFieldNameException; | ||
use Doctrine\DBAL\Exception\NonUniqueFieldNameException; | ||
use Doctrine\DBAL\Exception\NotNullConstraintViolationException; | ||
use Doctrine\DBAL\Exception\TableNotFoundException; | ||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; | ||
use Doctrine\DBAL\Schema\Exception\ColumnDoesNotExist; | ||
use Doctrine\DBAL\Schema\Exception\TableDoesNotExist; | ||
use MakinaCorpus\QueryBuilder\Bridge\ErrorConverter; | ||
use MakinaCorpus\QueryBuilder\Error\Bridge\AmbiguousIdentifierError; | ||
use MakinaCorpus\QueryBuilder\Error\Bridge\ColumnDoesNotExistError; | ||
use MakinaCorpus\QueryBuilder\Error\Bridge\ConstraintViolationError; | ||
use MakinaCorpus\QueryBuilder\Error\Bridge\DatabaseObjectDoesNotExistError; | ||
use MakinaCorpus\QueryBuilder\Error\Bridge\ForeignKeyConstraintViolationError; | ||
use MakinaCorpus\QueryBuilder\Error\Bridge\NotNullConstraintViolationError; | ||
use MakinaCorpus\QueryBuilder\Error\Bridge\TableDoesNotExistError; | ||
use MakinaCorpus\QueryBuilder\Error\Bridge\UnableToConnectError; | ||
use MakinaCorpus\QueryBuilder\Error\Bridge\UniqueConstraintViolationError; | ||
use MakinaCorpus\QueryBuilder\Bridge\Pdo\ErrorConverter\PdoSQLiteErrorConverter; | ||
|
||
class DoctrineErrorConverter implements ErrorConverter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertError(\Throwable $error, ?string $sql = null, ?string $message = null): \Throwable | ||
{ | ||
$message ??= $error->getMessage(); | ||
|
||
if ($error instanceof InvalidFieldNameException || $error instanceof ColumnDoesNotExist) { | ||
return new ColumnDoesNotExistError($message, $error->getCode(), $error); | ||
} | ||
|
||
if ($error instanceof DatabaseDoesNotExist) { | ||
return new DatabaseObjectDoesNotExistError($message, $error->getCode(), $error); | ||
} | ||
|
||
if ($error instanceof ForeignKeyConstraintViolationException) { | ||
return new ForeignKeyConstraintViolationError($message, $error->getCode(), $error); | ||
} | ||
|
||
if ($error instanceof NotNullConstraintViolationException) { | ||
return new NotNullConstraintViolationError($message, $error->getCode(), $error); | ||
} | ||
|
||
if ($error instanceof TableDoesNotExist || $error instanceof TableNotFoundException) { | ||
return new TableDoesNotExistError($message, $error->getCode(), $error); | ||
} | ||
|
||
/* if ($error instanceof Foo) { | ||
return new TransactionDeadlockError(); | ||
} */ | ||
|
||
/* if ($error instanceof Foo) { | ||
return new TransactionLockWaitTimeoutError(); | ||
} */ | ||
|
||
if ($error instanceof ConnectionException || $error instanceof ConnectionLost) { | ||
return new UnableToConnectError($message, $error->getCode(), $error); | ||
} | ||
|
||
if ($error instanceof UniqueConstraintViolationException) { | ||
return new UniqueConstraintViolationError($message, $error->getCode(), $error); | ||
} | ||
|
||
/* | ||
* More generic errors after. | ||
*/ | ||
|
||
if ($error instanceof NonUniqueFieldNameException) { | ||
return new AmbiguousIdentifierError(); | ||
} | ||
|
||
/* if ($error instanceof Foo) { | ||
return new TransactionFailedError(); | ||
} */ | ||
|
||
if ($error instanceof ConstraintViolationException) { | ||
return new ConstraintViolationError($message, $error->getCode(), $error); | ||
} | ||
|
||
// Provide fallbacks for SQLite, because DBAL don't catch them all. | ||
return PdoSQLiteErrorConverter::createErrorFromMessage($error, $sql, $message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MakinaCorpus\QueryBuilder\Bridge; | ||
|
||
interface ErrorConverter | ||
{ | ||
/** | ||
* Convert underlaying connector errors to a common error. | ||
* | ||
* @param \Throwable $error | ||
* Original error from driver. | ||
* @param null|string $sqlQuery | ||
* The raw SQL query that is the subject of this error. | ||
* @param null|string $message | ||
* Overriden error message, if any. | ||
*/ | ||
public function convertError(\Throwable $error, ?string $sql = null, ?string $message = null): \Throwable; | ||
Check failure on line 19 in src/Bridge/ErrorConverter.php GitHub Actions / Static Analysis (8.2)
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MakinaCorpus\QueryBuilder\Bridge; | ||
|
||
/** | ||
* Implementation for when the error handling is disabled. | ||
* | ||
* Useful when configured over a doctrine/dbal bridge in Symfony context and | ||
* you want the Query Builder to be absolutely transparently integrated with | ||
* it. It's also true for other bridges contextes. | ||
*/ | ||
class PassthroughErrorConverter implements ErrorConverter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertError(\Throwable $error, ?string $sql = null, ?string $message = null): \Throwable | ||
{ | ||
return $error; | ||
} | ||
} |
Oops, something went wrong.