bcit-ci / CodeIgniter

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
18.27k stars 7.61k forks source link

Error messages on CLI - show in plain text format #1743

Closed ivantcholakov closed 10 years ago

ivantcholakov commented 12 years ago

The views within the directory application/views/errors/ show html pages only. They can be improved for showing errors in palin text format when code is executed on command-line interface.

rolo2012 commented 11 years ago

you can put this code in application/core/MY_Exceptions.php

 class MY_Exceptions extends CI_Exceptions {    
    function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {
        $IN =& load_class('Input', 'core');
        if($IN->is_cli_request()){
            echo "  #######################################################".PHP_EOL;
            echo "      ERROR : $heading".PHP_EOL;
            echo "  #######################################################".PHP_EOL;
            echo "  $message";

        }else{
            parent::show_error($heading, $message, $template, $status_code);

        }
    }
}       
narfbg commented 11 years ago

A status code doesn't make sense for CLI, not those status codes at least - they are for HTTP only. Otherwise I agree that something needs to be done in that direction.

ivantcholakov commented 11 years ago

What about this kind of implementation:

application/
    views/
        errors/
            error_404.php
            error_404_cli.php
            error_db.php
            error_db_cli.php
            error_general.php
            error_general_cli.php
            error_php.php
            error_php_cli.php
            index.html

Error-related views may have plain-text versions that could be loaded conditionally, on CLI. Thus, a developer would be able to edit them too, in order to make some textual decorations, etc.

fuxu commented 11 years ago

oh yeah, @rolo2012 implemented this feature same as my first version. :+1: :)

Since it was labeled as NewFeature, I'll waiting for your good news :)

ivantcholakov commented 11 years ago

@dickfu You check for CLI this way:

if (defined('STDIN')) {
    // Do something
}

In CodeIgniter this check is a little bit different:

if ((php_sapi_name() == 'cli') or defined('STDIN'))
{
    // Do something
}
narfbg commented 11 years ago

... and there's CI_Input::is_cli_request() for that purpose, but that's not the point - hacking the views isn't the right approach.

fuxu commented 11 years ago

In my first version, I used CI_Input::is_cli_request(), and changed core/Exceptions.php. But I noticed that core/Codeignitor wat not defined when some error occurs. So I choosed another way to detect cli mode. sorry for my forget that why I choose hacking the views instead of modify core/Exceptions.php after my first version.....