CodeIgniter Forums
CLI::error throws exception when run from controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: CLI::error throws exception when run from controller (/showthread.php?tid=89170)



CLI::error throws exception when run from controller - jetspeed - 01-18-2024

So I have a spark command where I call CLI::error("something went wrong"). This works fine when run from the command line.
When I run this same spark command inside a controller, it will throw the following exception:

Quote:CRITICAL - 2024-01-18 15:16:50 --> Error: Undefined constant "CodeIgniter\CLI\STDERR"

in SYSTEMPATH\CLI\CLI.php on line 488.

If I instead, change it to CLI::write() then there are no problems.
Is there something I need to include?


RE: CLI::error throws exception when run from controller - kenjis - 01-19-2024

Try:
Code:
--- a/system/CLI/CLI.php
+++ b/system/CLI/CLI.php
@@ -180,6 +180,10 @@ class CLI
            // we need to define STDOUT ourselves
            // For "! defined('STDOUT')" see: https://github.com/codeigniter4/CodeIgniter4/issues/7047
            define('STDOUT', 'php://output'); // @codeCoverageIgnore
+
+            if (! defined('STDERR')) {
+                define('STDERR', 'php://output'); // @codeCoverageIgnore
+            }
        }
    }

But I don't recommend to call CLI command via web.


RE: CLI::error throws exception when run from controller - jetspeed - 01-20-2024

Thanks, I don't want to modify system files and keep track of them for upgrade reasons. I think I will just use CLI::print if is_cli() returns false.

Are there some known dangers to calling CLI commands via a controller?


RE: CLI::error throws exception when run from controller - kenjis - 01-20-2024

I just want you to check the patch works or not.
If it works, I will fix CI4.

The known dangers are:
1. If you fails to restrict access to the controller, an attacker may execute the command.
2. If the command takes long time, the PHP process may be killed by the server.


RE: CLI::error throws exception when run from controller - jetspeed - 01-20-2024

Thanks, I get the following error with your suggested patch (with xammp):


Code:
CRITICAL - 2024-01-20 16:56:18 --> TypeError: sapi_windows_vt100_support(): Argument #1 ($stream) must be of type resource, string given
in SYSTEMPATH\CLI\CLI.php on line 705.
1 SYSTEMPATH\CLI\CLI.php(705): sapi_windows_vt100_support('php://output')
2 SYSTEMPATH\CLI\CLI.php(731): CodeIgniter\CLI\CLI::streamSupports('sapi_windows_vt100_support', 'php://output')
3 SYSTEMPATH\CLI\CLI.php(492): CodeIgniter\CLI\CLI::hasColorSupport('php://output')



RE: CLI::error throws exception when run from controller - kenjis - 01-20-2024

Oh, it seems CLI commands do not work at all via web on Windows.