Permalink
Browse files

Added PhealHTTPException for handling HTTP Errors. Example: 404 throw…

…s PhealHTTPException with code 404
  • Loading branch information...
1 parent 66ad86f commit 40a91d6fc1d9b8468fabf120af05f55da7cb9e50 @dhoffend committed Feb 16, 2011
Showing with 134 additions and 1 deletion.
  1. +23 −1 Pheal.php
  2. +111 −0 PhealHTTPException.php
View
@@ -138,6 +138,11 @@ private function request_xml($scope, $name, $opts)
// parse
$element = new SimpleXMLElement($this->xml);
+ // just forward HTTP Errors
+ } catch(PhealHTTPException $e) {
+ throw $e;
+
+ // other request errors
} catch(Exception $e) {
// log + throw error
PhealConfig::getInstance()->log->errorLog($scope,$name,$opts,$e->getCode() . ': ' . $e->getMessage());
@@ -232,9 +237,17 @@ public static function request_http_curl($url,$opts)
$errno = curl_errno(self::$curl);
$error = curl_error(self::$curl);
+ // response http headers
+ $httpCode = curl_getinfo(self::$curl, CURLINFO_HTTP_CODE);
+
if(!PhealConfig::getInstance()->http_keepalive)
self::disconnect();
+ // http errors
+ if($httpCode >= 400)
+ throw new PhealHTTPException($httpCode, $url);
+
+ // curl errors
if($errno)
throw new Exception($error, $errno);
else
@@ -290,8 +303,17 @@ public static function request_http_file($url,$opts)
} else {
$result = @file_get_contents($url);
}
+
+ // check for http errors via magic $http_response_header
+ $httpCode = 200;
+ if(isset($http_response_header[0]))
+ list($httpVersion,$httpCode,$httpMsg) = explode(' ', $http_response_header[0], 3);
+
+ // throw http error
+ if(is_numeric($httpCode) && $httpCode >= 400)
+ throw new PhealHTTPException($httpCode, $url);
- // throw error
+ // throw error
if($result === false) {
$message = ($php_errormsg ? $php_errormsg : 'HTTP Request Failed');
View
@@ -0,0 +1,111 @@
+<?php
+/*
+ MIT License
+ Copyright (c) 2010 Peter Petermann
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+*/
+/**
+ * basic Pheal Exception, all Pheal exceptions should be derived from this
+ */
+class PhealHTTPException extends PhealException
+{
+ /**
+ * http response code to string conversation
+ * @var array list of httpcode > description
+ */
+ static private $codes = array(
+
+ // Informational 1xx
+ 100 => "Continue",
+ 101 => "Switching Protocols",
+
+ // Successful 2xx
+ 200 => "OK",
+ 201 => "Created",
+ 202 => "Accepted",
+ 203 => "Non-Authoritative Information",
+ 204 => "No Content",
+ 205 => "Reset Content",
+ 206 => "Partial Content",
+
+ // Redirection 3xx
+ 300 => "Multiple Choices",
+ 301 => "Moved Permanently",
+ 302 => "Found",
+ 303 => "See Other",
+ 304 => "Not Modified",
+ 305 => "Use Proxy",
+ 306 => "(Unused)",
+ 307 => "Temporary Redirect",
+
+ // Client Error 4xx
+ 400 => "Bad Request",
+ 401 => "Unauthorized",
+ 402 => "Payment Required",
+ 403 => "Forbidden",
+ 404 => "Not Found",
+ 405 => "Method Not Allowed",
+ 406 => "Not Acceptable",
+ 407 => "Proxy Authentication Required",
+ 408 => "Request Timeout",
+ 409 => "Conflict",
+ 410 => "Gone",
+ 411 => "Length Required",
+ 412 => "Precondition Failed",
+ 413 => "Request Entity Too Large",
+ 414 => "Request-URI Too Long",
+ 415 => "Unsupported Media Type",
+ 416 => "Requested Range Not Satisfiable",
+ 417 => "Expectation Failed",
+
+ // Server Error 5xx
+ 500 => "Internal Server Error",
+ 501 => "Not Implemented",
+ 502 => "Bad Gateway",
+ 503 => "Service Unavailable",
+ 504 => "Gateway Timeout",
+ 505 => "HTTP Version Not Supported"
+ );
+
+ /**
+ * requested url
+ * @var string
+ */
+ public $url;
+
+ /**
+ * construct exception with http reponse code and url
+ * @param int $code
+ * @param string $url
+ */
+ public function __construct($code, $url)
+ {
+ // safe url and strip apikey for security reasons
+ $this->url = preg_replace("/(apikey=)[a-z0-9]*/i","\\1...",$url);
+
+ // build error message
+ $message = self::$codes[$code] ? self::$codes[$code] : 'Unknown HTTP Code';
+
+ parent::__construct($message, $code);
+ }
+}

0 comments on commit 40a91d6

Please sign in to comment.