Skip to content

Commit

Permalink
Overhaul exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Feb 16, 2015
1 parent 952a7ee commit 237d97d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
43 changes: 40 additions & 3 deletions src/Exception/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php namespace October\Rain\Exception;

use App;
use Config;
use Request;
use Response;
use Exception;
use October\Rain\Exception\ApplcationException;

/**
* System Error Handler, this class handles application exception events.
Expand All @@ -29,7 +32,7 @@ class ErrorHandler
* @param Exception $proposedException The exception candidate that has been thrown.
* @return mixed Error page contents
*/
public function handleException(\Exception $proposedException, $httpCode = 500)
public function handleException(Exception $proposedException)
{
// Disable the error handler for test and CLI environment
if (App::runningUnitTests() || App::runningInConsole()) {
Expand All @@ -38,9 +41,11 @@ public function handleException(\Exception $proposedException, $httpCode = 500)

// Detect AJAX request and use error 500
if (Request::ajax()) {
return Response::make($proposedException->getMessage(), $httpCode);
return static::getDetailedMessage($proposedException);
}

$this->beforeHandleError($proposedException);

// Clear the output buffer
while (ob_get_level()) {
ob_end_clean();
Expand Down Expand Up @@ -74,7 +79,7 @@ public function handleException(\Exception $proposedException, $httpCode = 500)
* @param Exception $exception The mask exception.
* @return void
*/
public static function applyMask(\Exception $exception)
public static function applyMask(Exception $exception)
{
if (static::$activeMask !== null) {
array_push(static::$maskLayers, static::$activeMask);
Expand All @@ -97,10 +102,42 @@ public static function removeMask()
}
}

/**
* Returns a more descriptive error message if application
* debug mode is turned on.
* @param Exception $exception
* @return string
*/
public static function getDetailedMessage($exception)
{
/*
* Application Exceptions never display a detailed error
*/
if (!($exception instanceof ApplicationException) && Config::get('app.debug', false)) {
return sprintf('"%s" on line %s of %s',
$exception->getMessage(),
$exception->getLine(),
$exception->getFile()
);
}
else {
return $exception->getMessage();
}
}

//
// Overrides
//

/**
* We are about to display an error page to the user,
* provide an opportunity to handle extra functions.
* @return void
*/
public function beforeHandleError($exception)
{
}

/**
* Check if using a custom error page, if so return the contents.
* Return NULL if a custom error is not set up.
Expand Down
24 changes: 0 additions & 24 deletions src/Exception/ExceptionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use URL;
use File;
use Config;
use Exception;

/**
Expand Down Expand Up @@ -67,29 +66,6 @@ public function __construct($message = "", $code = 0, Exception $previous = null
parent::__construct($message, $code, $previous);
}

/**
* Returns a more descriptive error message if application
* debug mode is turned on.
* @param Exception $exception
* @return string
*/
public static function getDetailedMessage($exception)
{
/*
* Application Exceptions never display a detailed error
*/
if (!($exception instanceof ApplicationException) && Config::get('app.debug', false)) {
return sprintf('"%s" on line %s of %s',
$exception->getMessage(),
$exception->getLine(),
$exception->getFile()
);
}
else {
return $exception->getMessage();
}
}

/**
* Returns the class name of the called Exception.
* @return string
Expand Down
7 changes: 0 additions & 7 deletions src/Exception/SystemException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,5 @@ class SystemException extends ExceptionBase
public function __construct($message = "", $code = 0, \Exception $previous = null)
{
parent::__construct($message, $code, $previous);

/*
* Log the exception
*/
if (!App::runningUnitTests()) {
Log::error($this);
}
}
}
17 changes: 9 additions & 8 deletions src/Foundation/Exception/Handler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace October\Rain\Foundation\Exception;

use Log;
use Event;
use Response;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
Expand All @@ -15,7 +16,9 @@ class Handler extends ExceptionHandler
* @var array
*/
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
'October\Rain\Exception\ValidationException',
'October\Rain\Exception\ApplicationException',
'Symfony\Component\HttpKernel\Exception\HttpException',
];

/**
Expand All @@ -28,7 +31,10 @@ class Handler extends ExceptionHandler
*/
public function report(Exception $e)
{
return parent::report($e);
if ($this->shouldntReport($e))
return;

Log::error($e);
}

/**
Expand All @@ -45,12 +51,7 @@ public function render($request, Exception $e)
return Response::make($event, $statusCode);
}

if ($this->isHttpException($e)) {
return $this->renderHttpException($e);
}
else {
return parent::render($request, $e);
}
return parent::render($request, $e);
}

/**
Expand Down

0 comments on commit 237d97d

Please sign in to comment.