【问题标题】:Prevent Laravel Artisan exceptions from being sent to error reporting防止 Laravel Artisan 异常被发送到错误报告
【发布时间】:2016-04-15 14:56:26
【问题描述】:

我有一个 Laravel 应用程序,它将其异常发送到 Errbit 服务器。

这是异常处理程序的代码:

<?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;
    }
}

虽然这通常非常有效,但我想避免在运行 artisan 命令时记录错误。记录错误的全部意义在于通知问题,但如果我坐在控制台前,我已经知道问题所在。

我想查看堆栈跟踪以查看 artisan 是否存在,但我发现有两个问题:

  • 这听起来不是一件非常有效的事情。
  • 队列侦听器也通过 artisan 运行,我确实需要从中获取异常,因为它们实际上并不是从控制台运行的。

如何跳过从控制台运行的所有异常的异常报告,而对所有其他异常保持打开状态?

【问题讨论】:

    标签: laravel exception-handling laravel-5.2


    【解决方案1】:

    实际运行artisan 命令的Illuminate\Foundation\Console\Kernel 类有一个函数reportException,它调用你的ExceptionHandlerreport 方法。

    我在 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多