Permalink
Browse files

creating a custom error method that is used when there is a custom er…

…ror page available
  • Loading branch information...
dogmatic69 committed May 21, 2012
1 parent 151b40b commit 5c502f2f295224e0c2206776d45d59f2d5b0802c
@@ -16,7 +16,7 @@ public function getCustomLayout() {
class SecurityIpAddressBlockedException extends SecurityException {
protected $_messageTemplate = 'Your IP address "%s" has been temporarily blocked.';
- public function __construct($message, $code = null) {
+ public function __construct($message, $code = 503) {
parent::__construct($message, $code);
}
}
@@ -1,5 +1,21 @@
<?php
- class InfinitasException extends CakeException {
+ class InfinitasException extends RuntimeException {
+
+ /**
+ * Array of attributes that are passed in from the constructor, and
+ * made available in the view when a development error is displayed.
+ *
+ * @var array
+ */
+ protected $_attributes = array();
+
+ /**
+ * Template string that has attributes sprintf()'ed into it.
+ *
+ * @var string
+ */
+ protected $_messageTemplate = '';
+
/**
* @brief the name of the plugin that raised the exception
*
@@ -16,11 +32,16 @@ class InfinitasException extends CakeException {
* @param type $code
*/
public function __construct($message, $code = null) {
- parent::__construct($message, $code);
-
if($this instanceof InfinitasException) {
$this->_plugin = str_replace('Exception', '', get_parent_class($this));
}
+
+ if (is_array($message)) {
+ $this->_attributes = $message;
+ $message = __d($this->_plugin ? $this->_plugin : 'infinitas', $this->_messageTemplate, $message);
+ }
+
+ parent::__construct($message, $code);
}
/**
@@ -34,4 +55,13 @@ public function __construct($message, $code = null) {
public function plugin() {
return $this->_plugin;
}
+
+ /**
+ * Get the passed in attributes
+ *
+ * @return array
+ */
+ public function getAttributes() {
+ return $this->_attributes;
+ }
}
@@ -71,6 +71,7 @@ private function __changeTemplate($exceptionClass, $plugin) {
$path = InfinitasPlugin::path($plugin) . 'View' . DS . 'Errors' . DS . $view . '.ctp';
if(is_file($path)) {
+ $this->method = '_infinitasError';
$this->template = sprintf('%s.%s', $plugin, $view);
}
}
@@ -79,7 +80,24 @@ private function __changeTemplate($exceptionClass, $plugin) {
$path = APP . 'View' . DS . 'Errors' . DS . $view . '.ctp';
if(is_file($path)) {
+ $this->method = '_infinitasError';
$this->template = $view;
}
}
+
+ protected function _infinitasError($error) {
+ $message = $error->getMessage();
+ $code = $error->getCode();
+ $url = $this->controller->request->here();
+ $this->controller->response->statusCode($code);
+ $this->controller->set(array(
+ 'code' => $code,
+ 'url' => h($url),
+ 'name' => $error->getMessage(),
+ 'error' => $error,
+ '_serialize' => array('code', 'url', 'name')
+ ));
+ $this->controller->set($error->getAttributes());
+ $this->_outputMessage($this->template);
+ }
}

0 comments on commit 5c502f2

Please sign in to comment.