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.3 API

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