donnikitos / vite-plugin-php

Vite's speed and tooling to preprocess PHP-files!
https://www.npmjs.com/package/vite-plugin-php
MIT License
40 stars 0 forks source link

Improve warning and errors print outs #33

Open donnikitos opened 2 months ago

donnikitos commented 2 months ago

There are sometimes missing the correct source file in errors, as everything is fed through the router, but this mostly an issue in bigger refactorings when it's not clear where one missed to fix a variable name, etc.

Warning: Undefined variable $nonexistant in /krei.se/node_modules/vite-plugin-php/dist/router.php(37) : eval()'d code on line 286

I think this will be known to other developers that this is not a fault of the plugin, but i modified router.php to catch warnings as errors and print out a more detailed stack trace with the correct sourceFile:

(function () {

        set_error_handler(function ($errno, $errstr, $errfile, $errline) {
                // Throw an ErrorException so it can be caught by the try-catch block
                throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
        });

    try {
        eval('?> ' . func_get_arg(0) . ' <?php');
        die();
    } catch (\Throwable $th) {
        die(
                        "Error: " . $th->getMessage() . "<br>" .
                        "In file: " . func_get_arg(1) . "<br>" .
                        "On line: " . $th->getLine() . "<br>" .
                        "Stack trace: <pre>" . $th->getTraceAsString() . "</pre>"
                );
    }
})($source, $sourceFile);

For me this is fine, but "legacy" php-projects will throw a lot of warnings that usually gets ignored by paid for time programmers lol.

Originally posted by @krei-se in https://github.com/donnikitos/vite-plugin-php/issues/29#issuecomment-2343461238