error.php 2.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php namespace Laravel;

class Error {

	/**
	 * Handle an exception and display the exception report.
	 *
	 * @param  Exception  $exception
	 * @return void
	 */
	public static function exception($exception)
	{
		static::log($exception);

15
		ob_get_level() and ob_end_clean();
16

17 18
		// 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
19
		// View in case the problem is in the View class.
20 21 22 23 24 25 26 27 28 29
		if (Config::get('error.detail'))
		{
			echo "<html><h2>Unhandled Exception</h2>
				  <h3>Message:</h3>
				  <pre>".$exception->getMessage()."</pre>
				  <h3>Location:</h3>
				  <pre>".$exception->getFile()." on line ".$exception->getLine()."</pre>
				  <h3>Stack Trace:</h3>
				  <pre>".$exception->getTraceAsString()."</pre></html>";
		}
30 31 32 33

		// 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.
34 35
		else
		{
36 37 38
			$response = Event::first('500');

			return Response::prepare($response)->send();
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
		}

		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;

		// For a PHP error, we'll create an ErrorExcepetion and then feed that
		// exception to the exception method, which will create a simple view
59
		// of the exception details for the developer.
60 61 62 63 64
		$exception = new \ErrorException($error, $code, 0, $file, $line);

		if (in_array($code, Config::get('error.ignore')))
		{
			return static::log($exception);
65 66

			return true;
67 68 69 70 71 72 73 74 75 76 77 78 79 80
		}

		static::exception($exception);
	}

	/**
	 * Handle the PHP shutdown event.
	 *
	 * @return void
	 */
	public static function shutdown()
	{
		// If a fatal error occured that we have not handled yet, we will
		// create an ErrorException and feed it to the exception handler,
81 82 83 84
		// as it will not yet have been handled.
		$error = error_get_last();

		if ( ! is_null($error))
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		{
			extract($error, EXTR_SKIP);

			static::exception(new \ErrorException($message, $type, 0, $file, $line));
		}
	}

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

}