]> dev.renevier.net Git - syj.git/blob - application/controllers/ErrorController.php
cache js, css and png files
[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_NotFound) {
32                     $error_code = $error->exception->getCode();
33                 }
34                 break;
35         }
36         $this->httpError($error_code);
37
38         // Log exception, if logger available
39         if ($log = $this->getLog()) {
40             $log->crit($this->view->message, $error->exception);
41         }
42
43         if ($error_code == 400 and $error->request->isXmlHttpRequest()) {
44             return $this->_helper->json(array('message' => $error->exception->getMessage()));
45         } else if ($error->exception instanceof Syj_Exception_InvalidGeomUpload) {
46             // invalid file upload: we will redirect to main page
47             $this->_helper->SyjReset->resetPlaceHolders();
48             $this->_request->setControllerName('idx')->setActionName('error')->setDispatched(false);
49             return;
50         }
51
52         // conditionally display exceptions
53         if ($this->getInvokeArg('displayExceptions') == true) {
54             $this->view->exception = $error->exception;
55         }
56
57         $this->view->request   = $error->request;
58         $this->view->isServerError = ($error_code >= 500 and $error_code < 600);
59         $this->view->headTitle($this->view->translate("Oups, something went wrong"));
60     }
61
62     public function getLog() {
63         $bootstrap = $this->getInvokeArg('bootstrap');
64         if (!$bootstrap->hasPluginResource('Log')) {
65             return false;
66         }
67         $log = $bootstrap->getResource('Log');
68         return $log;
69     }
70
71 }