error.php 3.04 KB
Newer Older
1 2 3 4 5 6 7 8
<?php namespace Laravel;

class Error {

	/**
	 * Handle an exception and display the exception report.
	 *
	 * @param  Exception  $exception
Taylor Otwell committed
9
	 * @param  bool       $trace
10 11
	 * @return void
	 */
12
	public static function exception($exception, $trace = true)
13 14 15
	{
		static::log($exception);

16
		ob_get_level() and ob_end_clean();
17

Taylor Otwell committed
18 19 20 21 22
		$message = $exception->getMessage();

		// For Laravel view errors we want to show a prettier error:
		$file = $exception->getFile();

Taylor Otwell committed
23
		if (str_contains($exception->getFile(), 'eval()') and str_contains($exception->getFile(), 'laravel'.DS.'view.php'))
Taylor Otwell committed
24 25 26 27 28 29
		{
			$message = 'Error rendering view: ['.View::$last['name'].']'.PHP_EOL.PHP_EOL.$message;

			$file = View::$last['path'];
		}

30 31
		// 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
32
		// View in case the problem is in the View class.
33

34 35
		if (Config::get('error.detail'))
		{
36 37 38 39 40
			$response_body = "<html><h2>Unhandled Exception</h2>
				<h3>Message:</h3>
				<pre>".$message."</pre>
				<h3>Location:</h3>
				<pre>".$file." on line ".$exception->getLine()."</pre>";
Taylor Otwell committed
41

42 43
			if ($trace)
			{
44
				$response_body .= "
45 46
				  <h3>Stack Trace:</h3>
				  <pre>".$exception->getTraceAsString()."</pre></html>";
47
			}
48 49

			$response = Response::make($response_body, 500);
50
		}
51 52 53 54

		// 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.
55 56
		else
		{
57 58
			$response = Event::first('500');

59
			$response = Response::prepare($response);
60 61
		}

62 63 64 65
		$response->render();
		$response->send();
		$response->foundation->finish();

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		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;

Pascal Borreli committed
82
		// For a PHP error, we'll create an ErrorException and then feed that
83
		// exception to the exception method, which will create a simple view
84
		// of the exception details for the developer.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		$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()
	{
Pascal Borreli committed
102
		// If a fatal error occurred that we have not handled yet, we will
103
		// create an ErrorException and feed it to the exception handler,
104 105 106 107
		// as it will not yet have been handled.
		$error = error_get_last();

		if ( ! is_null($error))
108 109 110
		{
			extract($error, EXTR_SKIP);

111
			static::exception(new \ErrorException($message, $type, 0, $file, $line), false);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		}
	}

	/**
	 * 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);
		}
	}

129
}