From f4a22b98e9f60592d68472d1c927cf26b7eb7996 Mon Sep 17 00:00:00 2001 From: mamuz Date: Mon, 17 Aug 2015 22:43:03 +0200 Subject: [PATCH] add errorhandler --- config/global.php | 5 --- public/index.php | 4 +-- src/Application/Service/ErrorHandler.php | 40 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 src/Application/Service/ErrorHandler.php diff --git a/config/global.php b/config/global.php index 2bf3ceb..fee0f50 100644 --- a/config/global.php +++ b/config/global.php @@ -4,11 +4,6 @@ 'dispatcher' => [ 'controllerDefaultNamespace' => 'PhalconSkeleton\Application\Controller', 'taskDefaultNamespace' => 'PhalconSkeleton\Application\Task', - 'errorForwarding' => [ - 'controller' => 'error', - 'notFoundAction' => 'notFound', - 'errorAction' => 'fatal', - ], ], 'view' => [ 'templatePath' => './view/', diff --git a/public/index.php b/public/index.php index 5a7cb24..c64ab3c 100644 --- a/public/index.php +++ b/public/index.php @@ -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); diff --git a/src/Application/Service/ErrorHandler.php b/src/Application/Service/ErrorHandler.php new file mode 100644 index 0000000..94bc738 --- /dev/null +++ b/src/Application/Service/ErrorHandler.php @@ -0,0 +1,40 @@ +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; + } + ); + } +}