From 41ca2b0195bf6a29429dfc5405f3c2073b1c3aba Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 19 Jun 2010 13:52:48 -0700 Subject: Rework our exception framework to fit into Kohana's model better. Instead of overwriting Kohana_Exception::handle() (which we were doing in MY_Kohana_Exception) we instead use their existing template system. gallery/views/kohana/error.php overrides system/views/kohana/error.php and is the standard error template for all exceptions. Our version of error.php figures out the appropriate view based on context (cli, authenticated admin, guest viewing a 404, guest viewing a system error) and delegates appropriately. Each delegated view has a narrow responsibility. This paves the way for us to add new error views per module. For example, the rest module will define its own template in Rest_Exception and then its exceptions can be rendered the way that it wants (json encoded, in that case). --- modules/gallery/helpers/item_rest.php | 24 +- modules/gallery/libraries/MY_Kohana_Exception.php | 62 ----- modules/gallery/views/error_admin.html.php | 272 ++++++++++++++++++ modules/gallery/views/error_cli.txt.php | 3 + modules/gallery/views/error_user.html.php | 42 +++ modules/gallery/views/kohana/error.php | 321 +++------------------- 6 files changed, 372 insertions(+), 352 deletions(-) create mode 100644 modules/gallery/views/error_admin.html.php create mode 100644 modules/gallery/views/error_cli.txt.php create mode 100644 modules/gallery/views/error_user.html.php (limited to 'modules') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 0839b144..6869181d 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -161,20 +161,22 @@ class item_rest_Core { case "photo": case "movie": if (empty($request->file)) { - throw new Rest_Exception("file: Upload failed", 400); + throw new Rest_Exception( + "Bad Request", 400, array("errors" => array("file" => t("Upload failed")))); } - $item->type = $entity->type; - $item->parent_id = $parent->id; - $item->set_data_file($request->file); - $item->name = $entity->name; - $item->title = isset($entity->title) ? $entity->title : $entity->name; - $item->description = isset($entity->description) ? $entity->description : null; - $item->slug = isset($entity->slug) ? $entity->slug : null; - $item->save(); - break; + $item->type = $entity->type; + $item->parent_id = $parent->id; + $item->set_data_file($request->file); + $item->name = $entity->name; + $item->title = isset($entity->title) ? $entity->title : $entity->name; + $item->description = isset($entity->description) ? $entity->description : null; + $item->slug = isset($entity->slug) ? $entity->slug : null; + $item->save(); + break; default: - throw new Rest_Exception("Invalid type: $entity->type", 400); + throw new Rest_Exception( + "Bad Request", 400, array("errors" => array("type" => "invalid"))); } return array("url" => rest::url("item", $item)); diff --git a/modules/gallery/libraries/MY_Kohana_Exception.php b/modules/gallery/libraries/MY_Kohana_Exception.php index 11556f7a..72cb2ac0 100644 --- a/modules/gallery/libraries/MY_Kohana_Exception.php +++ b/modules/gallery/libraries/MY_Kohana_Exception.php @@ -29,68 +29,6 @@ class Kohana_Exception extends Kohana_Exception_Core { $e->getTraceAsString()); } - public static function handle(Exception $e) { - if ($e instanceof ORM_Validation_Exception) { - Kohana_Log::add("error", "Validation errors: " . print_r($e->validation->errors(), 1)); - } - try { - $user = identity::active_user(); - $try_themed_view = $user && !$user->admin; - } catch (Exception $e2) { - $try_themed_view = false; - } - - if ($try_themed_view) { - try { - return self::_show_themed_error_page($e); - } catch (Exception $e3) { - Kohana_Log::add("error", "Exception in exception handling code: " . self::text($e3)); - return parent::handle($e); - } - } else { - return parent::handle($e); - } - } - - /** - * Shows a themed error page. - * @see Kohana_Exception::handle - */ - private static function _show_themed_error_page(Exception $e) { - // Create a text version of the exception - $error = Kohana_Exception::text($e); - - // Add this exception to the log - Kohana_Log::add("error", $error); - - // Manually save logs after exceptions - Kohana_Log::save(); - - if (!headers_sent()) { - if ($e instanceof Kohana_Exception) { - $e->sendHeaders(); - } else { - header("HTTP/1.1 500 Internal Server Error"); - } - } - - $view = new Theme_View("page.html", "other", "error"); - if ($e instanceof Kohana_404_Exception) { - $view->page_title = t("Dang... Page not found!"); - $view->content = new View("error_404.html"); - $user = identity::active_user(); - $view->content->is_guest = $user && $user->guest; - if ($view->content->is_guest) { - $view->content->login_form = new View("login_ajax.html"); - $view->content->login_form->form = auth::get_login_form("login/auth_html"); - } - } else { - $view->page_title = t("Dang... Something went wrong!"); - $view->content = new View("error.html"); - } - print $view; - } - /** * @see Kohana_Exception::dump() */ diff --git a/modules/gallery/views/error_admin.html.php b/modules/gallery/views/error_admin.html.php new file mode 100644 index 00000000..40eb7374 --- /dev/null +++ b/modules/gallery/views/error_admin.html.php @@ -0,0 +1,272 @@ + + + + + + + + <?= t("Something went wrong!") ?> + + + + + +
+

+ +

+

+ +

+
+
+

+ +

+
+

+ + [ ]: + + + + +

+
+
    +
  1. +

    + + [ ] + +

    + +
    + $row): ?>"> + +
    +
  2. + + + $step): ?> +
  3. +

    + + + + [ ] + + [ ] + + + {} + + + » + ( + + ) +

    + + + + + + +
  4. + + +
+ + +
+

+ " onclick="return koggle('')"> +

+ +
+
+ + diff --git a/modules/gallery/views/error_cli.txt.php b/modules/gallery/views/error_cli.txt.php new file mode 100644 index 00000000..b4f87fa6 --- /dev/null +++ b/modules/gallery/views/error_cli.txt.php @@ -0,0 +1,3 @@ + + + diff --git a/modules/gallery/views/error_user.html.php b/modules/gallery/views/error_user.html.php new file mode 100644 index 00000000..74c6a8fb --- /dev/null +++ b/modules/gallery/views/error_user.html.php @@ -0,0 +1,42 @@ + + + + + + + <?= t("Something went wrong!") ?> + + +
+

+ +

+

+ +

+

+ +

+
+ + diff --git a/modules/gallery/views/kohana/error.php b/modules/gallery/views/kohana/error.php index d55105a0..b0f0e907 100644 --- a/modules/gallery/views/kohana/error.php +++ b/modules/gallery/views/kohana/error.php @@ -1,280 +1,43 @@ - - - - - - - <?= t("Something went wrong!") ?> - - - - - - admin) ?> -
-

- -

-

- -

- -

- -

- -
- -
-

- -

-
-

- - [ ]: - - - - -

-
-
    -
  1. -

    - - [ ] - -

    - -
    - $row): ?>"> - -
    -
  2. - - - $step): ?> -
  3. -

    - - - - [ ] - - [ ] - - - {} - - - » - ( - - ) -

    - - - - - - -
  4. - - -
- - -
-

- " onclick="return koggle('')"> -

- -
-
- - - +validation->errors(), 1)); +} + +if (php_sapi_name() == "cli") { + include Kohana::find_file("views", "error_cli.txt"); + return; +} + +try { + // Admins get a special error page + $user = identity::active_user(); + if ($user && $user->admin) { + include Kohana::find_file("views", "error_admin.html"); + return; + } +} catch (Exception $ignored) { +} + +// Try to show a themed error page for 404 errors +if ($e instanceof Kohana_404_Exception) { + $view = new Theme_View("page.html", "other", "error"); + $view->page_title = t("Dang... Page not found!"); + $view->content = new View("error_404.html"); + $user = identity::active_user(); + $view->content->is_guest = $user && $user->guest; + if ($view->content->is_guest) { + $view->content->login_form = new View("login_ajax.html"); + $view->content->login_form->form = auth::get_login_form("login/auth_html"); + } + print $view; + return; +} + +header("HTTP/1.1 500 Internal Server Error"); +include Kohana::find_file("views", "error_user.html"); +?> -- cgit v1.2.3