MigrationMoodle: Fixing exceptions - refs BT#15992

pull/3127/head
Angel Fernando Quiroz Campos 6 years ago
parent 593d2c27a3
commit 9509c69c66
  1. 13
      plugin/migrationmoodle/admin.php
  2. 23
      plugin/migrationmoodle/src/Exceptions/Exception.php
  3. 10
      plugin/migrationmoodle/src/Messages/ExtractMessage.php
  4. 16
      plugin/migrationmoodle/src/Messages/LoadMessage.php
  5. 64
      plugin/migrationmoodle/src/Messages/Message.php
  6. 12
      plugin/migrationmoodle/src/Messages/TransformMessage.php
  7. 31
      plugin/migrationmoodle/src/Task/BaseTask.php

@ -1,7 +1,7 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\Exception as MigrationMoodleException; use Chamilo\PluginBundle\MigrationMoodle\Exceptions\Message as MigrationMoodleException;
use Chamilo\PluginBundle\MigrationMoodle\Task\BaseTask; use Chamilo\PluginBundle\MigrationMoodle\Task\BaseTask;
require_once __DIR__.'/../../main/inc/global.inc.php'; require_once __DIR__.'/../../main/inc/global.inc.php';
@ -98,21 +98,14 @@ if (!empty($action) && isAllowedAction($action, $menu)) {
echo Display::page_subheader( echo Display::page_subheader(
$plugin->get_lang($taskName) $plugin->get_lang($taskName)
); );
echo '<pre>';
$taskName = 'Chamilo\\PluginBundle\\MigrationMoodle\\Task\\'.$taskName; $taskName = 'Chamilo\\PluginBundle\\MigrationMoodle\\Task\\'.$taskName;
/** @var BaseTask $task */ /** @var BaseTask $task */
$task = new $taskName(); $task = new $taskName();
try { echo '<pre>';
$task->execute(); $task->execute();
} catch (MigrationMoodleException $exception) {
$exception->displayAsString();
} catch (Exception $exception) {
echo $exception->getTraceAsString();
}
echo '</pre>'; echo '</pre>';
} }

@ -1,23 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Exceptions;
/**
* Class Exception.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Exceptions
*/
abstract class Exception extends \Exception
{
public function displayAsString()
{
$pieces = [$this->getMessage()];
if ($this->getPrevious()) {
$pieces[] = "\t".$this->getPrevious()->getMessage();
}
echo implode(PHP_EOL, $pieces);
}
}

@ -1,19 +1,19 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Exceptions; namespace Chamilo\PluginBundle\MigrationMoodle\Messages;
use Throwable; use Throwable;
/** /**
* Class ExtractException. * Class ExtractException.
* *
* @package Chamilo\PluginBundle\MigrationMoodle\Exceptions * @package Chamilo\PluginBundle\MigrationMoodle\Messages
*/ */
class ExtractException extends Exception class ExtractMessage extends Message
{ {
/** /**
* ExtractException constructor. * ExtractMessage constructor.
* *
* @param Throwable|null $previous * @param Throwable|null $previous
*/ */
@ -21,6 +21,6 @@ class ExtractException extends Exception
{ {
$message = 'Error while extracting data.'; $message = 'Error while extracting data.';
parent::__construct($message, 0, $previous); parent::__construct($message, $previous);
} }
} }

@ -1,28 +1,34 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Exceptions; namespace Chamilo\PluginBundle\MigrationMoodle\Messages;
use Throwable; use Throwable;
/** /**
* Class LoadException. * Class LoadMessage.
* *
* @package Chamilo\PluginBundle\MigrationMoodle\Exceptions * @package Chamilo\PluginBundle\MigrationMoodle\Messages
*/ */
class LoadException extends Exception class LoadMessage extends Message
{ {
/** /**
* @var array * @var array
*/ */
private $incomingData; private $incomingData;
/**
* LoadMessage constructor.
*
* @param $incomingData
* @param Throwable|null $previous
*/
public function __construct($incomingData, Throwable $previous = null) public function __construct($incomingData, Throwable $previous = null)
{ {
$message = 'Error while loading transformed data.'; $message = 'Error while loading transformed data.';
$this->incomingData = $incomingData; $this->incomingData = $incomingData;
parent::__construct($message, 0, $previous); parent::__construct($message, $previous);
} }
public function displayAsString() public function displayAsString()

@ -0,0 +1,64 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Messages;
use Throwable;
/**
* Class Message.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Messages
*/
abstract class Message
{
/**
* @var string
*/
protected $message;
/**
* @var Throwable
*/
protected $previous;
/**
* Message constructor.
*
* @param string $message
* @param Throwable|null $previous
*/
public function __construct($message = "", Throwable $previous = null)
{
$this->message = $message;
$this->previous = $previous;
$this->displayAsString();
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* @return Throwable
*/
public function getPrevious()
{
return $this->previous;
}
public function displayAsString()
{
$pieces = [$this->message];
if ($this->previous) {
$pieces[] = "\t".$this->previous->getMessage();
}
echo implode(PHP_EOL, $pieces);
}
}

@ -1,16 +1,16 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Exceptions; namespace Chamilo\PluginBundle\MigrationMoodle\Messages;
use Throwable; use Throwable;
/** /**
* Class TransformException. * Class TransformMessage.
* *
* @package Chamilo\PluginBundle\MigrationMoodle\Exceptions * @package Chamilo\PluginBundle\MigrationMoodle\Messages
*/ */
class TransformException extends Exception class TransformMessage extends Message
{ {
/** /**
* @var array * @var array
@ -18,7 +18,7 @@ class TransformException extends Exception
private $extractedData; private $extractedData;
/** /**
* TransformException constructor. * TransformMessage constructor.
* *
* @param array $extractedData * @param array $extractedData
* @param Throwable|null $previous * @param Throwable|null $previous
@ -28,7 +28,7 @@ class TransformException extends Exception
$message = 'Error while transforming extracted data.'; $message = 'Error while transforming extracted data.';
$this->extractedData = $extractedData; $this->extractedData = $extractedData;
parent::__construct($message, 0, $previous); parent::__construct($message, $previous);
} }
public function displayAsString() public function displayAsString()

@ -3,13 +3,12 @@
namespace Chamilo\PluginBundle\MigrationMoodle\Task; namespace Chamilo\PluginBundle\MigrationMoodle\Task;
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\Exception;
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\ExtractException;
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\LoadException;
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\TransformException;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\ExtractorInterface; use Chamilo\PluginBundle\MigrationMoodle\Interfaces\ExtractorInterface;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\LoaderInterface; use Chamilo\PluginBundle\MigrationMoodle\Interfaces\LoaderInterface;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\TransformerInterface; use Chamilo\PluginBundle\MigrationMoodle\Interfaces\TransformerInterface;
use Chamilo\PluginBundle\MigrationMoodle\Messages\ExtractMessage;
use Chamilo\PluginBundle\MigrationMoodle\Messages\LoadMessage;
use Chamilo\PluginBundle\MigrationMoodle\Messages\TransformMessage;
use Chamilo\PluginBundle\MigrationMoodle\Traits\MapTrait\MapTrait; use Chamilo\PluginBundle\MigrationMoodle\Traits\MapTrait\MapTrait;
/** /**
@ -67,27 +66,33 @@ abstract class BaseTask
*/ */
abstract public function getLoadConfiguration(); abstract public function getLoadConfiguration();
/**
* @throws Exception
*/
public function execute() public function execute()
{ {
foreach ($this->extractFiltered() as $extractedData) { foreach ($this->extractFiltered() as $extractedData) {
try { try {
$incomingData = $this->transformer->transform($extractedData); $incomingData = $this->transformer->transform($extractedData);
} catch (\Exception $exception) { } catch (\Exception $exception) {
throw new TransformException($extractedData, $exception); new TransformMessage($extractedData, $exception);
continue;
} }
try { try {
$loadedId = $this->loader->load($incomingData); $loadedId = $this->loader->load($incomingData);
} catch (\Exception $exception) { } catch (\Exception $exception) {
throw new LoadException($incomingData, $exception); new LoadMessage($incomingData, $exception);
continue;
} }
$hash = $this->saveMapLog($extractedData['id'], $loadedId); try {
$hash = $this->saveMapLog($extractedData['id'], $loadedId);
echo "Data migrated. $hash".PHP_EOL; echo "Data migrated. $hash".PHP_EOL;
} catch (\Exception $exception) {
echo "Data migrated, but... \n";
echo "\n".$exception->getTraceAsString();
}
} }
} }
@ -111,8 +116,6 @@ abstract class BaseTask
} }
/** /**
* @throws ExtractException
*
* @return \Generator * @return \Generator
*/ */
private function extractFiltered() private function extractFiltered()
@ -126,7 +129,7 @@ abstract class BaseTask
yield $extractedData; yield $extractedData;
} }
} catch (\Exception $exception) { } catch (\Exception $exception) {
throw new ExtractException($exception); new ExtractMessage($exception);
} }
} }

Loading…
Cancel
Save