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.

  • 1 – ERROR
    This will log down all E_ALL level php generated errors, HTML status code errors (like 404s) and any user-defined errors.
  • 2 – DEBUG
    In addition to ERROR messages, this will show messages which helps you to debug your CI application.
  • 3 – INFO
    This level will show all ERROR & DEBUG messages as well as any user-defined informational messages. CI doesn’t natively generate any INFO messages but you can do so in your application.
  • 4 – All Messages
    This will show all log messages. I could not see any difference between this level and level 3 setting. If you do know a difference, post it into the comments.

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.)

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.