Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelNotFoundException stays ignored after calling Exceptions::stopIgnoring(...) #54766

Closed
axlon opened this issue Feb 24, 2025 · 2 comments
Closed

Comments

@axlon
Copy link
Contributor

axlon commented Feb 24, 2025

Laravel Version

11.40

PHP Version

8.3.14

Database Driver & Version

n/a

Description

Problem

When Exceptions::stopIgnoring(ModelNotFoundException::class) is called Laravel will continue to ignore model not found exceptions. This appears to be because ModelNotFoundException extends RecordsNotFoundException which also on the ignore list. When checking whether to ignore an exception Laravel uses instanceof which will return true.

This will also happen for other exception types if their parent is on the ignore list.

How to fix this

I think there two (non-breaking) ways to address this:

  1. Allow developers to tell Laravel to never ignore an exception (e.g. Exceptions::forceReport($e) or Exceptions::withoutIgnoring()->report($e))
  2. Keep an internal "don't ignore list", if an exception is on that list we should always report it, regardless of whether its parents are on the ignore list. Exceptions are added to this list via Exceptions::stopIgnoring(...)

Steps To Reproduce

  • Open tinker
  • Run the following script:
Illuminate\Support\Facades\Exceptions::stopIgnoring(Illuminate\Database\Eloquent\ModelNotFoundException::class)->shouldReport(new Illuminate\Database\Eloquent\ModelNotFoundException)

This should return true but returns false

@axlon axlon changed the title ModelNotFoundExceptions stay ignored after calling Exceptions::stopIgnoring(...) ModelNotFoundException stays ignored after calling Exceptions::stopIgnoring(...) Feb 24, 2025
@macropay-solutions
Copy link

macropay-solutions commented Feb 24, 2025

If you setup the reports to ignore RecordsNotFoundException and then you say don't ignore a child of those, the behavior seems ok.
If you would like to change the check from instanceof to $e::class === $ex::class you would lose the ability to ignore all exceptions like RecordsNotFoundException.

A merge between the 2 options could be done but it needs refactoring the logic of dontReport by adding an extra stopIgnoring

    public function stopIgnoring(array|string $exceptions)
    {
        $exceptions = Arr::wrap($exceptions);
        $this->stopIgnoring =  \array_unique(\array_merge($this->stopIgnoring, exceptions)); // new line

        $this->dontReport = collect($this->dontReport)
                ->reject(fn ($ignored) => in_array($ignored, $exceptions))->values()->all();

        $this->internalDontReport = collect($this->internalDontReport)
                ->reject(fn ($ignored) => in_array($ignored, $exceptions))->values()->all();

        return $this;
    }

and then in \Illuminate\Foundation\Exceptions\Handler::shouldntReport

        $dontReport = array_merge($this->dontReport, $this->internalDontReport);

        if (! is_null(Arr::first($dontReport, fn ($type) => $e instanceof $type))) {
            return true;
        }

should be converted into

        $dontReport = array_merge($this->dontReport, $this->internalDontReport);

        if (! is_null(Arr::first($dontReport, fn (string $type): bool => !\in_array($e::class, $this->stopIgnoring, true) && $e instanceof $type))) {
            return true;
        }

@crynobone
Copy link
Member

This seems to be a breaking change request. Moving to Ideas

@laravel laravel locked and limited conversation to collaborators Feb 26, 2025
@crynobone crynobone converted this issue into discussion #54797 Feb 26, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants