I have a Laravel app that sends its exceptions to an Errbit server.
Here is the code for the exception handler:
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Validation\ValidationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Airbrake\Notifier;
class Handler extends ExceptionHandler
{
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
TokenMismatchException::class,
];
public function report(Exception $e)
{
if ($this->shouldReport($e)) {
$options = [
'environment' => app()->environment(),
'host' => config('airbrake.server'),
'projectId' => config('airbrake.api_key'),
'projectKey' => config('airbrake.api_key'),
'rootDirectory' => base_path(),
'secure' => TRUE,
'url' => request()->fullUrl(),
];
$notifier = new Notifier($options);
$notifier->notify($e);
}
return parent::report($e);
}
public function render($request, Exception $e)
{
// Replace `ModelNotFound` with 404.
if ($e instanceof ModelNotFoundException) {
$message = 'Page not found';
if (config('app.debug')) {
$message = $e->getMessage();
}
$e = new NotFoundHttpException($message, $e);
}
$response = parent::render($request, $e);
return $response;
}
}
While this works really well in general, I want to avoid logging errors that happen when I run artisan commands. The whole point of logging errors is to be notified of a problem, but if I'm sitting at the console, I already know about the problem.
I thought of looking in the stack trace to see if artisan is present there, but I see two problems with that:
How can I skip exception reporting for all exceptions run from the console, but keep it on for all the others?
答案 0 :(得分:0)
实际运行Illuminate\Foundation\Console\Kernel
命令的artisan
类具有调用reportException
ExceptionHandler
方法的函数report
。
我在我的Kernel
类中添加了该方法的覆盖,检查STDIN是否为交互式终端并禁用错误报告:
protected function reportException(Exception $e)
{
// Disable exception reporting if run from the console.
if (function_exists('posix_isatty') && @posix_isatty(STDIN)) {
echo "Not sending exception report";
return;
}
parent::reportException($e);
}