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

UrlGenerationException sends a log line to stderr that breaks artisan serve #53534

Open
jpbarbosa opened this issue Nov 16, 2024 · 1 comment · May be fixed by #53538
Open

UrlGenerationException sends a log line to stderr that breaks artisan serve #53534

jpbarbosa opened this issue Nov 16, 2024 · 1 comment · May be fixed by #53538

Comments

@jpbarbosa
Copy link

jpbarbosa commented Nov 16, 2024

Laravel Version

11.32.0

PHP Version

8.2.25

Database Driver & Version

No response

Description

When using a helper {{ route('anyroute') }} without the necessary route parameters (by accident), Laravel throws a UrlGenerationException, sending a log line to stderr that breaks the server.

The issue is the datetime string at the beginning, which getRequestPortFromLine is not prepared to parse. In Laravel 10, the same line does not display that datetime.

The problem only occurs when using LOG_CHANNEL=stderr.

[2024-11-16 06:58:05] local.ERROR: Undefined array key 1 {"exception":"[object] (ErrorException(code: 0): Undefined array key 1 at /Users/.../laravel-11/vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:383)

...

ErrorException 

Undefined array key 1

at vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:383
  379▕     protected function getRequestPortFromLine($line)
  380▕     {
  381▕         preg_match('/:(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches);
  382▕ 
➜ 383▕         return (int) $matches[1];
  384▕     }
  385▕ 
  386▕     /**
  387▕      * Get the console command options.

    +26 vendor frames 

27  artisan:13
    Illuminate\Foundation\Application::handleCommand(Object(Symfony\Component\Console\Input\ArgvInput))
Browser After reload Terminal

Steps To Reproduce

Install Laravel 11

composer create-project --prefer-dist laravel/laravel laravel-11
cd laravel-11

Change LOG_CHANNEL to stderr

# .env

LOG_CHANNEL=stderr

Create routes with any parameter

// routes/web.php

Route::view('/tenants/{tenant}', 'home')->name('home');
Route::View('/tenants/{tenant}/info', 'info')->name('info');

Create their views with routes missing required parameters

// resources/views/home.blade.php

<html>
    <body>
        <a href="{{ route('info') }}">Info</a>
    </body>
</html>
// resources/views/info.blade.php

<html>
    <body>
        <a href="{{ route('home') }}">Home</a>
    </body>
</html>

Run the server

php artisan serve

Access any route with parameter

open http://localhost:8000/tenants/tenant1

Server will throw an error, exit to the command line, and stop working

[2024-11-16 06:58:05] local.ERROR: Undefined array key 1 {"exception":"[object] (ErrorException(code: 0): Undefined array key 1 at /Users/.../laravel-11/vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:383)

...

ErrorException 

Undefined array key 1

at vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:383
  379▕     protected function getRequestPortFromLine($line)
  380▕     {
  381▕         preg_match('/:(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches);
  382▕ 
➜ 383▕         return (int) $matches[1];
  384▕     }
  385▕ 
  386▕     /**
  387▕      * Get the console command options.

    +26 vendor frames 

27  artisan:13
    Illuminate\Foundation\Application::handleCommand(Object(Symfony\Component\Console\Input\ArgvInput))
@ahmad-cit22
Copy link

ahmad-cit22 commented Nov 16, 2024

I tried to address this issue & proposed a solution in this PR: #53538

Hope that it can be helpful in solving the issue.

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