Skip to content

Commit

Permalink
Added option to bubble exceptions on requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardovf committed Feb 16, 2013
1 parent 31d9d90 commit 407c48a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
34 changes: 31 additions & 3 deletions classes/Kohana/Request.php
Expand Up @@ -633,6 +633,11 @@ protected static function _parse_accept( & $header, array $accepts = NULL)
*/
protected $_client;

/**
* @var bool Should bubble Exceptions in internal requests
*/
protected $_bubble_exceptions = FALSE;

/**
* Creates a new request object for the given URI. New requests should be
* created using the [Request::instance] or [Request::factory] methods.
Expand Down Expand Up @@ -914,6 +919,23 @@ public function requested_with($requested_with = NULL)
return $this;
}

/**
* Getter/Setter indicating if an Internal Request should bubble its exceptions instead of just return the response (when HTTP_Exception) when they happen
*
* @param bool $bubble Should bubble exceptions or not when in a internal request
* @return Request|bool
*/
public function bubble_exceptions($bubble = NULL)
{
if ($bubble === NULL) {
return $this->_bubble_exceptions;
}

$this->_bubble_exceptions = $bubble;

return $this;
}

/**
* Processes the request, executing the controller action that handles this
* request, determined by the [Route].
Expand Down Expand Up @@ -974,10 +996,16 @@ public function execute()

if ( ! $this->_route instanceof Route)
{
return HTTP_Exception::factory(404, 'Unable to find a route to match the URI: :uri', array(
$exception = HTTP_Exception::factory(404, 'Unable to find a route to match the URI: :uri', array(
':uri' => $this->_uri,
))->request($this)
->get_response();
))->request($this);

// If the request should bubble the exceptions, we do it now!
if ($this->bubble_exceptions() === TRUE) {
throw $exception;
}

return $exception->get_response();
}

if ( ! $this->_client instanceof Request_Client)
Expand Down
10 changes: 10 additions & 0 deletions classes/Kohana/Request/Client/Internal.php
Expand Up @@ -104,11 +104,21 @@ public function execute_request(Request $request, Response $response)
}
catch (HTTP_Exception $e)
{
// If the request should bubble the exceptions, we do it now!
if ($request->bubble_exceptions() === TRUE) {
throw $e;
}

// Get the response via the Exception
$response = $e->get_response();
}
catch (Exception $e)
{
// If the request should bubble the exceptions, we do it now!
if ($request->bubble_exceptions() === TRUE) {
throw $e;
}

// Generate an appropriate Response object
$response = Kohana_Exception::_handler($e);
}
Expand Down

0 comments on commit 407c48a

Please sign in to comment.