From 0aafec0e59c3ef48e8e2e6fc4a0b8aa458798619 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 8 Jun 2010 14:32:53 -0700 Subject: The status message for an HTTP 400 status should always be 'Bad Request', if I'm reading the specification right. --- modules/rest/libraries/Rest_Exception.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/rest/libraries') diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php index 505c2e7a..aa5b3281 100644 --- a/modules/rest/libraries/Rest_Exception.php +++ b/modules/rest/libraries/Rest_Exception.php @@ -24,7 +24,7 @@ class Rest_Exception_Core extends Kohana_Exception { public function sendHeaders() { if (!headers_sent()) { - header("HTTP/1.1 " . $this->getCode() . " " . $this->getMessage()); + header("HTTP/1.1 " . $this->getCode() . "Bad Request"); } } } \ No newline at end of file -- cgit v1.2.3 From 456d54ea2dccbe55a2efd89ecb4bde29fb91b619 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 19 Jun 2010 13:53:22 -0700 Subject: Throw exceptions as appropriate, but allow the Kohana exception handling framework to handle the exception and delegate to our template, which will JSON encode the response. --- modules/rest/controllers/rest.php | 41 +++++-------------------------- modules/rest/libraries/Rest_Exception.php | 11 +++++++-- modules/rest/views/error_rest.php | 2 ++ 3 files changed, 17 insertions(+), 37 deletions(-) create mode 100644 modules/rest/views/error_rest.php (limited to 'modules/rest/libraries') diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index ccccc762..f8a46515 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -81,41 +81,12 @@ class Rest_Controller extends Controller { } $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; } } \ No newline at end of file diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php index aa5b3281..c5baec63 100644 --- a/modules/rest/libraries/Rest_Exception.php +++ b/modules/rest/libraries/Rest_Exception.php @@ -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"; + } } \ No newline at end of file diff --git a/modules/rest/views/error_rest.php b/modules/rest/views/error_rest.php new file mode 100644 index 00000000..c018378e --- /dev/null +++ b/modules/rest/views/error_rest.php @@ -0,0 +1,2 @@ + +response) ?> \ No newline at end of file -- cgit v1.2.3 From cd96ed887323c4006fa1a2008f153937cfa2f0ea Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 19 Jun 2010 14:13:34 -0700 Subject: $response is optional in the Rest_Exception constructor. --- modules/rest/libraries/Rest_Exception.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/rest/libraries') diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php index c5baec63..1257e3cf 100644 --- a/modules/rest/libraries/Rest_Exception.php +++ b/modules/rest/libraries/Rest_Exception.php @@ -20,7 +20,7 @@ class Rest_Exception_Core extends Kohana_Exception { var $response = array(); - public function __construct($message, $code, $response) { + public function __construct($message, $code, $response=array()) { parent::__construct($message, null, $code); $this->response = $response; } -- cgit v1.2.3 From f6025026eb4798774044fcd12f0f09313073141a Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 20 Jun 2010 08:50:19 -0700 Subject: Rename error_rest.php to error_rest.json.php so that we specify the result type in the filename as is our convention for views. --- modules/rest/libraries/Rest_Exception.php | 2 +- modules/rest/views/error_rest.json.php | 2 ++ modules/rest/views/error_rest.php | 2 -- 3 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 modules/rest/views/error_rest.json.php delete mode 100644 modules/rest/views/error_rest.php (limited to 'modules/rest/libraries') diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php index 1257e3cf..087da939 100644 --- a/modules/rest/libraries/Rest_Exception.php +++ b/modules/rest/libraries/Rest_Exception.php @@ -32,6 +32,6 @@ class Rest_Exception_Core extends Kohana_Exception { } public function getTemplate() { - return "error_rest"; + return "error_rest.json"; } } \ No newline at end of file diff --git a/modules/rest/views/error_rest.json.php b/modules/rest/views/error_rest.json.php new file mode 100644 index 00000000..c018378e --- /dev/null +++ b/modules/rest/views/error_rest.json.php @@ -0,0 +1,2 @@ + +response) ?> \ No newline at end of file diff --git a/modules/rest/views/error_rest.php b/modules/rest/views/error_rest.php deleted file mode 100644 index c018378e..00000000 --- a/modules/rest/views/error_rest.php +++ /dev/null @@ -1,2 +0,0 @@ - -response) ?> \ No newline at end of file -- cgit v1.2.3