Skip to content

improve error handling SchemaBuilder #39799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright 2017 Adobe All rights reserved.
* See COPYING.txt for license details.
* Copyright 2017 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand All @@ -16,6 +16,8 @@
use Magento\Framework\Setup\Declaration\Schema\Sharding;
use Magento\Framework\Config\FileResolverByModule;
use Magento\Framework\Setup\Declaration\Schema\Declaration\ReaderComposite;
use Psr\Log\LoggerInterface;
use Magento\Framework\Exception\LocalizedException;

/**
* This type of builder is responsible for converting ENTIRE data, that comes from db
Expand All @@ -27,6 +29,7 @@
*
* @see Schema
* @inheritdoc
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class SchemaBuilder
{
Expand Down Expand Up @@ -55,30 +58,39 @@ class SchemaBuilder
*/
private $readerComposite;

/**
* @var LoggerInterface
*/
private $logger;

/**
* Constructor.
*
* @param ElementFactory $elementFactory
* @param DbSchemaReaderInterface $dbSchemaReader
* @param Sharding $sharding
* @param ReaderComposite $readerComposite
* @param LoggerInterface $logger
*/
public function __construct(
ElementFactory $elementFactory,
DbSchemaReaderInterface $dbSchemaReader,
Sharding $sharding,
ReaderComposite $readerComposite
ReaderComposite $readerComposite,
LoggerInterface $logger
) {
$this->elementFactory = $elementFactory;
$this->dbSchemaReader = $dbSchemaReader;
$this->sharding = $sharding;
$this->readerComposite = $readerComposite;
$this->logger = $logger;
}

/**
* @inheritdoc
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @throws LocalizedException
*/
public function build(Schema $schema)
{
Expand All @@ -88,8 +100,22 @@ public function build(Schema $schema)
foreach ($data['table'] as $keyTable => $tableColumns) {
$tableColumns['column'] ??= [];
foreach ($tableColumns['column'] as $keyColumn => $columnData) {
if ($columnData['type'] == 'json') {
$tablesWithJsonTypeField[$keyTable] = $keyColumn;
try {
if ($columnData['type'] == 'json') {
$tablesWithJsonTypeField[$keyTable] = $keyColumn;
}
} catch (\Throwable $e) {
// Create a new exception with extended context message
$errorMessage = sprintf(
"%s\nError processing table %s column %s",
$e->getMessage(),
$keyTable,
$keyColumn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per this comment #39799 (comment), please add a check with table is defined without columns.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @engcom-Hotel , the check is already there at line 101

);
$this->logger->error($errorMessage);
// Throw a new exception with the extended message
// This preserves the original error but adds our context
throw new LocalizedException(new Phrase($errorMessage));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright 2018 Adobe All rights reserved.
* See COPYING.txt for license details.
* Copyright 2018 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand All @@ -25,6 +25,7 @@
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* Test for SchemaBuilder.
Expand Down Expand Up @@ -63,6 +64,11 @@ class SchemaBuilderTest extends TestCase
*/
private $sqlVersionProvider;

/**
* @var LoggerInterface|MockObject
*/
private $loggerMock;

protected function setUp(): void
{
$this->elementFactoryMock = $this->getMockBuilder(ElementFactory::class)
Expand Down Expand Up @@ -401,11 +407,16 @@ public function testBuildHandlesMissingColumnsGracefully()
->method('read')
->willReturn($data);

$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();

$schemaBuilder = new SchemaBuilder(
$this->elementFactoryMock,
$this->dbSchemaReaderMock,
$this->shardingMock,
$readerCompositeMock
$readerCompositeMock,
$this->loggerMock
);

$schemaBuilder->build($this->createMock(Schema::class));
Expand Down