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

ExceptionParser - $request->route() is null | line:249 #204

Closed
catabozan opened this issue Jul 13, 2022 · 3 comments
Closed

ExceptionParser - $request->route() is null | line:249 #204

catabozan opened this issue Jul 13, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@catabozan
Copy link

catabozan commented Jul 13, 2022

Laravel: 9
laravel-json-api: 2.4

I have implemented some API endpoints to my app that also has web routes.

The problem occurs when visiting non-existing web routes: instead of getting the 404 not found page, I get a blank page with a 500 response. The problem occurs in laravel-json-api/exceptions/src/ExceptionParser::acceptsMiddleware on line 249.

 public function acceptsMiddleware(...$middleware): self
    {
        $this->accept[] = static fn($ex, $request): bool => Collection::make($middleware)
            ->intersect($request->route()->gatherMiddleware()) // <- here
            ->isNotEmpty();

        return $this;
    }

Because $request->route() returns null.

I recently installed the stancl/tenancy package that adds multitenancy to my app, and divides the routes into central routes (available without being a tenant), and tenancy routes. I don't know if this is relevant but the stancl/tenancy package adds its own middleware before all other middleware. The weird part is that if for example I have a tenant specific route (let's say /home) and I try to access it without being in a tenant context, I get the 404 error (correct behavior). But if I try to access a route that is not defined as a tenant nor domain specific route (it does not exist anywhere), I get the error mentioned above.

Is this a package related problem?

Maybe change the acceptsMiddleware() method like this

 public function acceptsMiddleware(...$middleware): self
    {
        $this->accept[] = static fn($ex, $request): bool => Collection::make($middleware)
            ->intersect($request->route()?->gatherMiddleware() ?? Collection::make([])) // 249
            ->isNotEmpty();

        r

Or is there something wrong with my setup?

If this is the case, do you know what could be the cause of the issue, or where should I ask for help?

This is in my RouteServiceProvider:

public function boot(): void
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware(['api', 'force.json'])
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php')); // jsonapi routes

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web/tenant.php')); // tenant specific routes
        });

        $this->mapCentralRoutes(); // non-tenant specific routes (central)
        $this->mapUniversalRoutes(); // shared routes between tenancy and non-tenancy context
    }

protected function mapCentralRoutes(): void
    {
        foreach (config('tenancy.central_domains', []) as $domain) {
            Route::middleware('web')
                ->domain($domain)
                ->namespace($this->namespace)
                ->group(base_path('routes/web/central.php'));
        }
    }

protected function mapUniversalRoutes(): void
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web/universal.php'));
    }
@catabozan catabozan changed the title ExceptionParser - $request->route() is null ExceptionParser - $request->route() is null | line:249 Jul 13, 2022
@lindyhopchris
Copy link
Contributor

Looks like a bug - if the request's route() method can return null, we should handle that gracefully rather than causing another error. Thanks for reporting.

@lindyhopchris lindyhopchris added the bug Something isn't working label Jul 13, 2022
@lindyhopchris
Copy link
Contributor

Tagged the laravel-json-api/exceptions package with the fix as v1.1.1.

@lindyhopchris
Copy link
Contributor

To upgrade:

composer up laravel-json-api/*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants