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->_helper->SyjReset->resetPlaceHolders();
14 $this->view->headLink()->appendStylesheet('css/generic.css');
15 $this->view->headLink()->appendStylesheet('css/error.css');
18 public function errorAction() {
19 $error = $this->_getParam('error_handler');
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
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();
38 $this->httpError($error_code);
40 // Log exception, if logger available
41 if ($log = $this->getLog()) {
42 $log->crit($this->view->message, $error->exception);
45 if ($error_code != 404 and $error_code != 410 and $error->request->isXmlHttpRequest()) {
46 return $this->_helper->json(array('message' => $error->exception->getMessage()));
49 // conditionally display exceptions
50 if ($this->getInvokeArg('displayExceptions') == true) {
51 $this->view->exception = $error->exception;
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"));
59 public function getLog() {
60 $bootstrap = $this->getInvokeArg('bootstrap');
61 if (!$bootstrap->hasPluginResource('Log')) {
64 $log = $bootstrap->getResource('Log');