Skip to content

Instantly share code, notes, and snippets.

@fizerkhan
Last active February 8, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fizerkhan/ffbd684bb684067e1784f110b2b7870c to your computer and use it in GitHub Desktop.
Save fizerkhan/ffbd684bb684067e1784f110b2b7870c to your computer and use it in GitHub Desktop.
Atatus Exception Handler for Lumen Framework
// bootstrap/app.php
Replace the `$app->singleton()` call which registers the concrete exception handler in `bootstrap/app.php` with the following:
$app->instance(
Illuminate\Contracts\Debug\ExceptionHandler::class,
new App\Exceptions\ChainedExceptionHandler(
new Laravel\Lumen\Exceptions\Handler(),
[new App\Exceptions\AtatusExceptionHandler()]
)
);
<?php
// app/Exceptions/AtatusExceptionHandler.php
namespace App\Exceptions;
use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class AtatusExceptionHandler
* @package app\Exceptions
*/
class AtatusExceptionHandler implements ExceptionHandler
{
/**
* @var array list of class names of exceptions that should not be reported to Atatus. Defaults to the
* NotFoundHttpException class used for 404 requests.
*/
protected $ignoredExceptions = [
NotFoundHttpException::class,
];
/**
* AtatusExceptionHandler constructor.
*
* @param array|false $ignoredExceptions (optional) a list of exceptions to ignore, or false to use the default
* set
*/
public function __construct($ignoredExceptions = false)
{
if (is_array($ignoredExceptions)) {
$this->ignoredExceptions = $ignoredExceptions;
}
}
/**
* @inheritdoc
*/
public function report(Exception $e)
{
if (!in_array(get_class($e), $this->ignoredExceptions)) {
$this->logException($e);
}
}
/**
* @inheritdoc
*/
public function render($request, Exception $e)
{
}
/**
* @inheritdoc
*/
public function renderForConsole($output, Exception $e)
{
}
/**
* Logs the exception to Atatus (if the extension is loaded)
*
* @param Exception $e
*/
protected function logException(Exception $e)
{
if (extension_loaded('atatus')) {
atatus_notify_exception($e);
}
}
}
<?php
// app/Exceptions/ChainedExceptionHandler.php
namespace App\Exceptions;
use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
/**
* Class ChainedExceptionHandler
* @package App\Exceptions
*/
class ChainedExceptionHandler implements ExceptionHandler
{
/**
* @var ExceptionHandler
*/
private $primaryHandler;
/**
* @var ExceptionHandler[]
*/
private $secondaryHandlers;
/**
* ChainedExceptionHandler constructor.
*
* @param ExceptionHandler $primaryHandler
* @param ExceptionHandler[] $secondaryHandlers (optional)
*/
public function __construct(ExceptionHandler $primaryHandler, array $secondaryHandlers = [])
{
$this->primaryHandler = $primaryHandler;
$this->secondaryHandlers = $secondaryHandlers;
}
/**
* @inheritdoc
*/
public function report(Exception $e)
{
$this->primaryHandler->report($e);
foreach ($this->secondaryHandlers as $handler) {
$handler->report($e);
}
}
/**
* @inheritdoc
*/
public function render($request, Exception $e)
{
return $this->primaryHandler->render($request, $e);
}
/**
* @inheritdoc
*/
public function renderForConsole($output, Exception $e)
{
$this->primaryHandler->renderForConsole($output, $e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment