Skip to content

Commit

Permalink
Throw exceptions as appropriate, but allow the Kohana exception
Browse files Browse the repository at this point in the history
handling framework to handle the exception and delegate to our
template, which will JSON encode the response.
  • Loading branch information
bharat committed Jun 19, 2010
1 parent 41ca2b0 commit 456d54e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 37 deletions.
41 changes: 6 additions & 35 deletions modules/rest/controllers/rest.php
Expand Up @@ -81,41 +81,12 @@ public function __call($function, $args) {
}

$response = call_user_func(array($handler_class, $handler_method), $request);
} catch (Exception $e) {
$response = $this->_format_exception_response($e);
rest::reply($response);
} catch (ORM_Validation_Exception $e) {
// Note: this is totally insufficient because it doesn't take into account localization. We
// either need to map the result values to localized strings in the application code, or every
// client needs its own l10n string set.
throw new Rest_Exception("Bad Request", 400, $e->validation->errors());
}

rest::reply($response);
}

private function _format_exception_response($e) {
// Add this exception to the log
Kohana_Log::add("error", Kohana_Exception::text($e));

$rest_exception = array();
if ($e instanceof ORM_Validation_Exception) {
$detail_response = true;
$rest_exception["code"] = 400;
$rest_exception["message"] = "Validation errors";
$rest_exception["fields"] = $e->validation->errors();
} else if ($e instanceof Rest_Exception) {
$rest_exception["code"] = $e->getCode();
if ($e->getMessage() != "Bad Request") {
$rest_exception["message"] = "Bad Request";
$rest_exception["fields"] = array("type", $e->getMessage());
} else {
$rest_exception["message"] = $e->getMessage();
}
} else {
$rest_exception["code"] = 500;
$rest_exception["message"] = t("Remote server call failed. Please contact the Adminstrator.");
}

if (!headers_sent()) {
header($rest_exception["code"] == 500 ? "HTTP/1.1 500 Internal Server Error" :
"HTTP/1.1 400 Bad Request");
}

return $rest_exception;
}
}
11 changes: 9 additions & 2 deletions modules/rest/libraries/Rest_Exception.php
Expand Up @@ -18,13 +18,20 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Rest_Exception_Core extends Kohana_Exception {
public function __construct($message, $code) {
var $response = array();

public function __construct($message, $code, $response) {
parent::__construct($message, null, $code);
$this->response = $response;
}

public function sendHeaders() {
if (!headers_sent()) {
header("HTTP/1.1 " . $this->getCode() . "Bad Request");
header("HTTP/1.1 " . $this->getCode() . " " . $this->getMessage());
}
}

public function getTemplate() {
return "error_rest";
}
}
2 changes: 2 additions & 0 deletions modules/rest/views/error_rest.php
@@ -0,0 +1,2 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<?= json_encode($e->response) ?>

0 comments on commit 456d54e

Please sign in to comment.