Codeigniter: Handling errors

As coders, I’m sure we all know the value of good error handling. So I thought a quick post about how Codeigniter deals with those pesky errors would be a good post.

The way I see it, there are 2 types of errors we have to work with. The ones which are displayed out in nice friendly text to your users, and the type which are hidden and shown only to you as the coder to figure out where something has gone wrong. CI provides the means to do both, thankfully.

System/Application Errors
Let’s start with having a look at how CI deals with PHP errors. By default, on a clean install, CI will display ALL php errors of all severity. So if you make a mistake in your PHP coding, you’ll see plenty of these…

CI uses PHP’s error_reporting() function to define the level of error reporting, and you can find this in the main index.php file in the root of your CI install. It’s the first function you see.

Instead of E_ALL, you can change it to any of the predefined error constant that PHP understands to suit your needs. Obviously, once your site goes live, you should change from E_ALL to E_ERROR to show only messages for fatal run time problems. This will hide all php errors except those that will halt your script execution.

So, if the error messages are hidden, how else can we tell if something is not working right? Well, CI can writes out any errors it encounters into its log file. You can configure your CI log file by going to the config.php file in your applications/config directory.

By default, the logs are turned off, so you should turn it on by setting the $config['log_threshold'] variable. Once enabled, the logs will appear in the system/logs folder of your CI install. You can set the location of your log file if you wish to keep your logs elsewhere by defining a path for the $config['log_path'] variable.

You may notice that the $config['log_threshold'] allows a few settings.

In addition CI’s natively generated messages, you can define your own messages by calling the log_message() function. You can call this anywhere in your code, and you can set what level of messaging (error, debug, or info) you want logged. (See CI’s guide on how to use the log_message() function.)

Displaying Errors
Next, let’s look at what options are available if you want to display error messages to your user. CI provides 2 functions for this purpose – show_error() and show_404(). (See CI’s guide on how to use the show_error() and show_404() functions.)

1
2
3
if (SHOW_ERROR==true) {
	show_error('This is where your error message will appear surrounded by this default template.');
}

When you call show_error(), you will generate page that looks like this. The layout can changed by modifying this file application/errors/error_general.php)

show_404() works in a similar way, except it will display the application/errors/error_404.php layout and will throw a HTTP status code of 404.

Both of these functions are breakpoints and will stop the execution of your script, so any functions that follows these will not be processed.

That’s it. Feel free to comment.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on February 9, 2010 at 12:19 pm by Eldee · Permalink
In: PHP Tutorials · Tagged with: ,

9 Responses

Subscribe to comments via RSS

  1. Written by Ask About PHP: Codeigniter: Handling errors | Webs Developer on February 10, 2010 at 6:01 am
    Permalink

    [...] a new post from the Ask About PHP blog they look at handling different types of errors in a popular PHP framework, CodeIgniter, most being included with the framework. The way I see [...]

  2. Written by Chris Henry on February 10, 2010 at 2:53 pm
    Permalink

    CodeIgniter is really great for development and provides a lot of functionality. It’s error handler is great for being able to see what’s happening in an easy to read way. However, it doesn’t provide a good or intuitive way to turn errors off.

    CodeIgniter pays no attention to php.ini’s display_errors, which is terrible. In my experience the best way to turn off errors has been to empty the error.php file. Additionally, the function called as to handle errors is named exception_handler. These issues aren’t damning, but difficult to deal with when you really, really don’t want to show errors in production.

  3. Written by Eric Jones on February 10, 2010 at 9:51 pm
    Permalink

    Great Write Up. I have been working with CodeIgniter for about six months now and I really like the framework.

    However, I have noticed that even if you set error reporting to something like E_ERROR or 0, and set the log_threshold to 1. That DB errors are still displayed. To turn off, DB errors you need to change the setting for db_debug within the file ./system/application/config/database.php. This files states this about the setting.

    ['db_debug'] TRUE/FALSE – Whether database errors should be displayed.

    I set db_debug to false, and purposefully created some DB errors. The errors were not displayed on the screen. However, the errors were not logged either, even though I had a setting of 1 for error reporting. I even changed my error reporting threshold to 4, but still no DB Errors were getting written to my log file.

    Upon further investigation, I noticed that the query function within the file ./system/application/database/DB_driver.php will only log messages if the db_debug is set to true.

    So just be aware of that issue when launching your website into production with db_debug set to false.

  4. Written by webmaster on February 11, 2010 at 2:53 pm
    Permalink

    Hi Eric
    Thanks for the heads up. Didn’t know about the db_debug setting.

  5. Written by syabac on July 6, 2010 at 4:48 pm
    Permalink

    thanks for sharing.
    but how if i want to save the logs to database like sqlite?
    any idea?

  6. Written by Alexey on January 7, 2011 at 6:02 pm
    Permalink

    How to log sql errors?

    $db['default']['db_debug'] = FALSE;
    But I want to see error in log file.

  7. Written by krillo on March 19, 2011 at 3:51 pm
    Permalink

    Hi,
    I’m fairly new to CI and think it’s hard to find out “best practice” of it.
    Thanks to your post (and comments) I can better figure out how I want to use the built in error handling.

  8. Written by Codeigniter: Handling errors | CodeIgniter Tutorials on May 17, 2011 at 6:17 pm
    Permalink

    [...] Series andrewroland zend lucene librarycodeigniter 2.0 workshoplearncodeigniter basics Sponsors Top Rated TutorialsUsing Elliot Haughin's Twitter API with Codeigniter Part 2 (2 votes)CodeIgniter Active Record class and INSERT IGNORE (2 votes)Codeigniter Project Development from scratch to app Part 6 (2 votes)Tags2.0 active record ajax amplio.ch andrewroland.com api askaboutphp beginner class CMS codesamplez controller country db dragffy.com drop down egypt error flashdata form from scratch helper html index.php install jquery login lucene MVC net.tutsplus profiler reactor registration search session simplycodeigniter twitter upload URL validation variables view views workshop zend Sponsors LinksCodeigniter: Handling errors [...]

  9. Written by b k on February 5, 2012 at 11:14 am
    Permalink

    I note PHP’s default error reporting is a security risk, in that it exposes directory structure underlying the web site structure. Came to this page looking for ways to control error reporting.
    In the sample above, the part where it says “M:/xamplelite/codeigniter/…/errtest.php” On Unix systems, the folder structure includes a login id, which on web hosts is usually the control panel login id.

    (breath in) sorry for the long prefix, but is there any way to either tell php NOT to print out the underlying folder structure, or to insert my own print/format function that will keep private information private?

    so it says JUST”… in errtest.php on line 9″?
    or even better it gives the error based on docroot?

    as in
    “in /www/askabout.com/errtest.php on line 9″
    But any old way not to have PHP tell the world my login id and private folders would be great.

    And yes I know I can send it to a log file, but I still need to give that log file to my web developer, who I carefully gave restricted ftp access to a sub folder, only to have php blurt out not only the supposedly private enclosing directory structure but my login id?

    And yes, I got burned by one of the rare crooked web developers out there, who make it harder for the honest majority to find work.

    So, any way to keep private information private besides just not using PHP?

Subscribe to comments via RSS

Leave a Reply