Permalink
Browse files

Cleaned up exceptions and added exception codes

  • Loading branch information...
1 parent 7a9200b commit 2c6360ced0cefb1b19b7d13987ae847b54b593ce @hm2k hm2k committed Feb 1, 2012
Showing with 73 additions and 31 deletions.
  1. +65 −24 Services/Mailman.php
  2. +6 −4 Services/Mailman/Exception.php
  3. +1 −2 tests/test_member.phpt
  4. +1 −1 tests/test_setdigest.phpt
View
89 Services/Mailman.php
@@ -110,7 +110,8 @@ public function setList($string)
if (!is_string($string)) {
throw new Services_Mailman_Exception(
'setList() expects parameter 1 to be string, ' .
- gettype($string) . ' given'
+ gettype($string) . ' given',
+ Services_Mailman_Exception::USER_INPUT
);
}
$this->list = $string;
@@ -129,18 +130,23 @@ public function setAdminUrl($string)
{
if (empty($string)) {
throw new Services_Mailman_Exception(
- 'setAdminUrl() does not expect parameter 1 to be empty'
+ 'setAdminUrl() does not expect parameter 1 to be empty',
+ Services_Mailman_Exception::USER_INPUT
);
}
if (!is_string($string)) {
throw new Services_Mailman_Exception(
'setAdminUrl() expects parameter 1 to be string, ' .
- gettype($string) . ' given'
+ gettype($string) . ' given',
+ Services_Mailman_Exception::USER_INPUT
);
}
$string = filter_var($string, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
if (!$string) {
- throw new Services_Mailman_Exception(Services_Mailman_Exception::INVALID_URL);
+ throw new Services_Mailman_Exception(
+ 'Invalid URL',
+ Services_Mailman_Exception::INVALID_URL
+ );
}
$this->adminUrl = trim($string, '/');
return $this;
@@ -159,7 +165,8 @@ public function setAdminPw($string)
if (!is_string($string)) {
throw new Services_Mailman_Exception(
'setAdminPw() expects parameter 1 to be string, ' .
- gettype($string) . ' given'
+ gettype($string) . ' given',
+ Services_Mailman_Exception::USER_INPUT
);
}
$this->adminPw = $string;
@@ -171,8 +178,6 @@ public function setAdminPw($string)
* @param HTTP_Request2 $object A HTTP request instance (otherwise one will be created)
*
* @return Services_Mailman
- *
- * @throws Services_Mailman_Exception
*/
public function setRequest(HTTP_Request2 $object = null)
{
@@ -193,19 +198,28 @@ protected function fetch($url)
{
$url = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
if (!$url) {
- throw new Services_Mailman_Exception(Services_Mailman_Exception::INVALID_URL);
+ throw new Services_Mailman_Exception(
+ 'Invalid URL',
+ Services_Mailman_Exception::INVALID_URL
+ );
}
try {
$this->request->setUrl($url);
$this->request->setMethod('GET');
$html = $this->request->send()->getBody();
} catch (HTTP_Request2_Exception $e) {
- throw new Services_Mailman_Exception($e);
+ throw new Services_Mailman_Exception(
+ $e,
+ Services_Mailman_Exception::HTML_FETCH
+ );
}
if (strlen($html)>5) {
return $html;
}
- throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_FETCH);
+ throw new Services_Mailman_Exception(
+ 'Could not fetch HTML',
+ Services_Mailman_Exception::HTML_FETCH
+ );
}
/**
@@ -232,7 +246,10 @@ public function lists($assoc = true)
$descs = $xpath->query('/html/body/table[1]/tr/td[2]');
$count = $names->length;
if (!$count) {
- throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
+ throw new Services_Mailman_Exception(
+ 'Failed to parse HTML',
+ Services_Mailman_Exception::HTML_PARSE
+ );
}
$a = array();
for ($i=0;$i < $count;$i++) {
@@ -268,7 +285,8 @@ public function member($string)
if (!is_string($string)) {
throw new Services_Mailman_Exception(
'member() expects parameter 1 to be string, ' .
- gettype($string) . ' given'
+ gettype($string) . ' given',
+ Services_Mailman_Exception::USER_INPUT
);
}
$path = '/' . $this->list . '/members';
@@ -301,12 +319,15 @@ public function member($string)
libxml_clear_errors();
$count = $queries['address']->length;
if (!$count) {
- throw new Services_Mailman_Exception('No match.');
+ throw new Services_Mailman_Exception(
+ 'No match',
+ Services_Mailman_Exception::NO_MATCH
+ );
}
$a = array();
for ($i=0;$i < $count;$i++) {
foreach ($queries as $key => $query) {
- $a[$i][$key]=$query->item($i)?$query->item($i)->nodeValue:'';
+ $a[$i][$key] = $query->item($i) ? $query->item($i)->nodeValue : '';
}
}
return $a;
@@ -348,9 +369,15 @@ public function unsubscribe($email)
return $this;
}
if ($h3) {
- throw new Services_Mailman_Exception(trim($h3->item(0)->nodeValue, ':'));
+ throw new Services_Mailman_Exception(
+ trim($h3->item(0)->nodeValue, ':'),
+ Services_Mailman_Exception::HTML_PARSE
+ );
}
- throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
+ throw new Services_Mailman_Exception(
+ 'Failed to parse HTML',
+ Services_Mailman_Exception::HTML_PARSE
+ );
}
/**
@@ -389,9 +416,15 @@ public function subscribe($email, $invite = false)
if ($h5 && $h5->item(0)->nodeValue == 'Successfully subscribed:') {
return $this;
} elseif ($h5) {
- throw new Services_Mailman_Exception(trim($h5->item(0)->nodeValue, ':'));
+ throw new Services_Mailman_Exception(
+ trim($h5->item(0)->nodeValue, ':'),
+ Services_Mailman_Exception::HTML_PARSE
+ );
}
- throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
+ throw new Services_Mailman_Exception(
+ 'Failed to parse HTML',
+ Services_Mailman_Exception::HTML_PARSE
+ );
}
/**
@@ -434,7 +467,8 @@ public function setOption($email, $option, $value)
if (!is_string($email)) {
throw new Services_Mailman_Exception(
'setOption() expects parameter 1 to be string, ' .
- gettype($email) . ' given'
+ gettype($email) . ' given',
+ Services_Mailman_Exception::USER_INPUT
);
}
$path = '/options/' . $this->list . '/' . str_replace('@', '--at--', $email);
@@ -490,7 +524,10 @@ public function setOption($email, $option, $value)
$query['options-submit'] = 'Submit+My+Changes';
$xp = "//input[@name='$option' and @checked]/@value";
} else {
- throw new Services_Mailman_Exception(Services_Mailman_Exception::INVALID_OPTION);
+ throw new Services_Mailman_Exception(
+ 'Invalid option',
+ Services_Mailman_Exception::INVALID_OPTION
+ );
}
$query = http_build_query($query, '', '&');
$url = dirname($this->adminUrl) . $path . '?' . $query;
@@ -505,15 +542,16 @@ public function setOption($email, $option, $value)
if ($query->item(0)) {
return $query->item(0)->nodeValue;
}
- throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
+ throw new Services_Mailman_Exception(
+ 'Failed to parse HTML',
+ Services_Mailman_Exception::HTML_PARSE
+ );
}
/**
* List members
*
* @return array Returns two nested arrays, the first contains email addresses, the second contains names
- *
- * @throws Services_Mailman_Exception
*/
public function members()
{
@@ -587,7 +625,10 @@ public function version()
if (preg_match('#version ([\d-.]+)#is', $content, $m)) {
return array_pop($m);
}
- throw new Services_Mailman_Exception(Services_Mailman_Exception::HTML_PARSE);
+ throw new Services_Mailman_Exception(
+ 'Failed to parse HTML',
+ Services_Mailman_Exception::HTML_PARSE
+ );
}
} //end
//eof
View
10 Services/Mailman/Exception.php
@@ -52,8 +52,10 @@
*/
class Services_Mailman_Exception extends Exception
{
- define('INVALID_URL', 'Invalid URL');
- define('HTML_FETCH', 'Could not fetch HTML');
- define('HTML_PARSE', 'Failed to parse HTML');
- define('INVALID_OPTION', 'Invalid Option');
+ const USER_INPUT = 1;
+ const INVALID_URL = 2;
+ const HTML_FETCH = 3;
+ const HTML_PARSE = 4;
+ const INVALID_OPTION = 5;
+ const NO_MATCH = 6;
}
View
3 tests/test_member.phpt
@@ -47,7 +47,6 @@ try {
?>
--EXPECT--
-
array(2) {
[0]=>
array(11) {
@@ -100,4 +99,4 @@ array(2) {
string(2) "en"
}
}
-Caught exception: No match.
+Caught exception: No match
View
2 tests/test_setdigest.phpt
@@ -48,4 +48,4 @@ try {
?>
--EXPECT--
-Caught exception: Failed to parse HTML.
+Caught exception: Failed to parse HTML

0 comments on commit 2c6360c

Please sign in to comment.