]> dev.renevier.net Git - syj.git/blob - application/controllers/ErrorController.php
version 0.1
[syj.git] / application / controllers / ErrorController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 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         $this->view->message = Zend_Http_Response::responseCodeAsText($code);
10     }
11
12     public function init() {
13         $this->view->jslocales = null;
14         $this->view->headScript()->exchangeArray(array());
15         $this->view->headLink()->exchangeArray(array());
16         $this->view->headTitle()->exchangeArray(array());
17         $this->view->headStyle()->exchangeArray(array());
18
19         $this->view->headLink()->appendStylesheet('css/generic.css');
20         $this->view->headLink()->appendStylesheet('css/error.css');
21     }
22
23     public function errorAction() {
24         $error = $this->_getParam('error_handler');
25
26         $error_code = 500; // default value: Internal Server Error
27         switch ($error->type) {
28             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
29             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
30             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
31                 $error_code = 404; // Not Found
32                 break;
33             default:
34                 if ($error->exception instanceof Syj_Exception_Request) {
35                     $error_code = 400; // Bad Request
36                 } else if ($error->exception instanceof Syj_Exception_Forbidden) {
37                     $error_code = 403; // Forbidden
38                 } else if ($error->exception instanceof Syj_Exception_NotFound) {
39                     $error_code = $error->exception->getCode();
40                 }
41                 break;
42         }
43         $this->httpError($error_code);
44
45         // Log exception, if logger available
46         if ($log = $this->getLog()) {
47             $log->crit($this->view->message, $error->exception);
48         }
49
50         if ($error_code != 404 and $error_code != 410 and $error->request->isXmlHttpRequest()) {
51             return $this->_helper->json(array('message' => $error->exception->getMessage()));
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 }