]> dev.renevier.net Git - syj.git/blob - application/controllers/ErrorController.php
make release.py script simpler
[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->view->headLink()->appendStylesheet('css/generic.css', 'all');
14         $this->view->headLink()->appendStylesheet('css/error.css', 'all');
15     }
16
17     public function errorAction() {
18         $error = $this->_getParam('error_handler');
19
20         $error_code = 500; // default value: Internal Server Error
21         switch ($error->type) {
22             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
23             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
24             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
25                 $error_code = 404; // Not Found
26                 break;
27             default:
28                 if ($error->exception instanceof Syj_Exception_Request) {
29                     $error_code = 400; // Bad Request
30                 } else if ($error->exception instanceof Syj_Exception_Forbidden) {
31                     $error_code = 403; // Forbidden
32                 } else if ($error->exception instanceof Syj_Exception_NotFound) {
33                     $error_code = $error->exception->getCode();
34                 }
35                 break;
36         }
37         $this->httpError($error_code);
38
39         // Log exception, if logger available
40         if ($log = $this->getLog()) {
41             $log->crit($this->view->message, $error->exception);
42         }
43
44         if ($error_code == 400 and $error->request->isXmlHttpRequest()) {
45             return $this->_helper->json(array('message' => $error->exception->getMessage()));
46         } else if ($error->exception instanceof Syj_Exception_InvalidGeomUpload) {
47             // invalid file upload: we will redirect to main page
48             $this->_helper->SyjReset->resetPlaceHolders();
49             $this->_request->setControllerName('idx')->setActionName('error')->setDispatched(false);
50             return;
51         }
52
53         // conditionally display exceptions
54         if ($this->getInvokeArg('displayExceptions') == true) {
55             $this->view->exception = $error->exception;
56         }
57
58         $this->view->request   = $error->request;
59         $this->view->isServerError = ($error_code >= 500 and $error_code < 600);
60         $this->view->headTitle($this->view->translate("Oups, something went wrong"));
61     }
62
63     public function getLog() {
64         $bootstrap = $this->getInvokeArg('bootstrap');
65         if (!$bootstrap->hasPluginResource('Log')) {
66             return false;
67         }
68         $log = $bootstrap->getResource('Log');
69         return $log;
70     }
71
72 }