User Authentication With patUser (part 3) - Making Exceptions (
Page 2 of 7 )
First
up, error handling. You'll have seen, from the previous sections of this
article, that many of patUser's functions return either true or false to
indicate success or failure; this value can then be used in your script to
display an appropriate status message.
However, patUser also goes
further, allowing you to display a more verbose message indicating the cause of
error if one occurred. A built-in error stack keeps track of all the errors that
have occurred, and the library include a number of functions designed to help
you read and modify this stack.
The two functions you're most likely to
use are getLastErrorCode() and getLastErrorMessage(), which return the last
error code and message respectively. Consider the following example, which
demonstrates:
<?php
// include classes
include("../include/patDbc.php"); include("../include/patUser.php");
// initialize database layer
$db = new patMySqlDbc("localhost", "db211", "us111", "secret");
// initialize patUser
$u = new patUser(true);
// connect patUser to database
$u->setAuthDbc($db);
// set tables
$u->setAuthTable("users");
// add user
$u->addUser( array("username" => "tom", "passwd" => "tom") );
// add user again
$u->addUser( array("username" => "tom", "passwd" => "tom") );
// get message and display
echo "Operation failed. " . $u->getLastErrorMessage() . " (#" .
$u->getLastErrorCode() . ")";
?>
Since there's a subtle error in the script above, patUser
will not be able to execute the call to addUser() successfully. An error code
will be generated internally and added to the error stack; this code can be
viewed and retrieved via the calls to getLastErrorCode() and
getLastErrorMessage(). Here's an example of what you might see:

Obviously, this is a little more informative than the
standard "User could not be added" message.
You can also use the shortcut
getLastError() method, which returns an array containing both error code and
message. The following example, which is equivalent to the one above,
illustrates:
<?php
// include classes
include("../include/patDbc.php"); include("../include/patUser.php");
// initialize database layer
$db = new patMySqlDbc("localhost", "db211", "us111", "secret");
// initialize patUser
$u = new patUser(true);
// connect patUser to database
$u->setAuthDbc($db);
// set tables
$u->setAuthTable("users");
// add user
$u->addUser( array("username" => "tom", "passwd" => "tom") );
// add user again
$u->addUser( array("username" => "tom", "passwd" => "tom") );
// get message and display
$e = $u->getLastError();
echo "Operation failed. " . $e['message'] . " (#" . $e['code'] . ")";
?>
You can obtain a complete list of all the errors in the stack
via the getAllErrorCodes() and getAllErrorMessages() methods, which return
arrays of all the error codes and messages generated by patUser during the
execution of the script, or again use the equivalent shortcut method
getAllErrors() to get both codes and messages in one pass. A complete list of
possible error codes and what they mean is available in the patUser
documentation.