laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Exceptions Handler dontReportByStatusCode #2618

Closed mirzazeyrek closed 3 years ago

mirzazeyrek commented 3 years ago

Currently in the \App\Exceptions\Handler we are using this feature to ignore and not report exceptions:

    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

But I am thinking there could be cases that exceptions needs to be ignored not only by class but also with status code such as:

    protected array $dontReportByStatusCode =
        [
            [
                'exception' => LeagueOAuthServerException::class,
                'code' => 9
            ],
            [
                'exception' => LaravelOAuthServerException::class,
                'code' => 4
            ],
            [
                'exception' => LaravelOAuthServerException::class,
                'code' => 10
            ]
        ];

            public function shouldntReportByStatusCode(\Throwable $exception): bool
    {
        foreach ($this->dontReportByStatusCode as $ignoredExceptionWithCode) {
            if ($exception instanceof $ignoredExceptionWithCode['exception']) {
                if ($exception->getCode() === $ignoredExceptionWithCode['code']) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Determine if the exception should be reported.
     *
     * @param  \Throwable  $e
     * @return bool
     */
    public function shouldReport(Throwable $e)
    {
        return ! $this->shouldntReport($e) && ! $this->shouldntReportByStatusCode($e);
    }

But then this feature needs to be added to \Illuminate\Foundation\Exceptions\Handler Any ideas or suggestions regarding to how to handle this properly ?

themsaid commented 3 years ago

You may override the App\Exceptions\Handler to achieve such thing in your app.