Skip to content

Commit

Permalink
Add lookup for http reason phrase for standard http codes.
Browse files Browse the repository at this point in the history
The logic now uses the passed reason phrase if there is one, if not it
then attempts to lookup a reason phrase based on the http code, if that
fails it then uses the error message.
  • Loading branch information
trevormorse committed Jun 15, 2012
1 parent 6a75bef commit 2ff290c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/frapi/library/Frapi/Error.php
Expand Up @@ -244,11 +244,22 @@ private static function _get($error_name, $error_msg = false, $http_code = false

if ($http_phrase !== false) {
$error['http_phrase'] = $http_phrase;
} else if (isset(Frapi_Response::$http_reason[$error['http_code']])){
$error['http_phrase'] = Frapi_Response::$http_reason[$error['http_code']];
} else if ($error['message'] !== false) {
$error['http_phrase'] = $error['message'];
}

return $error;
}

if ($http_phrase === false) {
if (isset(Frapi_Response::$http_reason[$http_code])) {
$http_phrase = Frapi_Response::$http_reason[$http_code];
} else if ($error_msg !== false) {
$http_phrase = $error_msg;
}
}

return array(
'name' => $error_name,
'message' => $error_msg !== false ? $error_msg : $error_name,
Expand Down
55 changes: 55 additions & 0 deletions src/frapi/library/Frapi/Response.php
Expand Up @@ -50,6 +50,59 @@ class Frapi_Response
*/
protected $data = array();

/**
* Mapping of http code to reason phrases
*
* Standard http codes to standard reason phrase mapping
*
* @var array
*/
public static $http_reason = array (
100 => 'Continue',
101 => 'Switching Protocols',
103 => 'Checkpoint',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
306 => 'Switch Proxy',
307 => 'Temporary Redirect',
308 => 'Resume Incomplete',
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',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
511 => 'Network Authentication Required',
);

/**
* The constructor
*
Expand Down Expand Up @@ -78,6 +131,8 @@ public function __construct(array $response)

if (isset($response['reason_phrase'])) {
$this->http_phrase = $response['reason_phrase'];
} else if (isset(self::$http_reason[$this->http_code])){
$this->http_phrase = self::$http_reason[$this->http_code];
}

if (isset($response['data'])) {
Expand Down

0 comments on commit 2ff290c

Please sign in to comment.