diff options
author | zach <zach@copley.name> | 2008-07-20 03:09:05 -0400 |
---|---|---|
committer | zach <zach@copley.name> | 2008-07-20 03:09:05 -0400 |
commit | 140689800b03ff704f6c70260c2b740ad41578c0 (patch) | |
tree | 7d9fe90e1ae731f9275910302ec6063df2486d62 /lib/twitterapi.php | |
parent | 00a6f3d01510e01a5dcc0999a50fba7bafbcd5e1 (diff) |
Twitter-compatible API - Error handling that better matches Twitter's
darcs-hash:20080720070905-ca946-dda57dd92210461361fd58b7a3244bf24c01e801.gz
Diffstat (limited to 'lib/twitterapi.php')
-rw-r--r-- | lib/twitterapi.php | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/lib/twitterapi.php b/lib/twitterapi.php index 06f491fb6..12416bc36 100644 --- a/lib/twitterapi.php +++ b/lib/twitterapi.php @@ -280,7 +280,54 @@ class TwitterapiAction extends Action { break; } } - + + function client_error($msg, $code = 400, $content_type = 'json') { + + static $status = array(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'); + + $action = $this->trimmed('action'); + + common_debug("User error '$code' on '$action': $msg", __FILE__); + + if (!array_key_exists($code, $status)) { + $code = 400; + } + + $status_string = $status[$code]; + header('HTTP/1.1 '.$code.' '.$status_string); + + if ($content_type == 'xml') { + common_start_xml(); + common_element_start('hash'); + common_element('error', NULL, $msg); + common_element('request', NULL, $_SERVER['REQUEST_URI']); + common_element_end('hash'); + common_end_xml(); + } else { + $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']); + print(json_encode($error_array)); + } + + exit(); + } + function init_twitter_rss() { common_start_xml(); common_element_start('rss', array('version' => '2.0')); |