Passed
Pull Request — main (#122)
by Andreas
02:13
created

ExceptionConverter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 44.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 17
c 1
b 0
f 0
dl 0
loc 34
ccs 8
cts 18
cp 0.4444
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B convert() 0 31 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Crate\DBAL\Driver\PDOCrate;
6
7
use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
8
use Doctrine\DBAL\Driver\Exception;
9
use Doctrine\DBAL\Exception\ConnectionException;
10
use Doctrine\DBAL\Exception\DriverException;
11
use Doctrine\DBAL\Exception\InvalidFieldNameException;
12
use Doctrine\DBAL\Exception\SchemaDoesNotExist;
13
use Doctrine\DBAL\Exception\SyntaxErrorException;
14
use Doctrine\DBAL\Exception\TableExistsException;
15
use Doctrine\DBAL\Exception\TableNotFoundException;
16
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
17
use Doctrine\DBAL\Query;
18
use Doctrine\DBAL\Schema\Exception\ColumnDoesNotExist;
19
20
use function strpos;
21
22
/** @internal */
23
final class ExceptionConverter implements ExceptionConverterInterface
24
{
25
    /** @link https://cratedb.com/docs/crate/reference/en/latest/interfaces/http.html#error-codes */
26 10
    public function convert(Exception $exception, ?Query $query): DriverException
27
    {
28 10
        switch ($exception->getCode()) {
29 10
            case '4000':
30 2
                return new SyntaxErrorException($exception, $query);
31
32 8
            case '4008':
33 8
            case '4043':
34
                return new InvalidFieldNameException($exception, $query);
35
36 8
            case '4041':
37 8
                return new TableNotFoundException($exception, $query);
38
39
            case '4045':
40
                return new SchemaDoesNotExist($exception, $query);
41
42
            case '4091':
43
                return new UniqueConstraintViolationException($exception, $query);
44
45
            case '4093':
46
                return new TableExistsException($exception, $query);
47
        }
48
49
        // Prior to fixing https://bugs.php.net/bug.php?id=64705 (PHP 7.4.10),
50
        // in some cases (mainly connection errors) the PDO exception wouldn't provide a SQLSTATE via its code.
51
        // We have to match against the SQLSTATE in the error message in these cases.
52
        if ($exception->getCode() === 7 && strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) {
53
            return new ConnectionException($exception, $query);
54
        }
55
56
        return new DriverException($exception, $query);
57
    }
58
}
59