Skip to content
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

IBX-385: Fixed error handling when composer files are corrupted #94

Merged
merged 10 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions Resources/translations/dashboard.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@
<note>key: dashboard.ez_version.non_stable_ee</note>
</trans-unit>
<trans-unit id="c1fb5ba9fd547e30a49a4e1895822e69cd4a9dbc" resname="dashboard.ez_version.release_not_determined">
<source><![CDATA[The system could not find your <code>composer.lock</code> file. It's needed to determine information about
<source><![CDATA[The system could not find your <code>composer.lock</code> or <code>composer.json</code> file or it's not valid. It's needed to determine information about
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
your eZ install, and recommended to be kept on project development to make sure same package versions are used across all environments.]]></source>
<target state="new"><![CDATA[The system could not find your <code>composer.lock</code> file. It's needed to determine information about
<target state="new"><![CDATA[The system could not find your <code>composer.lock</code> or <code>composer.json</code> file or it's not valid. It's needed to determine information about
your eZ install, and recommended to be kept on project development to make sure same package versions are used across all environments.]]></target>
<note>key: dashboard.ez_version.release_not_determined</note>
</trans-unit>
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/themes/admin/dashboard/block/ez.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% set severity = 1 %}
<div class="alert alert-warning mb-0 mt-3" role="alert">
{{ 'dashboard.ez_version.release_not_determined'|trans
|desc("The system could not find your <code>composer.lock</code> file. It's needed to determine information about
|desc("The system could not find your <code>composer.lock</code> or <code>composer.json</code> file or it's not valid. It's needed to determine information about
your eZ installation. It is recommended to keep it during project development to make sure the same package versions are
used across all environments.")
|raw }}
Expand Down
3 changes: 2 additions & 1 deletion SystemInfo/Collector/EzSystemInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Collector;

use EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerFileValidationException;
use EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerLockFileNotFoundException;
use EzSystems\EzSupportToolsBundle\SystemInfo\Value\EzSystemInfo;
use DateTime;
Expand Down Expand Up @@ -123,7 +124,7 @@ public function __construct(SystemInfoCollector $composerCollector, $debug = fal
{
try {
$this->composerInfo = $composerCollector->collect();
} catch (ComposerLockFileNotFoundException $e) {
} catch (ComposerLockFileNotFoundException | ComposerFileValidationException $e) {
// do nothing
}
$this->debug = $debug;
Expand Down
9 changes: 9 additions & 0 deletions SystemInfo/Collector/JsonComposerLockSystemInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct($lockFile, $jsonFile)
* Collects information about installed composer packages.
*
* @throws Exception\ComposerLockFileNotFoundException if the composer.lock file was not found.
* @throws Exception\ComposerFileValidationException if composer.lock of composer.json are not valid.
*
* @return Value\ComposerSystemInfo
*/
Expand All @@ -74,6 +75,14 @@ public function collect()
$lockData = json_decode(file_get_contents($this->lockFile), true);
$jsonData = json_decode(file_get_contents($this->jsonFile), true);

if (!$lockData) {
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
throw new Exception\ComposerFileValidationException($this->lockFile);
}

if (!$jsonData) {
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
throw new Exception\ComposerFileValidationException($this->jsonFile);
}

return $this->value = new Value\ComposerSystemInfo([
'packages' => $this->extractPackages($lockData),
'repositoryUrls' => $this->extractRepositoryUrls($jsonData),
Expand Down
21 changes: 21 additions & 0 deletions SystemInfo/Exception/ComposerFileValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* File containing the ComposerJsonFileNotFoundException class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Exception;

use Exception;

class ComposerFileValidationException extends Exception
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
{
public function __construct(string $path, $code = 0, Exception $previous = null)
{
$message = sprintf('Composer file %s is not valid.', $path);

parent::__construct($message, $code, $previous);
}
}
24 changes: 24 additions & 0 deletions SystemInfo/Value/InvalidSystemInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* File containing the SymfonyKernelSystemInfo class.
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Value;

use eZ\Publish\API\Repository\Values\ValueObject;

/**
* Invalid value for system info used in case of any errors occur while collecting data.
*/
class InvalidSystemInfo extends ValueObject implements SystemInfo
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* Error message shown in the System info tab.
*
* @var string
*/
public $errorMessage;
}
10 changes: 9 additions & 1 deletion View/SystemInfoViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use eZ\Publish\Core\MVC\Symfony\View\Builder\ViewBuilder;
use eZ\Publish\Core\MVC\Symfony\View\Configurator;
use EzSystems\EzSupportToolsBundle\SystemInfo\SystemInfoCollectorRegistry;
use EzSystems\EzSupportToolsBundle\SystemInfo\Value\InvalidSystemInfo;

class SystemInfoViewBuilder implements ViewBuilder
{
Expand Down Expand Up @@ -37,8 +38,15 @@ public function buildView(array $parameters)
{
$collector = $this->getCollector($parameters['systemInfoIdentifier']);
$view = new SystemInfoView(null, [], $parameters['viewType']);
$view->setInfo($collector->collect());

try {
$collectedData = $collector->collect();
} catch (\Exception $e) {
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
$collectedData = new InvalidSystemInfo();
$collectedData->errorMessage = $e->getMessage();
}

$view->setInfo($collectedData);
$this->viewConfigurator->configure($view);

return $view;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"ocramius/proxy-manager": "~1.0 || ~2.0",
"symfony/proxy-manager-bridge": "^2.8.40 || ^3.4.11",
"zetacomponents/system-information": "^1.1.1",
"ezsystems/ezplatform-admin-ui": "^1.5@dev"
"ezsystems/ezplatform-admin-ui": "dev-ibx-385-responsiveness-if-composer-files-corrupted as 1.5.x-dev"
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
},
"require-dev": {
"ezsystems/ezplatform-code-style": "^0.1.0",
Expand Down