"Expected" HTTP exception class. Used for all HTTP_Exception's where a standard Kohana error page should never be shown.
Eg HTTP_Exception_301, HTTP_Exception_302 etc
Class declared in SYSPATH/classes/HTTP/Exception/401.php on line 3.
string
$error_viewerror rendering view
string(12) "kohana/error"
string
$error_view_content_typeerror view content type
string(9) "text/html"
array
$php_errorsPHP error code => human readable name
array(9) ( 1 => string(11) "Fatal Error" 256 => string(10) "User Error" 4 => string(11) "Parse Error" 2 => string(7) "Warning" 512 => string(12) "User Warning" 2048 => string(6) "Strict" 8 => string(6) "Notice" 4096 => string(17) "Recoverable Error" 8192 => string(10) "Deprecated" )
integer
$_codeHTTP 401 Unauthorized
integer 401
Request
$_requestRequest instance that triggered this exception.
NULL
Response
$_responseResponse Object
NULL
$codeinteger 0
$fileNULL
$lineNULL
$messagestring(0) ""
Specifies the WWW-Authenticate challenge.
string
$challenge
= NULL - WWW-Authenticate challenge (eg `Basic realm="Control Panel"`)public function authenticate($challenge = NULL)
{
if ($challenge === NULL)
return $this->headers('www-authenticate');
$this->headers('www-authenticate', $challenge);
return $this;
}
Validate this exception contains everything needed to continue.
bool
public function check()
{
if ($this->headers('www-authenticate') === NULL)
throw new Kohana_Exception('A \'www-authenticate\' header must be specified for a HTTP 401 Unauthorized');
return TRUE;
}
Creates a new translated exception.
throw new Kohana_Exception('Something went terrible wrong, :user',
array(':user' => $user));
string
$message
= NULL - Status message, custom content to display with errorarray
$variables
= NULL - Translation variablesunknown
$previous
= NULLvoid
public function __construct($message = NULL, array $variables = NULL, Exception $previous = NULL)
{
parent::__construct($message, $variables, $previous);
// Prepare our response object and set the correct status code.
$this->_response = Response::factory()
->status($this->_code);
}
Generate a Response for the current Exception
Response
public function get_response()
{
$this->check();
return $this->_response;
}
Gets and sets headers to the Response.
mixed
$key
= NULL - $keystring
$value
= NULL - $valuemixed
public function headers($key = NULL, $value = NULL)
{
$result = $this->_response->headers($key, $value);
if ( ! $result instanceof Response)
return $result;
return $this;
}
Creates an HTTP_Exception of the specified type.
integer
$code
required - The http status codestring
$message
= NULL - Status message, custom content to display with errorarray
$variables
= NULL - Translation variablesunknown
$previous
= NULLHTTP_Exception
public static function factory($code, $message = NULL, array $variables = NULL, Exception $previous = NULL)
{
$class = 'HTTP_Exception_'.$code;
return new $class($message, $variables, $previous);
}
Store the Request that triggered this exception.
Request
$request
= NULL - Request object that triggered this exception.HTTP_Exception
public function request(Request $request = NULL)
{
if ($request === NULL)
return $this->_request;
$this->_request = $request;
return $this;
}
Magic object-to-string method.
echo $exception;
string
public function __toString()
{
return Kohana_Exception::text($this);
}
Exception handler, logs the exception and generates a Response object for display.
Exception
$e
required - $eResponse
public static function _handler(Exception $e)
{
try
{
// Log the exception
Kohana_Exception::log($e);
// Generate the response
$response = Kohana_Exception::response($e);
return $response;
}
catch (Exception $e)
{
/**
* Things are going *really* badly for us, We now have no choice
* but to bail. Hard.
*/
// Clean the output buffer if one exists
ob_get_level() AND ob_clean();
// Set the Status code to 500, and Content-Type to text/plain.
header('Content-Type: text/plain; charset='.Kohana::$charset, TRUE, 500);
echo Kohana_Exception::text($e);
exit(1);
}
}
Inline exception handler, displays the error message, source of the exception, and the stack trace of the error.
Exception
$e
required - $evoid
public static function handler(Exception $e)
{
$response = Kohana_Exception::_handler($e);
// Send the response to the browser
echo $response->send_headers()->body();
exit(1);
}
Logs an exception.
Exception
$e
required - $eint
$level
= integer 0 - $levelvoid
public static function log(Exception $e, $level = Log::EMERGENCY)
{
if (is_object(Kohana::$log))
{
// Create a text version of the exception
$error = Kohana_Exception::text($e);
// Add this exception to the log
Kohana::$log->add($level, $error, NULL, array('exception' => $e));
// Make sure the logs are written
Kohana::$log->write();
}
}
Get a Response object representing the exception
Exception
$e
required - $eResponse
public static function response(Exception $e)
{
try
{
// Get the exception information
$class = get_class($e);
$code = $e->getCode();
$message = $e->getMessage();
$file = $e->getFile();
$line = $e->getLine();
$trace = $e->getTrace();
/**
* HTTP_Exceptions are constructed in the HTTP_Exception::factory()
* method. We need to remove that entry from the trace and overwrite
* the variables from above.
*/
if ($e instanceof HTTP_Exception AND $trace[0]['function'] == 'factory')
{
extract(array_shift($trace));
}
if ($e instanceof ErrorException)
{
/**
* If XDebug is installed, and this is a fatal error,
* use XDebug to generate the stack trace
*/
if (function_exists('xdebug_get_function_stack') AND $code == E_ERROR)
{
$trace = array_slice(array_reverse(xdebug_get_function_stack()), 4);
foreach ($trace as & $frame)
{
/**
* XDebug pre 2.1.1 doesn't currently set the call type key
* http://bugs.xdebug.org/view.php?id=695
*/
if ( ! isset($frame['type']))
{
$frame['type'] = '??';
}
// Xdebug returns the words 'dynamic' and 'static' instead of using '->' and '::' symbols
if ('dynamic' === $frame['type'])
{
$frame['type'] = '->';
}
elseif ('static' === $frame['type'])
{
$frame['type'] = '::';
}
// XDebug also has a different name for the parameters array
if (isset($frame['params']) AND ! isset($frame['args']))
{
$frame['args'] = $frame['params'];
}
}
}
if (isset(Kohana_Exception::$php_errors[$code]))
{
// Use the human-readable error name
$code = Kohana_Exception::$php_errors[$code];
}
}
/**
* The stack trace becomes unmanageable inside PHPUnit.
*
* The error view ends up several GB in size, taking
* serveral minutes to render.
*/
if (
defined('PHPUnit_MAIN_METHOD')
OR
defined('PHPUNIT_COMPOSER_INSTALL')
OR
defined('__PHPUNIT_PHAR__')
)
{
$trace = array_slice($trace, 0, 2);
}
// Instantiate the error view.
$view = View::factory(Kohana_Exception::$error_view, get_defined_vars());
// Prepare the response object.
$response = Response::factory();
// Set the response status
$response->status(($e instanceof HTTP_Exception) ? $e->getCode() : 500);
// Set the response headers
$response->headers('Content-Type', Kohana_Exception::$error_view_content_type.'; charset='.Kohana::$charset);
// Set the response body
$response->body($view->render());
}
catch (Exception $e)
{
/**
* Things are going badly for us, Lets try to keep things under control by
* generating a simpler response object.
*/
$response = Response::factory();
$response->status(500);
$response->headers('Content-Type', 'text/plain');
$response->body(Kohana_Exception::text($e));
}
return $response;
}
Get a single line of text representing the exception:
Error [ Code ]: Message ~ File [ Line ]
Exception
$e
required - $estring
public static function text(Exception $e)
{
return sprintf('%s [ %s ]: %s ~ %s [ %d ]',
get_class($e), $e->getCode(), strip_tags($e->getMessage()), Debug::path($e->getFile()), $e->getLine());
}