CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • ErrorHandler
  • ExceptionRenderer

Exceptions

  • AclException
  • BadRequestException
  • CacheException
  • CakeException
  • CakeLogException
  • CakeSessionException
  • ConfigureException
  • ConsoleException
  • FatalErrorException
  • ForbiddenException
  • HttpException
  • InternalErrorException
  • MethodNotAllowedException
  • MissingActionException
  • MissingBehaviorException
  • MissingComponentException
  • MissingConnectionException
  • MissingControllerException
  • MissingDatabaseException
  • MissingDatasourceConfigException
  • MissingDatasourceException
  • MissingDispatcherFilterException
  • MissingHelperException
  • MissingLayoutException
  • MissingModelException
  • MissingPluginException
  • MissingShellException
  • MissingShellMethodException
  • MissingTableException
  • MissingTaskException
  • MissingTestLoaderException
  • MissingViewException
  • NotFoundException
  • NotImplementedException
  • PrivateActionException
  • RouterException
  • SocketException
  • UnauthorizedException
  • XmlException
  1: <?php
  2: /**
  3:  * Exception Renderer
  4:  *
  5:  * Provides Exception rendering features.  Which allow exceptions to be rendered
  6:  * as HTML pages.
  7:  *
  8:  * PHP 5
  9:  *
 10:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 11:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 12:  *
 13:  * Licensed under The MIT License
 14:  * Redistributions of files must retain the above copyright notice.
 15:  *
 16:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 17:  * @link          http://cakephp.org CakePHP(tm) Project
 18:  * @package       Cake.Error
 19:  * @since         CakePHP(tm) v 2.0
 20:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 21:  */
 22: 
 23: App::uses('Sanitize', 'Utility');
 24: App::uses('Router', 'Routing');
 25: App::uses('CakeResponse', 'Network');
 26: App::uses('Controller', 'Controller');
 27: 
 28: /**
 29:  * Exception Renderer.
 30:  *
 31:  * Captures and handles all unhandled exceptions. Displays helpful framework errors when debug > 1.
 32:  * When debug < 1 a CakeException will render 404 or  500 errors.  If an uncaught exception is thrown
 33:  * and it is a type that ExceptionHandler does not know about it will be treated as a 500 error.
 34:  *
 35:  * ### Implementing application specific exception rendering
 36:  *
 37:  * You can implement application specific exception handling in one of a few ways:
 38:  *
 39:  * - Create a AppController::appError();
 40:  * - Create a subclass of ExceptionRenderer and configure it to be the `Exception.renderer`
 41:  *
 42:  * #### Using AppController::appError();
 43:  *
 44:  * This controller method is called instead of the default exception handling.  It receives the
 45:  * thrown exception as its only argument.  You should implement your error handling in that method.
 46:  *
 47:  * #### Using a subclass of ExceptionRenderer
 48:  *
 49:  * Using a subclass of ExceptionRenderer gives you full control over how Exceptions are rendered, you
 50:  * can configure your class in your core.php, with `Configure::write('Exception.renderer', 'MyClass');`
 51:  * You should place any custom exception renderers in `app/Lib/Error`.
 52:  *
 53:  * @package       Cake.Error
 54:  */
 55: class ExceptionRenderer {
 56: 
 57: /**
 58:  * Controller instance.
 59:  *
 60:  * @var Controller
 61:  */
 62:     public $controller = null;
 63: 
 64: /**
 65:  * template to render for CakeException
 66:  *
 67:  * @var string
 68:  */
 69:     public $template = '';
 70: 
 71: /**
 72:  * The method corresponding to the Exception this object is for.
 73:  *
 74:  * @var string
 75:  */
 76:     public $method = '';
 77: 
 78: /**
 79:  * The exception being handled.
 80:  *
 81:  * @var Exception
 82:  */
 83:     public $error = null;
 84: 
 85: /**
 86:  * Creates the controller to perform rendering on the error response.
 87:  * If the error is a CakeException it will be converted to either a 400 or a 500
 88:  * code error depending on the code used to construct the error.
 89:  *
 90:  * @param Exception $exception Exception
 91:  */
 92:     public function __construct(Exception $exception) {
 93:         $this->controller = $this->_getController($exception);
 94: 
 95:         if (method_exists($this->controller, 'apperror')) {
 96:             return $this->controller->appError($exception);
 97:         }
 98:         $method = $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));
 99:         $code = $exception->getCode();
100: 
101:         $methodExists = method_exists($this, $method);
102: 
103:         if ($exception instanceof CakeException && !$methodExists) {
104:             $method = '_cakeError';
105:             if (empty($template)) {
106:                 $template = 'error500';
107:             }
108:             if ($template == 'internalError') {
109:                 $template = 'error500';
110:             }
111:         } elseif ($exception instanceof PDOException) {
112:             $method = 'pdoError';
113:             $template = 'pdo_error';
114:             $code = 500;
115:         } elseif (!$methodExists) {
116:             $method = 'error500';
117:             if ($code >= 400 && $code < 500) {
118:                 $method = 'error400';
119:             }
120:         }
121: 
122:         if (Configure::read('debug') == 0) {
123:             if ($method == '_cakeError') {
124:                 $method = 'error400';
125:             }
126:             if ($code == 500) {
127:                 $method = 'error500';
128:             }
129:         }
130:         $this->template = $template;
131:         $this->method = $method;
132:         $this->error = $exception;
133:     }
134: 
135: /**
136:  * Get the controller instance to handle the exception.
137:  * Override this method in subclasses to customize the controller used.
138:  * This method returns the built in `CakeErrorController` normally, or if an error is repeated
139:  * a bare controller will be used.
140:  *
141:  * @param Exception $exception The exception to get a controller for.
142:  * @return Controller
143:  */
144:     protected function _getController($exception) {
145:         App::uses('AppController', 'Controller');
146:         App::uses('CakeErrorController', 'Controller');
147:         if (!$request = Router::getRequest(true)) {
148:             $request = new CakeRequest();
149:         }
150:         $response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
151:         try {
152:             $controller = new CakeErrorController($request, $response);
153:             $controller->startupProcess();
154:         } catch (Exception $e) {
155:             if (!empty($controller) && $controller->Components->enabled('RequestHandler')) {
156:                 $controller->RequestHandler->startup($controller);
157:             }
158:         }
159:         if (empty($controller)) {
160:             $controller = new Controller($request, $response);
161:             $controller->viewPath = 'Errors';
162:         }
163:         return $controller;
164:     }
165: 
166: /**
167:  * Renders the response for the exception.
168:  *
169:  * @return void
170:  */
171:     public function render() {
172:         if ($this->method) {
173:             call_user_func_array(array($this, $this->method), array($this->error));
174:         }
175:     }
176: 
177: /**
178:  * Generic handler for the internal framework errors CakePHP can generate.
179:  *
180:  * @param CakeException $error
181:  * @return void
182:  */
183:     protected function _cakeError(CakeException $error) {
184:         $url = $this->controller->request->here();
185:         $code = ($error->getCode() >= 400 && $error->getCode() < 506) ? $error->getCode() : 500;
186:         $this->controller->response->statusCode($code);
187:         $this->controller->set(array(
188:             'code' => $code,
189:             'url' => h($url),
190:             'name' => $error->getMessage(),
191:             'error' => $error,
192:             '_serialize' => array('code', 'url', 'name')
193:         ));
194:         $this->controller->set($error->getAttributes());
195:         $this->_outputMessage($this->template);
196:     }
197: 
198: /**
199:  * Convenience method to display a 400 series page.
200:  *
201:  * @param Exception $error
202:  * @return void
203:  */
204:     public function error400($error) {
205:         $message = $error->getMessage();
206:         if (Configure::read('debug') == 0 && $error instanceof CakeException) {
207:             $message = __d('cake', 'Not Found');
208:         }
209:         $url = $this->controller->request->here();
210:         $this->controller->response->statusCode($error->getCode());
211:         $this->controller->set(array(
212:             'name' => $message,
213:             'url' => h($url),
214:             'error' => $error,
215:             '_serialize' => array('name', 'url')
216:         ));
217:         $this->_outputMessage('error400');
218:     }
219: 
220: /**
221:  * Convenience method to display a 500 page.
222:  *
223:  * @param Exception $error
224:  * @return void
225:  */
226:     public function error500($error) {
227:         $message = $error->getMessage();
228:         if (Configure::read('debug') == 0) {
229:             $message = __d('cake', 'An Internal Error Has Occurred.');
230:         }
231:         $url = $this->controller->request->here();
232:         $code = ($error->getCode() > 500 && $error->getCode() < 506) ? $error->getCode() : 500;
233:         $this->controller->response->statusCode($code);
234:         $this->controller->set(array(
235:             'name' => $message,
236:             'message' => h($url),
237:             'error' => $error,
238:             '_serialize' => array('name', 'message')
239:         ));
240:         $this->_outputMessage('error500');
241:     }
242: 
243: /**
244:  * Convenience method to display a PDOException.
245:  *
246:  * @param PDOException $error
247:  * @return void
248:  */
249:     public function pdoError(PDOException $error) {
250:         $url = $this->controller->request->here();
251:         $code = 500;
252:         $this->controller->response->statusCode($code);
253:         $this->controller->set(array(
254:             'code' => $code,
255:             'url' => h($url),
256:             'name' => $error->getMessage(),
257:             'error' => $error,
258:             '_serialize' => array('code', 'url', 'name', 'error')
259:         ));
260:         $this->_outputMessage($this->template);
261:     }
262: 
263: /**
264:  * Generate the response using the controller object.
265:  *
266:  * @param string $template The template to render.
267:  * @return void
268:  */
269:     protected function _outputMessage($template) {
270:         try {
271:             $this->controller->render($template);
272:             $this->controller->afterFilter();
273:             $this->controller->response->send();
274:         } catch (MissingViewException $e) {
275:             $attributes = $e->getAttributes();
276:             if (isset($attributes['file']) && strpos($attributes['file'], 'error500') !== false) {
277:                 $this->_outputMessageSafe('error500');
278:             } else {
279:                 $this->_outputMessage('error500');
280:             }
281:         } catch (Exception $e) {
282:             $this->_outputMessageSafe('error500');
283:         }
284:     }
285: 
286: /**
287:  * A safer way to render error messages, replaces all helpers, with basics
288:  * and doesn't call component methods.
289:  *
290:  * @param string $template The template to render
291:  * @return void
292:  */
293:     protected function _outputMessageSafe($template) {
294:         $this->controller->layoutPath = null;
295:         $this->controller->subDir = null;
296:         $this->controller->viewPath = 'Errors/';
297:         $this->controller->layout = 'error';
298:         $this->controller->helpers = array('Form', 'Html', 'Session');
299: 
300:         $view = new View($this->controller);
301:         $this->controller->response->body($view->render($template, 'error'));
302:         $this->controller->response->type('html');
303:         $this->controller->response->send();
304:     }
305: 
306: }
307: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs