Commits

David Winter committed 0fe0459

Refactored exception so that standard exception constructor can be used, but provide factory alternative.

Comments (0)

Files changed (3)

src/twitteryauth.php

 		}
 		catch (Exception $e)
 		{
-			throw new TwitteryauthRequestException($this->_oauth->debugInfo, $e);
+			throw TwitteryauthRequestException::get_instance($this->_oauth->debugInfo, $e);
 		}
 		
 		return false;

src/twitteryauth_request_exception.php

 
 class TwitteryauthRequestException extends Exception
 {
-	public function __construct($debug, $previous)
+	const SERVER_ERROR = 500;
+	
+	const MIME_PLAIN_TEXT = 'text/plain';
+	const MIME_HTML = 'text/html';
+	const MIME_JSON = 'application/json';
+	
+	public static function get_instance($debug, $previous)
 	{
-		if ($previous instanceof OAuthException 
-			&& isset($debug['headers_recv']) && isset($debug['body_recv']))
+		if ($previous instanceof OAuthException)
 		{
-			$headers = http_parse_headers($debug['headers_recv']);
-			$body = $debug['body_recv'];
-			
-			// code
-			if (isset($headers['Status']) && preg_match('/^(\d{3})/', $headers['Status'], $matches))
+			if (isset($debug['headers_recv']) && isset($debug['body_recv']))
 			{
-				$code = $matches[1];
+				$headers = http_parse_headers($debug['headers_recv']);
+				$body = $debug['body_recv'];
+				
+				$code = self::get_code_from_headers($headers);
+				$message = self::get_message_from_headers($headers, $body);
+				
+				return new self($message, $code, $previous);
 			}
-			else
-			{
-				$code = 500;
-			}
-			
-			// content type
-			if (isset($headers['Content-Type']))
-			{
-				$type = http_parse_params($headers['Content-Type']);
-				$type = $type->params[0];
-			}
-			
-			// error
-			switch ($type)
-			{
-				case 'application/json':
-					$error = json_decode($body);
-					$error = $error->error.': '.stripslashes($error->request);
-					
-					break;
+		}
+		
+		return new self($previous->getMessage(), $previous->getCode(), $previous);
+	}
+	
+	protected static function get_code_from_headers($headers)
+	{
+		if (isset($headers['Status']) && preg_match('/^(\d{3})/', $headers['Status'], $matches))
+		{
+			return $matches[1];
+		}
+
+		return self::SERVER_ERROR;
+	}
+	
+	protected static function get_message_from_headers($headers, $body)
+	{
+		switch (self::get_content_type($headers))
+		{
+			case self::MIME_JSON:
+				$error = json_decode($body);
+				return $error->error.': '.stripslashes($error->request);
+							
+			case self::MIME_HTML:
+				if (preg_match('/<h2>(.+)<\/h2>/', $body, $matches))
+				{
+					return $matches[1];
+				}
 				
-				case 'text/html':
-					if (preg_match('/<h2>(.+)<\/h2>/', $body, $matches))
-					{
-						$error = $matches[1];
-					}
-					else
-					{
-						$error = $body;
-					}
-					
-					break;
-				
-				default:
-					$error = $body;
-					
-					break;
-			}
-			
-			parent::__construct($error, $code, $previous);
-		}
-		else
-		{
-			parent::__construct($previous->getMessage(), $previous->getCode(), $previous);
+				return $body;
+							
+			default:
+				return $body;
 		}
 	}
+	
+	protected static function get_content_type($headers)
+	{
+		if (isset($headers['Content-Type']))
+		{
+			$type = http_parse_params($headers['Content-Type']);
+			return $type->params[0];
+		}
+		
+		return self::MIME_PLAIN_TEXT;
+	}
 }

tests/TwitteryauthRequestExceptionTest.php

 	public function testIgnoresIfStandardException()
 	{
 		$exception = new Exception('test');
-		$twitter_exception = new TwitteryauthRequestException(array(), $exception);
+		$twitter_exception = TwitteryauthRequestException::get_instance(array(), $exception);
 		
 		$this->assertEquals('test', $twitter_exception->getMessage());
 	}
 		);
 		
 		$exception = new OAuthException('test');
-		$twitter_exception = new TwitteryauthRequestException($debug, $exception);
+		$twitter_exception = TwitteryauthRequestException::get_instance($debug, $exception);
 		
 		$this->assertEquals('400', $twitter_exception->getCode());
 		$this->assertEquals('The user you are trying to remove from the list is not a member: /1/lists/members/destroy.json', 
 		);
 		
 		$exception = new OAuthException('test');
-		$twitter_exception = new TwitteryauthRequestException($debug, $exception);
+		$twitter_exception = TwitteryauthRequestException::get_instance($debug, $exception);
 		
 		$this->assertEquals('500', $twitter_exception->getCode());
 		$this->assertEquals('Something is technically wrong.',