Skip to content
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

HoneybadgerLaravel::setRouteActionAndUserContext() Invalid Argument Error in Unit Tests #153

Open
fulopattila122 opened this issue Feb 3, 2025 · 0 comments

Comments

@fulopattila122
Copy link

fulopattila122 commented Feb 3, 2025

Error Description

I get the following error when running my unit tests:

TypeError: Honeybadger\HoneybadgerLaravel\HoneybadgerLaravel::setRouteActionAndUserContext(): Argument #1 ($request) must be of type Symfony\Component\HttpFoundation\Request, Illuminate\Support\Facades\Request given, called in /srv/app.vanilo.cloud/vendor/honeybadger-io/honeybadger-laravel/src/HoneybadgerLaravel.php on line 63

Image

I am using version 4.4.0 of the library.

Possible Solution

I think we have two issues here: https://github.com/honeybadger-io/honeybadger-laravel/blob/v4.4.0/src/HoneybadgerLaravel.php#L63

  1. Apparently, Laravel's request() call may return the facade, not the resolved request object. I guess the following code would be better:
    $this->setRouteActionAndUserContext($request ?: \Illuminate\Support\Facades\Request::instance());
  2. I think the setRouteActionAndUserContext() shouldn't be called at all if shouldReport() returns false. (I'm not sure about this condition though)

I believe this version of the notify method would fix the issue and would be more optimal:

    public function notify(Throwable $throwable, ?Request $request = null, array $additionalParams = []): array
    {
        // CHANGE 1: Return early if reporting is disabled
        if (! $this->shouldReport($throwable)) {
            return [];
        }

        // CHANGE 2: Use the Request facade's instance() method as a fallback request resolver
        $this->setRouteActionAndUserContext($request ?: \Illuminate\Support\Facades\Request::instance());
        $result = parent::notify($throwable, $request, $additionalParams);

        // Persist the most recent error for the rest of the request, so we can display on error page.
        if (app()->bound('session')) {
            // Lumen doesn't come with sessions.
            session()->now('honeybadger_last_error', $result['id'] ?? null);
        }

        return $result;
    }

Next Steps

I'd be happy to submit a PR, but I wanted to get your input first on whether it's a good direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant