matchException example

public function testExceptionHandler(): void
    {
        $handler = new CategoryNonExistentExceptionHandler();

        static::assertEquals(ExceptionHandlerInterface::PRIORITY_DEFAULT, $handler->getPriority());

        $afterException = new ForeignKeyConstraintViolationException(
            new PDOException('SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shopware`.`category`, CONSTRAINT `fk.category.after_category_id` FOREIGN KEY (`after_category_id`, `after_category_version_id`) REFERENCES `category` (`id`, `version_id`) ON DELETE SET NULL O)'),
            new Query('SOME QUERY', [][])
        );

        $matched = $handler->matchException($afterException);

        static::assertInstanceOf(CategoryException::class$matched);
        static::assertEquals('Category to insert after not found.', $matched->getMessage());

        static::assertNull($handler->matchException(new \Exception('Some other exception')));
    }
}
static::assertSame(0, $handler->getPriority());
    }

    public function testSqlExceptionHandled(): void
    {
        $message = 'An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry \'foo-bar\' for key \'shipping_method_price.uniq.shipping_method_quantity_start\'';

        $e = new \Exception($message);

        $handler = new ShippingMethodPriceExceptionHandler();
        $result = $handler->matchException($e);

        static::assertInstanceOf(ShippingException::class$result);
        static::assertSame('Shipping method price quantity already exists.', $result->getMessage());
    }

    public function testNonAlignedSqlExceptionNotHandled(): void
    {
        $message = 'An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry \'foo-bar\' for key \'product.uniq.product.product_number__version_id\'';
        $e = new \Exception($message);

        $handler = new ShippingMethodPriceExceptionHandler();
        
if ($command instanceof InsertCommand) {
                        $inserts->addInsert($definition->getEntityName()$command->getPayload());

                        continue;
                    }

                    throw new UnsupportedCommandTypeException($command);
                } catch (\Exception $e) {
                    $command->setFailed(true);

                    $innerException = $this->exceptionHandlerRegistry->matchException($e);

                    if ($innerException instanceof \Exception) {
                        $e = $innerException;
                    }
                    $context->getExceptions()->add($e);

                    throw $e;
                }
            }

            $mappings->execute();
            


    public function add(ExceptionHandlerInterface $exceptionHandler): void
    {
        $this->exceptionHandlers[$exceptionHandler->getPriority()][] = $exceptionHandler;
    }

    public function matchException(\Exception $e): ?\Exception
    {
        foreach ($this->getExceptionHandlers() as $priorityExceptionHandlers) {
            foreach ($priorityExceptionHandlers as $exceptionHandler) {
                $innerException = $exceptionHandler->matchException($e);

                if ($innerException instanceof \Exception) {
                    return $innerException;
                }
            }
        }

        return null;
    }

    /** * @return array<int, list<ExceptionHandlerInterface>> */
class ThemeExceptionHandlerTest extends TestCase
{
    public function testMatchException(): void
    {
        $exception = new \Exception(
            'An exception occurred while executing a query: SQLSTATE[23000]: '
            . 'Integrity constraint violation: 1451 Cannot delete or update a parent row: '
            . 'a foreign key constraint fails '
            . '(`shopware`.`theme_media`, CONSTRAINT `fk.theme_media.media_id` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE)'
        );

        $result = (new ThemeExceptionHandler())->matchException($exception);

        static::assertInstanceOf(ThemeException::class$result);
    }

    public function testNotMatchUnrelatedFkException(): void
    {
        $exception = new \Exception(
            'An exception occurred while executing a query: SQLSTATE[23000]: '
            . 'Integrity constraint violation: 1451 Cannot delete or update a parent row: '
            . 'a foreign key constraint fails '
            . '(`shopware`.`product_media`, CONSTRAINT `fk.product_media.media_id` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE)'
        );
Home | Imprint | This part of the site doesn't use cookies.