Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Breaking change after upgrading from 2.0.3 to 2.0.4. Error: Call to undefined method class@anonymous::onRequestHandled() #530

Open
juhasev opened this issue May 11, 2021 · 8 comments

Comments

@juhasev
Copy link

juhasev commented May 11, 2021

I am getting this error after upgrading the package.

This is caused by HandleCors::class not resolving from the container. Seems like the package now has an issue if HandleCors::class is loaded in $middlewareGroups. Adding HandleCors::class to the $middleware array does not solve the issue either. We have 3 different middleware groups and we are only using HandleCors::class in one of them.

Error: Call to undefined method class@anonymous::onRequestHandled()

/var/www/html/vendor/fruitcake/laravel-cors/src/CorsServiceProvider.php:42
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:392
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:237
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:118

Here is the HTTP kernel

  /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        CheckForMaintenanceMode::class,
        TrustProxies::class,
        ValidatePostSize::class,
        TrimStrings::class,
        ConvertEmptyStringsToNull::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [

        'web' => [
            EncryptCookies::class,
            AddQueuedCookiesToResponse::class,
            HandleCors::class,
            StartSession::class,
            ShareErrorsFromSession::class,
            VerifyCsrfToken::class,
            SubstituteBindings::class,
            'trial' => CheckTrialExpiration::class,
        ],

        'landing' => [
            EncryptCookies::class,
            AddQueuedCookiesToResponse::class,
            StartSession::class,
            VerifyCsrfToken::class,
            SubstituteBindings::class,
        ],

        'api' => [
            SubstituteBindings::class,
            EncryptCookies::class,
            AddQueuedCookiesToResponse::class,
            CheckClientCredentials::class
        ],
    ];
@barryvdh
Copy link
Member

You haven't changed anything else? Not sure why the HandleCors class would be null if it's resolved from the container

@juhasev
Copy link
Author

juhasev commented May 12, 2021

No other changes. Reverted back to the previous version 2.0.3 and everything works properly with the above config.

@barryvdh
Copy link
Member

With Laravel 8? No Octane or anything?

I just did composer require laravel/laravel en moved the HandleCors from $middleware to the web part and don't get any errors.

Does it happen immediately? Or on CORS requests?

@barryvdh
Copy link
Member

It's strange cause this is calling it:

                $this->app->make(HandleCors::class)->onRequestHandled($event);

But why would that be null. It registered in register()

@juhasev
Copy link
Author

juhasev commented May 12, 2021

This is Laravel 8 standard without Octane. The error happened during testing.

@barryvdh
Copy link
Member

What do you mean during testing? A unit test? How does it look?

@juhasev
Copy link
Author

juhasev commented May 12, 2021

Yes during a unit (feature) controller test. Standard Laravel test setup. The test bootstrap loads the HTTP kernel normally, so I would assume this would also happen outside the tests.

@AIx86
Copy link

AIx86 commented Jun 3, 2021

I got the same error after upgrading to 2.0.4. from 2.0.3 on Laravel 8.44.0, no Octane.
The issue occurs only during phpunit feature tests. Like so:

    /**
     * @test
     * @return void
     */
    public function verifyResponse(): void
    {
        $this->withoutMiddleware([HandleCors::class])
        $response = $this->call('GET', '/api/v1/some-route');
        $response->assertStatus(Response::HTTP_OK);
    }

Here is the call stack:

3) Tests\Feature\someTest::verifyResponse
Error: Call to undefined method class@anonymous::onRequestHandled()

/var/www/html/vendor/fruitcake/laravel-cors/src/CorsServiceProvider.php:42
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:392
/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:237
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:118
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:509
/var/www/html/tests/Feature/someTest.php:88

After reverting to 2.0.3 the issue is resolved.

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

No branches or pull requests

3 participants