2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class ErrorController extends Zend_Controller_Action
7 protected function httpError($code) {
8 $this->getResponse()->setHttpResponseCode($code);
9 $this->view->message = Zend_Http_Response::responseCodeAsText($code);
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());
19 $this->view->headLink()->appendStylesheet('css/generic.css');
20 $this->view->headLink()->appendStylesheet('css/error.css');
23 public function errorAction() {
24 $error = $this->_getParam('error_handler');
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
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();
43 $this->httpError($error_code);
45 // Log exception, if logger available
46 if ($log = $this->getLog()) {
47 $log->crit($this->view->message, $error->exception);
50 if ($error_code != 404 and $error_code != 410 and $error->request->isXmlHttpRequest()) {
51 return $this->_helper->json(array('message' => $error->exception->getMessage()));
54 // conditionally display exceptions
55 if ($this->getInvokeArg('displayExceptions') == true) {
56 $this->view->exception = $error->exception;
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"));
64 public function getLog() {
65 $bootstrap = $this->getInvokeArg('bootstrap');
66 if (!$bootstrap->hasPluginResource('Log')) {
69 $log = $bootstrap->getResource('Log');