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

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