refactor exceptions
This commit is contained in:
98
src/Exception/CommandException.php
Normal file
98
src/Exception/CommandException.php
Normal 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);
|
||||
}
|
||||
}
|
10
src/Exception/DatetimeParseException.php
Normal file
10
src/Exception/DatetimeParseException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Exception;
|
||||
|
||||
/**
|
||||
* @author David Badura <d.a.badura@gmail.com>
|
||||
*/
|
||||
class DatetimeParseException extends TaskwarriorException
|
||||
{
|
||||
}
|
10
src/Exception/RecurringParseException.php
Normal file
10
src/Exception/RecurringParseException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Exception;
|
||||
|
||||
/**
|
||||
* @author David Badura <d.a.badura@gmail.com>
|
||||
*/
|
||||
class RecurringParseException extends TaskwarriorException
|
||||
{
|
||||
}
|
10
src/Exception/TaskwarriorException.php
Normal file
10
src/Exception/TaskwarriorException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Exception;
|
||||
|
||||
/**
|
||||
* @author David Badura <badura@simplethings.de>
|
||||
*/
|
||||
class TaskwarriorException extends \Exception
|
||||
{
|
||||
}
|
@@ -5,21 +5,23 @@
|
||||
|
||||
namespace DavidBadura\Taskwarrior;
|
||||
|
||||
use DavidBadura\Taskwarrior\Exception\RecurringParseException;
|
||||
|
||||
/**
|
||||
* @author David Badura <d.a.badura@gmail.com>
|
||||
*/
|
||||
class Recurring
|
||||
{
|
||||
const DAILY = 'daily';
|
||||
const WEEKDAYS = 'weekdays';
|
||||
const WEEKLY = 'weekly';
|
||||
const BIWEEKLY = 'biweekly';
|
||||
const QUARTERLY = 'quarterly';
|
||||
const DAILY = 'daily';
|
||||
const WEEKDAYS = 'weekdays';
|
||||
const WEEKLY = 'weekly';
|
||||
const BIWEEKLY = 'biweekly';
|
||||
const QUARTERLY = 'quarterly';
|
||||
const SEMIANNUAL = 'semiannual';
|
||||
const ANNUAL = 'annual';
|
||||
const YEARLY = 'yearly';
|
||||
const BIANNUAL = 'biannual';
|
||||
const BIYEARLY = 'biyearly';
|
||||
const ANNUAL = 'annual';
|
||||
const YEARLY = 'yearly';
|
||||
const BIANNUAL = 'biannual';
|
||||
const BIYEARLY = 'biyearly';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
@@ -28,15 +30,15 @@ class Recurring
|
||||
|
||||
/**
|
||||
* @param string $recurring
|
||||
* @throws TaskwarriorException
|
||||
* @throws RecurringParseException
|
||||
*/
|
||||
public function __construct($recurring)
|
||||
{
|
||||
if (self::isValid($recurring)) {
|
||||
$this->recurring = $recurring;
|
||||
} else {
|
||||
throw new TaskwarriorException();
|
||||
if (!self::isValid($recurring)) {
|
||||
throw new RecurringParseException(sprintf('recurring "%s" is not valid', $recurring));
|
||||
}
|
||||
|
||||
$this->recurring = $recurring;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +63,7 @@ class Recurring
|
||||
*/
|
||||
public static function isValid($recur)
|
||||
{
|
||||
$refClass = new \ReflectionClass(__CLASS__);
|
||||
$refClass = new \ReflectionClass(__CLASS__);
|
||||
$constants = $refClass->getConstants();
|
||||
|
||||
if (in_array($recur, $constants)) {
|
||||
|
@@ -3,6 +3,7 @@
|
||||
namespace DavidBadura\Taskwarrior;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DavidBadura\Taskwarrior\Exception\DatetimeParseException;
|
||||
use JMS\Serializer\Annotation as JMS;
|
||||
|
||||
/**
|
||||
@@ -381,7 +382,7 @@ class Task
|
||||
/**
|
||||
* @param string|\DateTime|null $date
|
||||
* @return \DateTime|null
|
||||
* @throws TaskwarriorException
|
||||
* @throws DatetimeParseException
|
||||
*/
|
||||
private function parseDateTime($date)
|
||||
{
|
||||
@@ -404,7 +405,7 @@ class Task
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new TaskwarriorException();
|
||||
throw new DatetimeParseException($date);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -2,13 +2,14 @@
|
||||
|
||||
namespace DavidBadura\Taskwarrior;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
|
||||
use DavidBadura\Taskwarrior\Serializer\Handler\CarbonHandler;
|
||||
use DavidBadura\Taskwarrior\Serializer\Handler\RecurringHandler;
|
||||
use JMS\Serializer\Handler\HandlerRegistryInterface;
|
||||
use JMS\Serializer\JsonSerializationVisitor;
|
||||
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
|
||||
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
|
||||
use JMS\Serializer\SerializationContext;
|
||||
use JMS\Serializer\Serializer;
|
||||
use JMS\Serializer\SerializerBuilder;
|
||||
|
||||
@@ -133,8 +134,13 @@ class TaskManager
|
||||
return;
|
||||
}
|
||||
|
||||
$this->taskwarrior->delete($task->getUuid());
|
||||
$this->refresh($task);
|
||||
if ($task->isRecurring()) {
|
||||
$task->setUntil('now');
|
||||
$this->save($task);
|
||||
} else {
|
||||
$this->taskwarrior->delete($task->getUuid());
|
||||
$this->refresh($task);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -2,7 +2,8 @@
|
||||
|
||||
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\Process\ProcessBuilder;
|
||||
|
||||
@@ -57,7 +58,6 @@ class Taskwarrior
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @throws TaskwarriorException
|
||||
*/
|
||||
public function add(array $params)
|
||||
{
|
||||
@@ -76,7 +76,6 @@ class Taskwarrior
|
||||
/**
|
||||
* @param null $filter
|
||||
* @return array
|
||||
* @throws TaskwarriorException
|
||||
*/
|
||||
public function projects($filter = null)
|
||||
{
|
||||
@@ -88,7 +87,6 @@ class Taskwarrior
|
||||
/**
|
||||
* @param null $filter
|
||||
* @return array
|
||||
* @throws TaskwarriorException
|
||||
*/
|
||||
public function tags($filter = null)
|
||||
{
|
||||
@@ -104,6 +102,7 @@ class Taskwarrior
|
||||
/**
|
||||
* @param string $json
|
||||
* @return string
|
||||
* @throws CommandException
|
||||
* @throws TaskwarriorException
|
||||
*/
|
||||
public function import($json)
|
||||
@@ -175,11 +174,7 @@ class Taskwarrior
|
||||
$process->run();
|
||||
|
||||
if (!$process->isSuccessful()) {
|
||||
throw new TaskwarriorException(
|
||||
$this->extractErrorMessage($process->getErrorOutput()),
|
||||
$process->getExitCode(),
|
||||
$process->getCommandLine()
|
||||
);
|
||||
throw new CommandException($process);
|
||||
}
|
||||
|
||||
return $process->getOutput();
|
||||
@@ -187,7 +182,6 @@ class Taskwarrior
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws TaskwarriorException
|
||||
*/
|
||||
public function version()
|
||||
{
|
||||
@@ -266,25 +260,6 @@ class Taskwarrior
|
||||
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
|
||||
* @return array
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user