]> dev.renevier.net Git - syj.git/blob - application/controllers/ErrorController.php
routes profile
[syj.git] / application / controllers / ErrorController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class ErrorController extends Zend_Controller_Action
6 {
7     protected function httpError($code) {
8         $this->getResponse()->setHttpResponseCode($code);
9     }
10
11     public function init() {
12         $this->_helper->SyjReset->resetPlaceHolders();
13         $this->_helper->SyjMedias->addStyleSheets('error');
14     }
15
16     public function errorAction() {
17         $error = $this->_getParam('error_handler');
18
19         $error_code = 500; // default value: Internal Server Error
20         switch ($error->type) {
21             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
22             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
23             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
24                 $error_code = 404; // Not Found
25                 break;
26             default:
27                 if ($error->exception instanceof Syj_Exception_Request) {
28                     $error_code = 400; // Bad Request
29                 } else if ($error->exception instanceof Syj_Exception_Forbidden) {
30                     $error_code = 403; // Forbidden
31                 } else if ($error->exception instanceof Syj_Exception_NotImplemented) {
32                     $error_code = 501; // Not Implemented
33                 } else if ($error->exception instanceof Syj_Exception_NotFound) {
34                     $error_code = $error->exception->getCode();
35                 }
36                 break;
37         }
38         $this->httpError($error_code);
39
40         // Log exception, if logger available
41         if ($log = $this->getLog()) {
42             $log->crit($this->view->message, $error->exception);
43         }
44
45         if ($error_code == 400 and $error->request->isXmlHttpRequest()) {
46             return $this->_helper->json(array('message' => $error->exception->getMessage()));
47         } else if ($error->exception instanceof Syj_Exception_InvalidGeomUpload) {
48             // invalid file upload: we will redirect to main page
49             $this->_helper->SyjReset->resetPlaceHolders();
50             $this->_request->setControllerName('idx')->setActionName('error')->setDispatched(false);
51             return;
52         }
53
54         // conditionally display exceptions
55         if ($this->getInvokeArg('displayExceptions') == true) {
56             $this->view->exception = $error->exception;
57         }
58
59         $this->view->request   = $error->request;
60         $this->view->isServerError = ($error_code >= 500 and $error_code < 600);
61         $this->view->headTitle($this->view->translate("Oups, something went wrong"));
62     }
63
64     public function getLog() {
65         $bootstrap = $this->getInvokeArg('bootstrap');
66         if (!$bootstrap->hasPluginResource('Log')) {
67             return false;
68         }
69         $log = $bootstrap->getResource('Log');
70         return $log;
71     }
72
73 }