Prevent Laravel Artisan exceptions from being sent to error reporting

Moshe Katz

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:

  • It doesn't sound like a very efficient thing to be doing.
  • Queue listeners are also run through artisan, and I do need to get the exceptions from those, because they are not actually being run from the console.

How can I skip exception reporting for all exceptions run from the console, but keep it on for all the others?

Moshe Katz

The Illuminate\Foundation\Console\Kernel class that actually runs artisan commands has a function reportException which calls your ExceptionHandler's report method.

I added an override of that method to my Kernel class that checks if STDIN is an interactive terminal and disables the error reporting:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

如何安装Laravel的Artisan?

来自分类Dev

Kendo UI Grid: Select single cell, get back DataItem, and prevent specific cells from being selected?

来自分类Dev

Displaying the Error Messages in Laravel after being Redirected from controller

来自分类Dev

在laravel中还原“ php artisan serve”命令

来自分类Dev

Laravel Artisan迁移[BadMethodCallException]

来自分类Dev

Error opening Xlsx files created from calling Reporting Services Web Service ReportExecution2005

来自分类Dev

How to prevent files from being checked out to the default changelist in perforce?

来自分类Dev

Error in fsproj file prevents it from being opened

来自分类Dev

How can I prevent a window from being resized with tkinter?

来自分类Dev

在Laravel 4中运行Artisan时出错

来自分类Dev

Laravel 4.2,Artisan :: call()忽略--env选项

来自分类Dev

How can one prevent a git branch from being merged into another?

来自分类Dev

ExtJS store filter is being sent not in full

来自分类Dev

Laravel php artisan用于模仿HTTPS

来自分类Dev

Laravel Artisan Scheduler中的链命令?

来自分类Dev

error_reporting bool操作

来自分类Dev

Laravel Artisan Command显示详细错误消息

来自分类Dev

从Laravel 4.2 Controller运行Artisan Command

来自分类Dev

laravel-PHP Artisan迁移失败

来自分类Dev

Laravel框架之外的Artisan :: call()

来自分类Dev

Laravel:PHP Artisan Tinker'SQLSTATE [23000]'错误

来自分类Dev

How can I prevent a PDF file from being downloaded or printed with PHP or JavaScript?

来自分类Dev

Laravel Artisan迁移[BadMethodCallException]

来自分类Dev

Laravel Artisan CLI URL参数

来自分类Dev

Laravel“ artisan make:observer”失败

来自分类Dev

Laravel框架之外的Artisan :: call()

来自分类Dev

Laravel Artisan命令中的错误

来自分类Dev

从错误的项目加载 Laravel Artisan

来自分类Dev

Laravel / Artisan CLI 重置 error_reporting 级别?

Related 相关文章

  1. 1

    如何安装Laravel的Artisan?

  2. 2

    Kendo UI Grid: Select single cell, get back DataItem, and prevent specific cells from being selected?

  3. 3

    Displaying the Error Messages in Laravel after being Redirected from controller

  4. 4

    在laravel中还原“ php artisan serve”命令

  5. 5

    Laravel Artisan迁移[BadMethodCallException]

  6. 6

    Error opening Xlsx files created from calling Reporting Services Web Service ReportExecution2005

  7. 7

    How to prevent files from being checked out to the default changelist in perforce?

  8. 8

    Error in fsproj file prevents it from being opened

  9. 9

    How can I prevent a window from being resized with tkinter?

  10. 10

    在Laravel 4中运行Artisan时出错

  11. 11

    Laravel 4.2,Artisan :: call()忽略--env选项

  12. 12

    How can one prevent a git branch from being merged into another?

  13. 13

    ExtJS store filter is being sent not in full

  14. 14

    Laravel php artisan用于模仿HTTPS

  15. 15

    Laravel Artisan Scheduler中的链命令?

  16. 16

    error_reporting bool操作

  17. 17

    Laravel Artisan Command显示详细错误消息

  18. 18

    从Laravel 4.2 Controller运行Artisan Command

  19. 19

    laravel-PHP Artisan迁移失败

  20. 20

    Laravel框架之外的Artisan :: call()

  21. 21

    Laravel:PHP Artisan Tinker'SQLSTATE [23000]'错误

  22. 22

    How can I prevent a PDF file from being downloaded or printed with PHP or JavaScript?

  23. 23

    Laravel Artisan迁移[BadMethodCallException]

  24. 24

    Laravel Artisan CLI URL参数

  25. 25

    Laravel“ artisan make:observer”失败

  26. 26

    Laravel框架之外的Artisan :: call()

  27. 27

    Laravel Artisan命令中的错误

  28. 28

    从错误的项目加载 Laravel Artisan

  29. 29

    Laravel / Artisan CLI 重置 error_reporting 级别?

热门标签

归档