laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.66k stars 11.04k forks source link

[11.x] Fix: Improve Request Port Extraction Handling in ServeCommand.php to Prevent Artisan Command Failures #53538

Closed ahmad-cit22 closed 5 days ago

ahmad-cit22 commented 1 week ago

[Updated] This PR fixes an issue (#53534) in the getRequestPortFromLine method in ServeCommand.php where failing to extract the request port from the log line resulted in an Undefined array key 1 error when using LOG_CHANNEL=stderr. This error would break the server when generating a URL with missing parameters, causing Artisan commands to fail.

Changes:

How This Fix Solves the Issue:

This fix resolves the issue where the server breaks because the getRequestPortFromLine method fails to parse log lines with a datetime prefix. By updating the regex, we ensure that both types of log lines (with and without datetime) are handled correctly. Additionally, the exception handling ensures that if parsing fails, it is immediately clear which log line caused the failure, facilitating quicker debugging for devs.


This way, this PR enhances the stability of Laravel's artisan serve command by addressing the log parsing issue in a a manner which can ensure smoother development workflows. Thank you.

taylorotwell commented 6 days ago

Can we extract this function into a static function that we then put a lot of tests around?

ahmad-cit22 commented 6 days ago

Can we extract this function into a static function that we then put a lot of tests around?

Thanks a lot.

Based on your suggestion, I moved the getRequestPortFromLine logic into a new static method in the LogParser utility class which ensures better testability and separation of concerns. There, I introduced a more specific InvalidArgumentException when the port cannot be parsed. And then wrote unit tests for the function considering various types of log lines as well as possible edge cases for invalid lines.

Hope this will solve the issue in a better approach.