]> dev.renevier.net Git - syj.git/blob - application/controllers/ErrorController.php
7e0301539129575891f1c6ae7c3e1e26c3d82b7a
[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->_helper->SyjReset->resetPlaceHolders();
14         $this->view->headLink()->appendStylesheet('css/generic.css');
15         $this->view->headLink()->appendStylesheet('css/error.css');
16     }
17
18     public function errorAction() {
19         $error = $this->_getParam('error_handler');
20
21         $error_code = 500; // default value: Internal Server Error
22         switch ($error->type) {
23             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
24             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
25             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
26                 $error_code = 404; // Not Found
27                 break;
28             default:
29                 if ($error->exception instanceof Syj_Exception_Request) {
30                     $error_code = 400; // Bad Request
31                 } else if ($error->exception instanceof Syj_Exception_Forbidden) {
32                     $error_code = 403; // Forbidden
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 != 404 and $error_code != 410 and $error->request->isXmlHttpRequest()) {
46             return $this->_helper->json(array('message' => $error->exception->getMessage()));
47         }
48
49         // conditionally display exceptions
50         if ($this->getInvokeArg('displayExceptions') == true) {
51             $this->view->exception = $error->exception;
52         }
53
54         $this->view->request   = $error->request;
55         $this->view->isServerError = ($error_code >= 500 and $error_code < 600);
56         $this->view->headTitle($this->view->translate("Oups, something went wrong"));
57     }
58
59     public function getLog() {
60         $bootstrap = $this->getInvokeArg('bootstrap');
61         if (!$bootstrap->hasPluginResource('Log')) {
62             return false;
63         }
64         $log = $bootstrap->getResource('Log');
65         return $log;
66     }
67
68 }