Skip to content

Commit

Permalink
add errorhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
mamuz committed Aug 17, 2015
1 parent 6ad9272 commit f4a22b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
5 changes: 0 additions & 5 deletions config/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
'dispatcher' => [
'controllerDefaultNamespace' => 'PhalconSkeleton\Application\Controller',
'taskDefaultNamespace' => 'PhalconSkeleton\Application\Task',
'errorForwarding' => [
'controller' => 'error',
'notFoundAction' => 'notFound',
'errorAction' => 'fatal',
],
],
'view' => [
'templatePath' => './view/',
Expand Down
4 changes: 2 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
include './vendor/autoload.php';

$env = trim(file_get_contents('./ENV'));
$configCache = './data/cache/config.php';
$configCache = $env === 'development' ? null : './data/cache/config.php';

Phapp\Application\Bootstrap::init($env, $env === 'development' ? null : $configCache)->runApplicationOn($_SERVER);
Phapp\Application\Bootstrap::init($env, $configCache)->runApplicationOn($_SERVER);
40 changes: 40 additions & 0 deletions src/Application/Service/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Phpg\Application\Service;

use Phalcon\Di;
use Phalcon\Dispatcher as PhalconDispatcher;
use Phalcon\Events\Event;
use Phalcon\Mvc;
use Phapp\Application\Service\InjectableInterface;

class Dispatcher implements InjectableInterface
{
public static function injectTo(Di $di)
{
/** @var \Phalcon\Mvc\Dispatcher $dispatcher */
$dispatcher = $di->getShared('dispatcher');
$dispatcher->getEventsManager()->attach(
'dispatch:beforeException',
function (Event $event, PhalconDispatcher $dispatcher, \Exception $e) {
$logger = $dispatcher->getDI()->get('logger');
$logger->error($e->getMessage());
if ($dispatcher instanceof \Phalcon\Mvc\Dispatcher) {
if ($e instanceof Mvc\Dispatcher\Exception) {
$action = 'notFound';
} else {
$action = 'fatal';
if ($dispatcher->getDI()->has('response')) {
/** @var \Phalcon\Http\Response $response */
$response = $dispatcher->getDI()->get('response');
$response->setStatusCode(500, "Internal Server Error");
}
}
$dispatcher->setNamespaceName($dispatcher->getDefaultNamespace());
$dispatcher->forward(['controller' => 'error', 'action' => $action]);
}
return false;
}
);
}
}

0 comments on commit f4a22b9

Please sign in to comment.