Sunday, 29 May 2016

Loading

Codeigniter Error Handling

Code igniter Error Handling

   CodeIgniter lets you build error reporting into your applications using the functions described below. In addition, it has an error logging class that permits error and debugging messages to be saved as text files.  

You would like the messages to be displayed, when the application is in developing mode rather than in production mode as the error messages can be solved easily at the developing stage.

The environment of your application can be changed, by changing the line given below from index.php file. This can be set to anything but normally there are three values (development, test, production) used for this purpose.




define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
show_error(‘messsage’[,int $status_code=500]) :
This function will display the error message supplied to it using the following error template:
application/errors/error_general.php
The optional parameter $status_code determines what HTTP status code should be sent with the error.
show_404('page' [, 'log_error'])
This function will display the 404 error message supplied to it using the following error template:
application/errors/error_404.php
The function expects the string passed to it to be the file path to the page that isn't found. Note that CodeIgniter automatically shows 404 messages if controllers are not found.
CodeIgniter automatically logs any show_404() calls. Setting the optional second parameter to FALSE will skip logging.
log_message('level', 'message')
This function lets you write messages to your log files. You must supply one of three "levels" in the first parameter, indicating what type of message it is (debug, error, info), with the message itself in the second parameter. Example:
if ($some_var == "")
{
    log_message('error', 'Some variable did not contain a value.');
}
else
{    log_message('debug', 'Some variable was correctly set'); }



log_message('info', 'The purpose of some variable is to provide some value.');

There are three message types:
  1. Error Messages. These are actual errors, such as PHP errors or user errors.
  2. Debug Messages. These are messages that assist in debugging. For example, if a class has been initialized, you could log this as debugging info.
  3. Informational Messages. These are the lowest priority messages, simply giving information regarding some process. CodeIgniter doesn't natively generate any info messages but you may want to in your application.
 enabled in application/config/config.php file. Given below is the screenshot of config.php file, where you can set threshold value.

$config['log_threshold'] = 0;




Example 1 :
class PHPErrorhand {
 
public function setHandler() {
        register_shutdown_function('handleShutdown');
    }
 
}
function handleShutdown() {
    if (($error = error_get_last())) {
        ob_start();
            echo "<pre>";
        var_dump($error);
            echo "</pre>"
        $message = ob_get_clean();
        sendEmail($message);
        ob_start();
        echo '{"status":"error","message":"Internal application error!"}';
        ob_flush();
        exit();
    }
}
Example 2 :
application/controllers/mycontroller.php:
 
try {
    $this->mymodel->insert();
} catch (Exception $e) {
    $error = $e->getMessage();
}
 
$this->view('t/header');
$this->view('mycontroller/myform');
$this->view('f/footer');
 
application/views/t/header.php:
 
$this->view('t/messages');
 
application/views/t/messages.php:
 
<?php if (isset($info) || $this->session->flashdata('info')) { ?>
<div class="notification information canhide">
    <p><strong>INFORMATION: </strong>
    <?php echo isset($info) ? $info : null; ?>
    <?php echo $this->session->flashdata('info'); ?>
    </p>
</div>
<?php } ?>
 
<?php if (isset($notice) || $this->session->flashdata('notice')) { ?>
<div class="notification lightbulb canhide">
    <p><strong>NOTIFICATION: </strong>
    <?php echo isset($notice) ? $notice : null; ?>
    <?php echo $this->session->flashdata('notice'); ?>
    </p>
 
</div>
<?php } ?>
 
<?php if (isset($warning) || $this->session->flashdata('warning')) { ?>
<div class="notification warning canhide">
    <p><strong>WARNING: </strong>
    <?php echo isset($warning) ? $warning : null; ?>
    <?php echo $this->session->flashdata('warning'); ?>
    </p>
</div>
<?php } ?>
 
<?php if (isset($success) || $this->session->flashdata('success')) { ?>
<div class="notification success canhide">
    <p><strong>SUCCESS: </strong>
    <?php echo isset($success) ? $success : null; ?>
    <?php echo $this->session->flashdata('success'); ?>
    </p>
</div>
<?php } ?>
 
<?php if (isset($error) || $this->session->flashdata('error')) { ?>
<div class="notification failure canhide">
    <p><strong>FAILURE: </strong>
    <?php echo isset($error) ? $error : null; ?>
    <?php echo $this->session->flashdata('error'); ?>
    </p>
</div>
<?php } ?>




No comments:

Post a Comment