Error reporting: Part II

Last month we looked at PHP's extensive error reporting framework. This month, we'll be looking at customising error reporting, and providing much more information for developers.

Python vs. PHP: Choosing your next project's language

Customised user error reporting

So far, we've focused on programmatic errors detected by the PHP interrupter. Once an application is finished and tested, however, these errors become secondary to producing standardised reports of user generated errors. PHP provides a framework to do this. In the following example, we define our own error handler and use it within our functions:

01 <?
02 error_reporting(0);
03 function errhdlr($errno, $errstr, $errfile, $errline, $vars)
04 {
05 	$ep = "";
06 	switch($errno) {
07 		case E_ERROR: $ep = "Error"; break;
08 		case E_WARNING: $ep = "Warning"; break;
09 		case E_NOTICE: $ep = "Notice"; break;
10 		case E_USER_ERROR: $ep = "Application error"; break;
11 		case E_USER_WARNING: $ep = "Application warning"; break;
12 		case E_USER_NOTICE: $ep = "Application notice"; break;
13 	}
14 	$ue = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
15 	$err = "<p><b>$ep: $errstr ($errfile: $errline)</b></p>";
16 	if(in_array($errno, $ue)) {
17 		$err .= "<p>Function variables: <br />".
18 			"<pre>".var_export($vars, true)."&lt/pre></p>";
19 	}
20 	$err .= "<p>Backtrace: <br /><pre>".var_export(debug_backtrace(), true)."</pre></p>";
21 	echo $err;
22 }
23 function negate($num)
24 {
25 	if(!is_numeric($num)) {
26 		trigger_error("'$num' is not a number", E_USER_ERROR);
27 		return 0;
28 	}
29 	if($num < 0) {
30 		trigger_error("'$num' is already negative", E_USER_WARNING);
31 		return($num);
32 	}
33 	return(- $num);
34 }
35 $olderrhdlr = set_error_handler("errhdlr");
36 $num = negate("foo");
37 $num = negate(-1);
38 $num = negate(1);
39 ?>

On line 02, we turn off PHP error reporting so that we can handle it ourselves. Following this, we define our error handling function, errhdlr(). This function takes five arguments: $errno, the type of error; $errstr, a string describing the error; $errfile, the file in which the error occurred; $errline, the line at which the error occurred; and $vars, an array representing the arguments to the function which generated the error.

Lines 05-13 determine what type of error is taking place and defines a string, $ep, which we'll use in our error report so that the user know s how severe the error is. PHP can generate other errors but, for the sake of simplicity, we'll only look at a few here. A complete list of error types can be found at http://www.php.net/manual/en/ref.errorfunc.php.

On line 14 we construct an array of user level errors to test our error type again. The reason we do this is that only in these cases does $var contain function arguments. On line 15 we finally construct our error report, while on lines 16-19 we append a dump of function arguments, if applicable. On line 20 we generate a backtrace. This describes the path of functions we took to get to the current line, and is very useful for debugging unexpected errors. On line 21 we display the report.

Next, we create a basic function negate() which attempts to negate the argument $num. There are two possible problems we may encounter: $num is not a number, in which case an error has occurred; or $num is already negative, in which case the function has been called incorrectly. In our case, these conditions generate an error and warning respectively.

To do this, we need to validate the argument first. On line 25 we test if $num is actually a number. If it isn't, we generate our error report. This is done by calling the function trigger_error(). The first argument is an error string: $errstr in our error handler errhdlr(). The second argument is the error type. Notice that it corresponds to one of the error types in errhdlr(). When viewed in a Web browser, the error will appear as follows:

Application error: 'foo' is not a number(/script.php: 27)
Function variables:
array (
'num' => 'foo'
)

Followed by a backtrace. On line 29 we test if $num is already negative. If it is, we generate a warning. Finally, on line 35 we change the default error handler to errhdlr() using set_error_handler(). Following this, we trigger user errors by calling negate() with invalid arguments. That's it for error handling. Next month, I'll be taking an early look at what's new in PHP 5.

Join the PC World newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Armand Abogado

HP OfficeJet 250 Mobile Printer

Wireless printing from my iPhone was also a handy feature, the whole experience was quick and seamless with no setup requirements - accessed through the default iOS printing menu options.

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?