Skip to content

Commit

Permalink
Added support for dontReport array to skip glove entirely (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
elle-the-dev authored Jun 30, 2023
1 parent e248220 commit 740ed58
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
15 changes: 15 additions & 0 deletions config/glove.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,28 @@
]
],

// Exceptions to pass directly through glove and do not catch
'skip' => [
],

// Laravel logs based on log levels.
// You only need to specify a log level if you want to override the default
// for a particular exception from the Exception level.
//
// If you do not want a particular exception logged, specify the level as 'ignore'
'logLevels' => [
Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => 'ignore',
Illuminate\Auth\AuthenticationException::class => 'ignore',
Illuminate\Auth\Access\AuthorizationException::class => 'ignore',
Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException::class => 'ignore',
Symfony\Component\HttpKernel\Exception\HttpException::class => 'ignore',
Illuminate\Http\Exceptions\HttpResponseException::class => 'ignore',
Illuminate\Database\Eloquent\ModelNotFoundException::class => 'ignore',
Illuminate\Database\MultipleRecordsFoundException::class => 'ignore',
Illuminate\Database\RecordsNotFoundException::class => 'ignore',
Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException::class => 'ignore',
Illuminate\Session\TokenMismatchException::class => 'ignore',
Illuminate\Validation\ValidationException::class => 'ignore',
Exception::class => 'error'
],

Expand Down
24 changes: 21 additions & 3 deletions src/GloveExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
use ElleTheDev\Glove\Renderers\ConsoleRenderer;
use ElleTheDev\Glove\Renderers\ExceptionRenderer;
use ElleTheDev\Glove\Renderers\SimpleExceptionRenderer;
use Illuminate\Config\Repository as Configuration;
use Illuminate\Foundation\Exceptions\Handler;
use Throwable;
use Illuminate\Contracts\Debug\ExceptionHandler;

/**
* Global Exception Handler
*
* Processes any otherwise uncaught exceptions and defers their processing to
* whichever Handler is most appropriate per the config in config/glove.php
*/
class GloveExceptionHandler implements ExceptionHandler
class GloveExceptionHandler extends Handler
{
/** @var ExceptionRenderer */
protected $exceptionRenderer;
Expand All @@ -29,22 +30,31 @@ class GloveExceptionHandler implements ExceptionHandler
/** @var Logger */
protected $logger;

/** @var Configuration */
protected $config;

protected $skip = [];

/**
* @param ExceptionRenderer $exceptionRenderer
* @param ConsoleRenderer $consoleRenderer
* @param SimpleExceptionRenderer $simpleRenderer
* @param Logger $logger
* @param Configuration $config
*/
public function __construct(
ExceptionRenderer $exceptionRenderer,
ConsoleRenderer $consoleRenderer,
SimpleExceptionRenderer $simpleRenderer,
Logger $logger
Logger $logger,
Configuration $config
) {
$this->exceptionRenderer = $exceptionRenderer;
$this->consoleRenderer = $consoleRenderer;
$this->simpleRenderer = $simpleRenderer;
$this->logger = $logger;
$this->config = $config;
$this->skip = $this->config->get('glove.skip');
}

/**
Expand All @@ -69,6 +79,9 @@ public function report(Throwable $e)
*/
public function render($request, Throwable $e)
{
if ($this->shouldntReport($e)) {
return parent::render($request, $e);
}
return $this->exceptionRenderer->render($request, $e) ?: $this->simpleRenderer->render($e);
}

Expand All @@ -94,6 +107,11 @@ public function renderForConsole($output, Throwable $e)
public function shouldReport(Throwable $e)
{
// ignoring is handled using `logLevels` in `config/glove.php`
foreach ($this->skip as $className) {
if ($e instanceof $className) {
return false;
}
}
return true;
}
}
21 changes: 16 additions & 5 deletions tests/GloveExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ElleTheDev\Glove\GloveExceptionHandler;
use Exception;
use Mockery;
use Illuminate\Config\Repository as Configuration;

class GloveExceptionHandlerTest extends \ElleTheDev\Tests\Glove\TestCase
{
Expand All @@ -24,8 +25,10 @@ public function testReport()
$simpleRenderer = Mockery::mock(SimpleExceptionRenderer::class);
$logger = Mockery::mock(Logger::class);
$logger->shouldReceive('log')->once()->with($e);
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('get')->with('glove.skip')->andReturn([]);

$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger);
$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger, $config);
$handler->report($e);
}

Expand All @@ -40,8 +43,10 @@ public function testRender()
$simpleRenderer = Mockery::mock(SimpleExceptionRenderer::class);
$logger = Mockery::mock(Logger::class);
$exceptionRenderer->shouldReceive('render')->once()->with($request, $e)->andReturn($response);
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('get')->with('glove.skip')->andReturn([]);

$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger);
$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger, $config);
$this->assertSame($response, $handler->render($request, $e));
}

Expand All @@ -57,8 +62,10 @@ public function testRenderSimple()
$logger = Mockery::mock(Logger::class);
$exceptionRenderer->shouldReceive('render')->once()->with($request, $e)->andReturn(null);
$simpleRenderer->shouldReceive('render')->once()->with($e)->andReturn($response);
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('get')->with('glove.skip')->andReturn([]);

$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger);
$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger, $config);
$this->assertSame($response, $handler->render($request, $e));
}

Expand All @@ -72,8 +79,10 @@ public function testRenderForConsole()
$simpleRenderer = Mockery::mock(SimpleExceptionRenderer::class);
$logger = Mockery::mock(Logger::class);
$consoleRenderer->shouldReceive('render')->once()->with($output, $e);
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('get')->with('glove.skip')->andReturn([]);

$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger);
$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger, $config);
$handler->renderForConsole($output, $e);
}

Expand All @@ -86,8 +95,10 @@ public function testShouldReport()
$consoleRenderer = Mockery::mock(ConsoleRenderer::class);
$simpleRenderer = Mockery::mock(SimpleExceptionRenderer::class);
$logger = Mockery::mock(Logger::class);
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('get')->with('glove.skip')->andReturn([]);

$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger);
$handler = new GloveExceptionHandler($exceptionRenderer, $consoleRenderer, $simpleRenderer, $logger, $config);
$this->assertTrue($handler->shouldReport($e));
}
}

0 comments on commit 740ed58

Please sign in to comment.