Skip to content

Commit

Permalink
no issue - various error handling fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Feb 26, 2024
1 parent caebb27 commit e54824a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
59 changes: 59 additions & 0 deletions docs/content/bridges/error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Error handling

## Query builder errors

Query builder errors are the generic exceptions for the whole API which are
not server errors, they are used for:

- misconfiguration errors,
- query builder invalid arguments errors,
- value conversion errors,
- result set errors.

They all inherit from `MakinaCorpus\QueryBuilder\Error\QueryBuilderError`.

## Server errors

All server side errors are exception, when a driver error is caught and supported,
it will be converted to a `MakinaCorpus\QueryBuilder\Error\Bridge\*` exception.

When an error is not supported then the generic
`MakinaCorpus\QueryBuilder\Error\Bridge\ServerError` is raised.

## Bridge errors passthrough

Each bridge implements one or more `MakinaCorpus\QueryBuilder\Bridge\ErrorConverter`
classes, depending upon the underlaying connector and platform.

This object converts the driver error to a uniformized exception provided under
the `MakinaCorpus\QueryBuilder\Error\Bridge` namespace.

Per default, specific error converters are lazily registered when a first error
happens. When used with Doctrine DBAL, you might want to keep a seemless integration
with it, and let the `doctrine/dbal` exception pass, in order to do this, simply
call the `Bridge::disableErrorConverter()` method when initializing the bridge:

```php
use Doctrine\DBAL\DriverManager;
use MakinaCorpus\QueryBuilder\Bridge\Doctrine\DoctrineQueryBuilder;

$connection = DriverManager::getConnection(['driver' => 'pdo_pgsql', /* ... */]);
$queryBuilder = new DoctrineQueryBuilder($connection);

// Here it is:
$queryBuilder->disableErrorConverter();
```

:::note
This works with all bridges.
:::

:::note
The `makinacorpus/query-builder-bundle` will per default configure the `doctrine/dbal`
bridge to disable specific error handling in order to be completely transparent for
users that will usually expect Doctrine DBAL errors.
:::

:::warning
This method must be called before any SQL request really happens.
:::
2 changes: 1 addition & 1 deletion src/Bridge/AbstractBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(?ConverterPluginRegistry $converterPluginRegistry =
/**
* Disable error converter. Must be called prior to initilization.
*/
public function disableErrorConverter(): bool
public function disableErrorConverter(): void
{
if ($this->errorConverter) {
throw new QueryBuilderError("Bridge is already initialized, configuration must happend before it gets used.");
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Bridge extends QueryExecutor, QueryBuilder
/**
* Disable error converter. Must be called prior to initilization.
*/
public function disableErrorConverter(): bool;
public function disableErrorConverter(): void;

/**
* Get server name.
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/ErrorConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ErrorConverter
*
* @param \Throwable $error
* Original error from driver.
* @param null|string $sqlQuery
* @param null|string $sql
* The raw SQL query that is the subject of this error.
* @param null|string $message
* Overriden error message, if any.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function convertError(\Throwable $error, ?string $sql = null, ?string $me
}

$errorCode = $error->errorInfo[1] ?? $error->getCode();
$sqlState = $error->errorInfo[0] ?? $error->getCode();

switch ($errorCode) {

Expand Down

0 comments on commit e54824a

Please sign in to comment.