Commit 543a5599 authored by catch's avatar catch

Issue #2679835 by quietone, Jo Fitzgerald, heddn, alexpott, phenaproxima, xjm,...

Issue #2679835 by quietone, Jo Fitzgerald, heddn, alexpott, phenaproxima, xjm, mikeryan: Improve exception handling in Migrate UI
parent 39d22bfb
......@@ -4,6 +4,7 @@
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\RequirementsInterface;
......@@ -56,7 +57,7 @@ protected function getSystemData(Connection $connection) {
$system_data[$result['type']][$result['name']] = $result;
}
}
catch (\Exception $e) {
catch (DatabaseExceptionWrapper $e) {
// The table might not exist for example in tests.
}
return $system_data;
......@@ -189,10 +190,18 @@ protected function getLegacyDrupalVersion(Connection $connection) {
// For Drupal 8 (and we're predicting beyond) the schema version is in the
// key_value store.
elseif ($connection->schema()->tableExists('key_value')) {
$result = $connection
->query("SELECT value FROM {key_value} WHERE collection = :system_schema and name = :module", [':system_schema' => 'system.schema', ':module' => 'system'])
->fetchField();
$version_string = unserialize($result);
try {
$result = $connection
->query("SELECT value FROM {key_value} WHERE collection = :system_schema and name = :module", [
':system_schema' => 'system.schema',
':module' => 'system',
])
->fetchField();
$version_string = unserialize($result);
}
catch (DatabaseExceptionWrapper $e) {
$version_string = FALSE;
}
}
else {
$version_string = FALSE;
......
......@@ -3,9 +3,11 @@
namespace Drupal\migrate_drupal_ui\Form;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\Exception\BadPluginDefinitionException;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -17,13 +19,6 @@
*/
class CredentialForm extends MigrateUpgradeFormBase {
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The HTTP client to fetch the files with.
*
......@@ -41,16 +36,13 @@ class CredentialForm extends MigrateUpgradeFormBase {
/**
* CredentialForm constructor.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $tempstore_private
* The private tempstore factory.
* @param \GuzzleHttp\ClientInterface $http_client
* A Guzzle client object.
*/
public function __construct(RendererInterface $renderer, PrivateTempStoreFactory $tempstore_private, ClientInterface $http_client) {
public function __construct(PrivateTempStoreFactory $tempstore_private, ClientInterface $http_client) {
parent::__construct($tempstore_private);
$this->renderer = $renderer;
$this->httpClient = $http_client;
}
......@@ -59,7 +51,6 @@ public function __construct(RendererInterface $renderer, PrivateTempStoreFactory
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('renderer'),
$container->get('tempstore.private'),
$container->get('http_client')
);
......@@ -220,32 +211,50 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
$database['driver'] = $driver;
// Validate the driver settings and just end here if we have any issues.
$connection = NULL;
$error_key = $database['driver'] . '][database';
if ($errors = $drivers[$driver]->validateDatabaseSettings($database)) {
foreach ($errors as $name => $message) {
$this->errors[$name] = $message;
}
}
else {
// Validate the database connection.
try {
$connection = $this->getConnection($database);
$version = (string) $this->getLegacyDrupalVersion($connection);
if (!$version) {
$this->errors[$database['driver'] . '][database'] = $this->t('Source database does not contain a recognizable Drupal version.');
}
elseif ($version !== (string) $form_state->getValue('version')) {
$this->errors['version'] = $this->t('Source database is Drupal version @version but version @selected was selected.',
[
'@version' => $version,
'@selected' => $form_state->getValue('version'),
]);
}
else {
// Setup migrations and save form data to private store.
$this->setupMigrations($database, $form_state);
}
}
catch (\Exception $e) {
$this->errors[$database['driver'] . '][database'] = $e->getMessage();
catch (DatabaseException $e) {
$this->errors[$error_key] = $e->getMessage();
}
}
// Get the Drupal version of the source database so it can be validated.
if (!$this->errors) {
$version = (string) $this->getLegacyDrupalVersion($connection);
if (!$version) {
$this->errors[$error_key] = $this->t('Source database does not contain a recognizable Drupal version.');
}
elseif ($version !== (string) $form_state->getValue('version')) {
$this->errors['version'] = $this->t('Source database is Drupal version @version but version @selected was selected.',
[
'@version' => $version,
'@selected' => $form_state->getValue('version'),
]);
}
}
// Setup migrations and save form data to private store.
if (!$this->errors) {
try {
$this->setupMigrations($database, $form_state);
}
catch (BadPluginDefinitionException $e) {
// BadPluginDefinitionException occurs if the source_module is not
// defined, which happens during testing.
$this->errors[$error_key] = $e->getMessage();
}
catch (RequirementsException $e) {
$this->errors[$error_key] = $e->getMessage();
}
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\migrate_drupal_ui\Form;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
......@@ -89,29 +88,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Retrieve the database driver from state.
$database_state_key = $this->state->get('migrate.fallback_state_key', '');
if ($database_state_key) {
try {
$database = $this->state->get($database_state_key, [])['database'];
if ($connection = $this->getConnection($database)) {
if ($version = $this->getLegacyDrupalVersion($connection)) {
$this->setupMigrations($database, $form_state);
$valid_legacy_database = TRUE;
}
}
}
catch (DatabaseExceptionWrapper $exception) {
// Hide DB exceptions and forward to the DB credentials form. In that
// form we can more properly display errors and accept new credentials.
}
}
}
/**
* {@inheritdoc}
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment