refactor exceptions

This commit is contained in:
DavidBadura 2015-04-06 21:13:02 +00:00
parent ec8ab39e7c
commit 0ab45fae1f
11 changed files with 189 additions and 88 deletions

View File

@ -0,0 +1,98 @@
<?php
namespace DavidBadura\Taskwarrior\Exception;
use Symfony\Component\Process\Process;
/**
* @author David Badura <d.a.badura@gmail.com>
*/
class CommandException extends TaskwarriorException
{
/**
* @var string
*/
private $command;
/**
* @var int
*/
private $exitCode;
/**
* @var string
*/
private $output;
/**
* @var string
*/
private $errorOutput;
/**
* @param Process $process
*/
public function __construct(Process $process)
{
$this->command = $process->getCommandLine();
$this->exitCode = $process->getExitCode();
$this->output = $process->getOutput();
$this->errorOutput = $process->getErrorOutput();
if (!$message = $this->getCleanErrorOutput()) {
$message = $this->output;
}
parent::__construct($message, $this->getExitCode());
}
/**
* @return string
*/
public function getCommand()
{
return $this->command;
}
/**
* @return int
*/
public function getExitCode()
{
return $this->exitCode;
}
/**
* @return string
*/
public function getOutput()
{
return $this->output;
}
/**
* @return string
*/
public function getErrorOutput()
{
return $this->errorOutput;
}
/**
* @return string
*/
public function getCleanErrorOutput()
{
$message = '';
foreach (explode("\n", $this->errorOutput) as $line) {
if (strpos($line, 'Using alternate') === 0 || strpos($line, 'Configuration override') === 0) {
continue;
}
$message .= $line . "\n";
}
return trim($message);
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace DavidBadura\Taskwarrior\Exception;
/**
* @author David Badura <d.a.badura@gmail.com>
*/
class DatetimeParseException extends TaskwarriorException
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace DavidBadura\Taskwarrior\Exception;
/**
* @author David Badura <d.a.badura@gmail.com>
*/
class RecurringParseException extends TaskwarriorException
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace DavidBadura\Taskwarrior\Exception;
/**
* @author David Badura <badura@simplethings.de>
*/
class TaskwarriorException extends \Exception
{
}

View File

@ -5,6 +5,8 @@
namespace DavidBadura\Taskwarrior; namespace DavidBadura\Taskwarrior;
use DavidBadura\Taskwarrior\Exception\RecurringParseException;
/** /**
* @author David Badura <d.a.badura@gmail.com> * @author David Badura <d.a.badura@gmail.com>
*/ */
@ -28,15 +30,15 @@ class Recurring
/** /**
* @param string $recurring * @param string $recurring
* @throws TaskwarriorException * @throws RecurringParseException
*/ */
public function __construct($recurring) public function __construct($recurring)
{ {
if (self::isValid($recurring)) { if (!self::isValid($recurring)) {
$this->recurring = $recurring; throw new RecurringParseException(sprintf('recurring "%s" is not valid', $recurring));
} else {
throw new TaskwarriorException();
} }
$this->recurring = $recurring;
} }
/** /**

View File

@ -3,6 +3,7 @@
namespace DavidBadura\Taskwarrior; namespace DavidBadura\Taskwarrior;
use Carbon\Carbon; use Carbon\Carbon;
use DavidBadura\Taskwarrior\Exception\DatetimeParseException;
use JMS\Serializer\Annotation as JMS; use JMS\Serializer\Annotation as JMS;
/** /**
@ -381,7 +382,7 @@ class Task
/** /**
* @param string|\DateTime|null $date * @param string|\DateTime|null $date
* @return \DateTime|null * @return \DateTime|null
* @throws TaskwarriorException * @throws DatetimeParseException
*/ */
private function parseDateTime($date) private function parseDateTime($date)
{ {
@ -404,7 +405,7 @@ class Task
return null; return null;
} }
throw new TaskwarriorException(); throw new DatetimeParseException($date);
} }
/** /**

View File

@ -2,13 +2,14 @@
namespace DavidBadura\Taskwarrior; namespace DavidBadura\Taskwarrior;
use Carbon\Carbon;
use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
use DavidBadura\Taskwarrior\Serializer\Handler\CarbonHandler; use DavidBadura\Taskwarrior\Serializer\Handler\CarbonHandler;
use DavidBadura\Taskwarrior\Serializer\Handler\RecurringHandler; use DavidBadura\Taskwarrior\Serializer\Handler\RecurringHandler;
use JMS\Serializer\Handler\HandlerRegistryInterface; use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\JsonSerializationVisitor; use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\Naming\CamelCaseNamingStrategy; use JMS\Serializer\Naming\CamelCaseNamingStrategy;
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Serializer; use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializerBuilder;
@ -133,9 +134,14 @@ class TaskManager
return; return;
} }
if ($task->isRecurring()) {
$task->setUntil('now');
$this->save($task);
} else {
$this->taskwarrior->delete($task->getUuid()); $this->taskwarrior->delete($task->getUuid());
$this->refresh($task); $this->refresh($task);
} }
}
/** /**
* @param Task $task * @param Task $task

View File

@ -2,7 +2,8 @@
namespace DavidBadura\Taskwarrior; namespace DavidBadura\Taskwarrior;
use JMS\Serializer\SerializerBuilder; use DavidBadura\Taskwarrior\Exception\CommandException;
use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\ProcessBuilder; use Symfony\Component\Process\ProcessBuilder;
@ -57,7 +58,6 @@ class Taskwarrior
/** /**
* @param array $params * @param array $params
* @throws TaskwarriorException
*/ */
public function add(array $params) public function add(array $params)
{ {
@ -76,7 +76,6 @@ class Taskwarrior
/** /**
* @param null $filter * @param null $filter
* @return array * @return array
* @throws TaskwarriorException
*/ */
public function projects($filter = null) public function projects($filter = null)
{ {
@ -88,7 +87,6 @@ class Taskwarrior
/** /**
* @param null $filter * @param null $filter
* @return array * @return array
* @throws TaskwarriorException
*/ */
public function tags($filter = null) public function tags($filter = null)
{ {
@ -104,6 +102,7 @@ class Taskwarrior
/** /**
* @param string $json * @param string $json
* @return string * @return string
* @throws CommandException
* @throws TaskwarriorException * @throws TaskwarriorException
*/ */
public function import($json) public function import($json)
@ -175,11 +174,7 @@ class Taskwarrior
$process->run(); $process->run();
if (!$process->isSuccessful()) { if (!$process->isSuccessful()) {
throw new TaskwarriorException( throw new CommandException($process);
$this->extractErrorMessage($process->getErrorOutput()),
$process->getExitCode(),
$process->getCommandLine()
);
} }
return $process->getOutput(); return $process->getOutput();
@ -187,7 +182,6 @@ class Taskwarrior
/** /**
* @return string * @return string
* @throws TaskwarriorException
*/ */
public function version() public function version()
{ {
@ -266,25 +260,6 @@ class Taskwarrior
return $builder; return $builder;
} }
/**
* @param string $string
* @return string
*/
private function extractErrorMessage($string)
{
$message = '';
foreach (explode("\n", $string) as $line) {
if (strpos($line, 'Using alternate') === 0 || strpos($line, 'Configuration override') === 0) {
continue;
}
$message .= $line . "\n";
}
return trim($message);
}
/** /**
* @param string $string * @param string $string
* @return array * @return array

View File

@ -1,34 +0,0 @@
<?php
namespace DavidBadura\Taskwarrior;
use Exception;
/**
* @author David Badura <badura@simplethings.de>
*/
class TaskwarriorException extends \Exception
{
/**
* @var string
*/
private $command;
/**
* @param string $message
* @param int $code
* @param string $command
*/
public function __construct($message = "", $code = 0, $command = '')
{
parent::__construct($message, $code, null);
}
/**
* @return string
*/
public function getCommand()
{
return $this->command;
}
}

View File

@ -67,7 +67,7 @@ class RecurringTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInvalid($recur) public function testInvalid($recur)
{ {
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException'); $this->setExpectedException('DavidBadura\Taskwarrior\Exception\RecurringParseException');
$obj = new Recurring($recur); $obj = new Recurring($recur);
} }

View File

@ -61,7 +61,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
public function testSaveTaskWithoutDescription() public function testSaveTaskWithoutDescription()
{ {
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException'); $this->setExpectedException('DavidBadura\Taskwarrior\Exception\TaskwarriorException');
$task = new Task(); $task = new Task();
@ -547,7 +547,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
public function testRecurringRemoveRecurringException() public function testRecurringRemoveRecurringException()
{ {
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException', $this->setExpectedException('DavidBadura\Taskwarrior\Exception\TaskwarriorException',
'You cannot remove the recurrence from a recurring task.'); 'You cannot remove the recurrence from a recurring task.');
$task1 = new Task(); $task1 = new Task();
@ -564,7 +564,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
public function testRecurringRemoveDueException() public function testRecurringRemoveDueException()
{ {
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException', $this->setExpectedException('DavidBadura\Taskwarrior\Exception\TaskwarriorException',
'You cannot remove the due date from a recurring task.'); 'You cannot remove the due date from a recurring task.');
$task1 = new Task(); $task1 = new Task();
@ -581,7 +581,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
public function testRecurringWithoutDue() public function testRecurringWithoutDue()
{ {
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException', $this->setExpectedException('DavidBadura\Taskwarrior\Exception\TaskwarriorException',
"A recurring task must also have a 'due' date."); "A recurring task must also have a 'due' date.");
$task1 = new Task(); $task1 = new Task();
@ -629,6 +629,29 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($recur2, $task1->getRecurring()); $this->assertEquals($recur2, $task1->getRecurring());
} }
public function testRecurringDelete()
{
$this->markTestIncomplete('not working yet');
$recur1 = new Recurring(Recurring::DAILY);
$task1 = new Task();
$task1->setDescription('foo1');
$task1->setDue('tomorrow');
$task1->setRecurring($recur1);
$this->taskManager->save($task1);
$this->taskManager->clear();
$this->assertCount(1, $this->taskManager->filterAll('status:recurring'));
$this->taskManager->delete($task1);
$this->taskManager->clear();
$this->assertCount(0, $this->taskManager->filterAll('status:recurring'));
}
public function testUntil() public function testUntil()
{ {
$task1 = new Task(); $task1 = new Task();