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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 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
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • ErrorHandler
  • ExceptionRenderer

Exceptions

  • AclException
  • BadRequestException
  • CacheException
  • CakeException
  • CakeLogException
  • CakeSessionException
  • ConfigureException
  • ConsoleException
  • ForbiddenException
  • HttpException
  • InternalErrorException
  • MethodNotAllowedException
  • MissingActionException
  • MissingBehaviorException
  • MissingComponentException
  • MissingConnectionException
  • MissingControllerException
  • MissingDatabaseException
  • MissingDatasourceConfigException
  • MissingDatasourceException
  • MissingHelperException
  • MissingLayoutException
  • MissingModelException
  • MissingPluginException
  • MissingShellException
  • MissingShellMethodException
  • MissingTableException
  • MissingTaskException
  • MissingTestLoaderException
  • MissingViewException
  • NotFoundException
  • 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: 
 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:             return $this->controller->appError($exception);
 96:         }
 97:         $method = $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));
 98:         $code = $exception->getCode();
 99: 
100:         $methodExists = method_exists($this, $method);
101: 
102:         if ($exception instanceof CakeException && !$methodExists) {
103:             $method = '_cakeError';
104:             if (empty($template)) {
105:                 $template = 'error500';
106:             }
107:             if ($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:         if (Configure::read('debug') == 0) {
122:             if ($method == '_cakeError') {
123:                 $method = 'error400';
124:             }
125:             if ($code == 500) {
126:                 $method = 'error500';
127:             }
128:         }
129:         $this->template = $template;
130:         $this->method = $method;
131:         $this->error = $exception;
132:     }
133: 
134: /**
135:  * Get the controller instance to handle the exception.
136:  * Override this method in subclasses to customize the controller used.
137:  * This method returns the built in `CakeErrorController` normally, or if an error is repeated
138:  * a bare controller will be used.
139:  *
140:  * @param Exception $exception The exception to get a controller for.
141:  * @return Controller
142:  */
143:     protected function _getController($exception) {
144:         App::uses('CakeErrorController', 'Controller');
145:         if (!$request = Router::getRequest(true)) {
146:             $request = new CakeRequest();
147:         }
148:         $response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
149:         try {
150:             $controller = new CakeErrorController($request, $response);
151:         } catch (Exception $e) {
152:             $controller = new Controller($request, $response);
153:             $controller->viewPath = 'Errors';
154:         }
155:         return $controller;
156:     }
157: 
158: /**
159:  * Renders the response for the exception.
160:  *
161:  * @return void
162:  */
163:     public function render() {
164:         if ($this->method) {
165:             call_user_func_array(array($this, $this->method), array($this->error));
166:         }
167:     }
168: 
169: /**
170:  * Generic handler for the internal framework errors CakePHP can generate.
171:  *
172:  * @param CakeException $error
173:  * @return void
174:  */
175:     protected function _cakeError(CakeException $error) {
176:         $url = $this->controller->request->here();
177:         $code = ($error->getCode() >= 400 && $error->getCode() < 506) ? $error->getCode() : 500;
178:         $this->controller->response->statusCode($code);
179:         $this->controller->set(array(
180:             'code' => $code,
181:             'url' => h($url),
182:             'name' => $error->getMessage(),
183:             'error' => $error,
184:             '_serialize' => array('code', 'url', 'name')
185:         ));
186:         $this->controller->set($error->getAttributes());
187:         $this->_outputMessage($this->template);
188:     }
189: 
190: /**
191:  * Convenience method to display a 400 series page.
192:  *
193:  * @param Exception $error
194:  * @return void
195:  */
196:     public function error400($error) {
197:         $message = $error->getMessage();
198:         if (Configure::read('debug') == 0 && $error instanceof CakeException) {
199:             $message = __d('cake', 'Not Found');
200:         }
201:         $url = $this->controller->request->here();
202:         $this->controller->response->statusCode($error->getCode());
203:         $this->controller->set(array(
204:             'name' => $message,
205:             'url' => h($url),
206:             'error' => $error,
207:             '_serialize' => array('name', 'url')
208:         ));
209:         $this->_outputMessage('error400');
210:     }
211: 
212: /**
213:  * Convenience method to display a 500 page.
214:  *
215:  * @param Exception $error
216:  * @return void
217:  */
218:     public function error500($error) {
219:         $message = $error->getMessage();
220:         if (Configure::read('debug') == 0) {
221:             $message = __d('cake', 'An Internal Error Has Occurred.');
222:         }
223:         $url = $this->controller->request->here();
224:         $code = ($error->getCode() > 500 && $error->getCode() < 506) ? $error->getCode() : 500;
225:         $this->controller->response->statusCode($code);
226:         $this->controller->set(array(
227:             'name' => $message,
228:             'message' => h($url),
229:             'error' => $error,
230:             '_serialize' => array('name', 'message')
231:         ));
232:         $this->_outputMessage('error500');
233:     }
234: 
235: /**
236:  * Convenience method to display a PDOException.
237:  *
238:  * @param PDOException $error
239:  * @return void
240:  */
241:     public function pdoError(PDOException $error) {
242:         $url = $this->controller->request->here();
243:         $code = 500;
244:         $this->controller->response->statusCode($code);
245:         $this->controller->set(array(
246:             'code' => $code,
247:             'url' => h($url),
248:             'name' => $error->getMessage(),
249:             'error' => $error,
250:             '_serialize' => array('code', 'url', 'name', 'error')
251:         ));
252:         $this->_outputMessage($this->template);
253:     }
254: 
255: /**
256:  * Generate the response using the controller object.
257:  *
258:  * @param string $template The template to render.
259:  * @return void
260:  */
261:     protected function _outputMessage($template) {
262:         try {
263:             $this->controller->render($template);
264:             $this->controller->afterFilter();
265:             $this->controller->response->send();
266:         } catch (MissingViewException $e) {
267:             try {
268:                 $this->_outputMessage('error500');
269:             } catch (Exception $e) {
270:                 $this->_outputMessageSafe('error500');
271:             }
272:         } catch (Exception $e) {
273:             $this->_outputMessageSafe('error500');
274:         }
275:     }
276: 
277: /**
278:  * A safer way to render error messages, replaces all helpers, with basics
279:  * and doesn't call component methods.
280:  *
281:  * @param string $template The template to render
282:  * @return void
283:  */
284:     protected function _outputMessageSafe($template) {
285:         $this->controller->layoutPath = '';
286:         $this->controller->subDir = '';
287:         $this->controller->viewPath = 'Errors/';
288:         $this->controller->viewClass = 'View';
289:         $this->controller->helpers = array('Form', 'Html', 'Session');
290: 
291:         $this->controller->render($template);
292:         $this->controller->response->type('html');
293:         $this->controller->response->send();
294:     }
295: 
296: }
297: 
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