vite-plugin-php icon indicating copy to clipboard operation
vite-plugin-php copied to clipboard

Improve warning and errors print outs

Open donnikitos opened this issue 1 year ago • 0 comments

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

donnikitos avatar Sep 11 '24 13:09 donnikitos