Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
And default to production environment which show nice error messages.
  • Loading branch information
tasuki committed Apr 28, 2013
1 parent 891dccf commit 8aed87f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
7 changes: 7 additions & 0 deletions application/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
*/
if (isset($_SERVER['KOHANA_ENV'])) {
Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
} else {
Kohana::$environment = Kohana::PRODUCTION;
}

/**
Expand Down Expand Up @@ -103,5 +105,10 @@
Route::set('admin', 'admin(/<action>(/<key>))')
->defaults(array('controller' => 'admin'));

Route::set('error', 'error/<code>(/<message>)', array(
'code' => '[0-9]+',
'message' => '.+',
))->defaults(array('controller' => 'gallery', 'action' => 'error'));

Route::set('gallery', '(<dir>)', array('dir' => '.+'))
->defaults(array('controller' => 'gallery'));
22 changes: 21 additions & 1 deletion application/classes/controller/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public function action_index()

// load directory model
$gallery_dir = Kohana::$config->load('application.dir.gallery');
$directory = new Model_Directory(DOCROOT . "$gallery_dir/$dir");
try {
$directory = new Model_Directory(DOCROOT . "$gallery_dir/$dir");
} catch (UnexpectedValueException $e) {
throw new HTTP_Exception_404("Gallery ':gallery' not found.", array(':gallery' => $dir));
}

// get sub-galleries
$view->galleries = array();
Expand Down Expand Up @@ -73,6 +77,22 @@ public function action_index()
$this->template->body = $view;
}

public function action_error()
{
$code = $this->request->param('code');
$message = rawurldecode($this->request->param('message'));

$title = "$code: " . Response::$messages[$code];
$this->set_title($title);

$view = View::factory("error");
$view->crumbs = self::get_crumbs($title);
$view->calibration = self::get_calibration();
$view->message = $message;

$this->template->body = $view;
}

/**
* Assemble breadcrumbs
*
Expand Down
5 changes: 5 additions & 0 deletions application/views/error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php echo View::factory('gallery/crumbs')->set('crumbs', $crumbs); ?>

<div class="navigation"><?php echo $message ?></div>

<?php echo View::factory('gallery/footer')->set('calibration', $calibration); ?>
33 changes: 29 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,32 @@
* Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
* If no source is specified, the URI will be automatically detected.
*/
echo Request::factory()
->execute()
->send_headers()
->body();
$request = Request::factory();

try {
$response = $request->execute();
} catch (Exception $e) {
if (Kohana::$environment === Kohana::DEVELOPMENT) {
throw $e;
}

Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e));

if ($e instanceof HTTP_Exception) {
$code = $e->getCode();
$message = $e->getMessage();
} else {
$code = 500;
$message = 'Something went horribly wrong!';
}

$url = Route::get('error')->uri(array(
'code' => $code,
'message' => rawurlencode($message),
));

$response = Request::factory($url)->execute();
$response->status($code);
}

echo $response->send_headers()->body();

0 comments on commit 8aed87f

Please sign in to comment.