Files
spa-api/laravel/error.php

130 lines
3.1 KiB
PHP
Raw Normal View History

2012-01-16 13:59:24 -06:00
<?php namespace Laravel;
class Error {
/**
* Handle an exception and display the exception report.
*
* @param Exception $exception
2012-04-19 08:28:22 -05:00
* @param bool $trace
2012-01-16 13:59:24 -06:00
* @return void
*/
public static function exception($exception, $trace = true)
2012-01-16 13:59:24 -06:00
{
static::log($exception);
2012-06-04 09:04:20 -05:00
ob_get_level() and ob_end_clean();
2012-02-20 09:28:12 -06:00
2012-09-26 16:20:56 -04:00
$message = $exception->getMessage();
// For Laravel view errors we want to show a prettier error:
$file = $exception->getFile();
2013-01-05 13:50:35 -06:00
if (str_contains($exception->getFile(), 'eval()') and str_contains($exception->getFile(), 'laravel'.DS.'view.php'))
2012-09-26 16:20:56 -04:00
{
$message = 'Error rendering view: ['.View::$last['name'].']'.PHP_EOL.PHP_EOL.$message;
$file = View::$last['path'];
}
2012-01-16 13:59:24 -06:00
// If detailed errors are enabled, we'll just format the exception into
// a simple error message and display it on the screen. We don't use a
2012-02-06 19:59:07 -06:00
// View in case the problem is in the View class.
2012-01-16 13:59:24 -06:00
if (Config::get('error.detail'))
{
$response_body = "<html><h2>Unhandled Exception</h2>
<h3>Message:</h3>
<pre>".$message."</pre>
<h3>Location:</h3>
<pre>".$file." on line ".$exception->getLine()."</pre>";
2012-04-19 08:28:22 -05:00
if ($trace)
{
$response_body .= "
2012-01-16 13:59:24 -06:00
<h3>Stack Trace:</h3>
<pre>".$exception->getTraceAsString()."</pre></html>";
}
$response = Response::make($response_body, 500);
2012-01-16 13:59:24 -06:00
}
2012-02-12 14:48:36 -06:00
// If we're not using detailed error messages, we'll use the event
// system to get the response that should be sent to the browser.
// Using events gives the developer more freedom.
2012-01-16 13:59:24 -06:00
else
{
2013-04-06 20:27:00 -05:00
$response = Event::first('500', array($exception));
2012-02-12 14:48:36 -06:00
$response = Response::prepare($response);
2012-01-16 13:59:24 -06:00
}
$response->render();
$response->send();
$response->foundation->finish();
2012-01-16 13:59:24 -06:00
exit(1);
}
/**
* Handle a native PHP error as an ErrorException.
*
* @param int $code
* @param string $error
* @param string $file
* @param int $line
* @return void
*/
public static function native($code, $error, $file, $line)
{
if (error_reporting() === 0) return;
2012-07-24 09:31:27 +00:00
// For a PHP error, we'll create an ErrorException and then feed that
2012-01-16 13:59:24 -06:00
// exception to the exception method, which will create a simple view
2012-02-12 14:48:36 -06:00
// of the exception details for the developer.
2012-01-16 13:59:24 -06:00
$exception = new \ErrorException($error, $code, 0, $file, $line);
if (in_array($code, Config::get('error.ignore')))
{
return static::log($exception);
}
static::exception($exception);
}
/**
* Handle the PHP shutdown event.
*
* @return void
*/
public static function shutdown()
{
2012-07-24 09:31:27 +00:00
// If a fatal error occurred that we have not handled yet, we will
2012-01-16 13:59:24 -06:00
// create an ErrorException and feed it to the exception handler,
2012-02-12 14:48:36 -06:00
// as it will not yet have been handled.
$error = error_get_last();
if ( ! is_null($error))
2012-01-16 13:59:24 -06:00
{
extract($error, EXTR_SKIP);
static::exception(new \ErrorException($message, $type, 0, $file, $line), false);
2012-01-16 13:59:24 -06:00
}
}
/**
* Log an exception.
*
* @param Exception $exception
* @return void
*/
public static function log($exception)
{
if (Config::get('error.log'))
{
call_user_func(Config::get('error.logger'), $exception);
}
}
}