Make errors work correctly

This commit is contained in:
Evan Prodromou 2011-06-10 16:50:06 -04:00
parent 2d72a64841
commit 625e63a303
3 changed files with 43 additions and 12 deletions

View File

@ -157,6 +157,15 @@ class DomainStatusNetworkPlugin extends Plugin
return true;
}
function onLoginAction($action, &$login) {
$this->debug($action);
if (in_array($action, array('globalregister', 'globallogin', 'globalrecover'))) {
$login = true;
return false;
}
return true;
}
static function nicknameForDomain($domain)
{
$registered = self::registeredDomain($domain);

View File

@ -57,8 +57,17 @@ class GlobalregisterAction extends GlobalApiAction
function prepare($argarray)
{
parent::prepare($argarray);
return true;
try {
parent::prepare($argarray);
return true;
} catch (ClientException $e) {
$this->showError($e->getMessage(), $e->getCode());
return false;
} catch (Exception $e) {
common_log(LOG_ERR, $e->getMessage());
$this->showError(_('An internal error occurred.'), 500);
return false;
}
}
/**
@ -75,10 +84,10 @@ class GlobalregisterAction extends GlobalApiAction
DomainStatusNetworkPlugin::registerEmail($this->email, true);
$this->showSuccess();
} catch (ClientException $e) {
$this->showError($e->getMessage());
$this->showError($e->getMessage(), $e->getCode());
} catch (Exception $e) {
common_log(LOG_ERR, $e->getMessage());
$this->showError(_('An internal error occurred.'));
$this->showError(_('An internal error occurred.'), 500);
}
return;

View File

@ -64,13 +64,13 @@ class GlobalApiAction extends Action
parent::prepare($args);
if (!common_config('globalapi', 'enabled')) {
throw new ClientException(_('Global API not enabled.'));
throw new ClientException(_('Global API not enabled.'), 403);
}
$apikey = $this->trimmed('apikey');
if (empty($apikey)) {
throw new ClientException(_('No API key.'));
throw new ClientException(_('No API key.'), 403);
}
$expected = common_config('globalapi', 'key');
@ -78,7 +78,7 @@ class GlobalApiAction extends Action
if ($expected != $apikey) {
// FIXME: increment a counter by IP address to prevent brute-force
// attacks on the key.
throw new ClientException(_('Bad API key.'));
throw new ClientException(_('Bad API key.'), 403);
}
$email = common_canonical_email($this->trimmed('email'));
@ -96,23 +96,36 @@ class GlobalApiAction extends Action
return true;
}
function showError($message)
function showError($message, $code=400)
{
$this->showOutput(array('error' => $message));
$this->showOutput(array('error' => $message), $code);
}
function showSuccess($values=null)
function showSuccess($values=null, $code=200)
{
if (empty($values)) {
$values = array();
}
$values['success'] = 1;
$this->showOutput($values);
$this->showOutput($values, $code);
}
function showOutput($values)
function showOutput($values, $code)
{
if (array_key_exists($code, ClientErrorAction::$status)) {
$status_string = ClientErrorAction::$status[$code];
} else if (array_key_exists($code, ServerErrorAction::$status)) {
$status_string = ServerErrorAction::$status[$code];
} else {
// bad code!
$code = 500;
$status_string = ServerErrorAction::$status[$code];
}
header('HTTP/1.1 '.$code.' '.$status_string);
header('Content-Type: application/json; charset=utf-8');
print(json_encode($values));
print("\n");
}
}