Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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