v5:userguide:error_handling
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
userguide:error_handling [2015/10/16 18:42] – mnewnham | v5:userguide:error_handling [2018/06/27 16:18] (current) – function names starting with lowercase (#430) dregad | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Error Handling | ====== Error Handling | ||
+ | |||
===== Default Behaviour ===== | ===== Default Behaviour ===== | ||
- | By default errors in the execution of SQL statements are handled by the database driver that is being accessed by PHP. No errors are normally handled and it is necessary to query the last error code to see if there has been an error. | + | By default, errors in the execution of SQL statements are handled by the database driver that is being accessed by PHP. No errors are normally handled and it is necessary to query the last error code to see if there has been an error. |
- | <code php> | + | |
- | /* | + | |
- | * Connection | + | |
- | */ | + | |
- | ===== adodb-errorhandler ===== | + | |
- | * | + | |
- | * | + | |
- | ==== Standard Version | + | ===== PHP set_error_handler |
+ | Once the PHP set_error_handler is used to override the default error handling, then the ' | ||
- | ==== Customization ==== | + | --------------------------------------------------- |
+ | <WRAP right box round> | ||
+ | == See Also == | ||
+ | [[v5: | ||
+ | </ | ||
+ | ===== adodb-errorhandler.inc.php ===== | ||
+ | This file is designed to be copied off and customized for your own use. In it's simplest form, it can be used to emulate the ' | ||
+ | <code php> | ||
+ | include ' | ||
+ | include ' | ||
- | ## <a name=" | + | $db = newAdoConnection(' |
+ | $db->connect('',' | ||
- | ADOdb supports PHP5 exceptions. Just include _adodb-exceptions.inc.php_ and you can now catch exceptions on errors as they occur. | + | /* |
+ | * Invalid SQL statement will be trapped inside the custom errorhandling file | ||
+ | */ | ||
+ | $result = $db-> | ||
+ | </ | ||
+ | ===== error_reporting ===== | ||
+ | The ADOdb error handler is not dependent on [[http:// | ||
+ | <code php> | ||
+ | function ADODB_Error_Handler($dbms, | ||
+ | { | ||
+ | |||
+ | /* | ||
+ | * Remark out this line to trap errors when error_reporting = 0 | ||
+ | */ | ||
+ | if (error_reporting() == 0) | ||
+ | return; // obey @ protocol | ||
+ | |||
+ | </ | ||
+ | ===== Override Error Trap/Resume ===== | ||
+ | In certain circumstances, | ||
- | <code php> | + | <code php> |
+ | /** | ||
+ | * Traps errors and ignores them | ||
+ | */ | ||
+ | function ignoreErrorHandler() | ||
+ | { | ||
+ | return true; | ||
+ | } | ||
- | | + | /* |
+ | * In Oracle we have no 'if index exists' | ||
+ | * so if the index did not exist in the first place, it would throw an error | ||
+ | * We want the error ignored, and for the program to continue as normal | ||
+ | */ | ||
- | try { | + | $saveErrorFn = $db-> |
+ | $db-> | ||
+ | set_error_handler(' | ||
+ | |||
+ | $db-> | ||
+ | |||
+ | /* | ||
+ | * Set the error handling back | ||
+ | */ | ||
+ | restore_error_handler(); | ||
+ | $db-> | ||
- | $db = NewADOConnection(" | + | </code> |
- | } catch (exception $e) { | + | ===== Using A Class Method ===== |
+ | <code php> | ||
+ | class c | ||
+ | { | ||
+ | function | ||
+ | { | ||
+ | print "ERROR TRAP"; | ||
+ | } | ||
+ | } | ||
- | var_dump($e); | + | $c = new c; |
- | adodb_backtrace($e->gettrace()); | + | $db->raiseErrorFn = array($c,' |
- | + | ||
- | } | + | |
</ | </ | ||
+ | ===== Combining With Custom Error Handling ===== | ||
+ | If you use '' | ||
+ | <code php> | ||
+ | error_reporting(E_ALL); | ||
- | ADOdb also provides two custom handlers which you can modify for your needs. The first one is in the **adodb-errorhandler.inc.php** file. This makes use of the standard PHP functions [error_reporting](http: | + | function myErrorHandler($p1, |
+ | { | ||
+ | /* | ||
+ | | ||
+ | | ||
+ | return true; | ||
+ | } | ||
- | Including the above file will cause _trigger_error($errorstring, | + | set_error_handler(' |
- | (a) Connect() or PConnect() fails, or | + | |
- | (b) a function that executes SQL statements such as Execute() or SelectLimit() has an error. | + | |
- | (c) GenID() appears to go into an infinite loop. | + | |
- | The $errorstring is generated by ADOdb and will contain useful debugging information similar to the error.log data generated below. This file adodb-errorhandler.inc.php | + | include './simple-errorhandler.inc.php'; |
+ | include '../ | ||
+ | </ | ||
- | If you define error_reporting(0), | ||
+ | ===== Exception Trapping ===== | ||
+ | ADOdb supports exception trapping using the '' | ||
+ | <code php> | ||
- | < | + | include ' |
+ | include ' | ||
- | **error_reporting(E_ALL); # pass any error messages triggered to error handler | + | try |
+ | { | ||
+ | $db = newADOConnection(" | ||
- | include(' | + | } |
- | </code> | + | catch (exception $e) |
+ | { | ||
+ | var_dump($e); | ||
+ | adodb_backtrace($e->gettrace()); | ||
+ | } | ||
- | < | ||
</ | </ | ||
- | < | ||
- | </ | ||
- | < | + | ===== Logging Error Messages ===== |
- | </ | + | If you want to log the error message, you can do so by defining the following optional constants: |
+ | * ADODB_ERROR_LOG_TYPE | ||
+ | * ADODB_ERROR_LOG_DEST | ||
- | < | + | ADODB_ERROR_LOG_TYPE is the error log message type (see [[http:// |
- | </ | + | |
- | + | ||
- | < | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | + | ||
- | If you want to log the error message, you can do so by defining the following optional constants ADODB_ERROR_LOG_TYPE and ADODB_ERROR_LOG_DEST. | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | < | + | |
- | + | ||
- | **error_reporting(E_ALL); | + | |
+ | <code php> | ||
+ | error_reporting(E_ALL); | ||
ini_set(" | ini_set(" | ||
define(' | define(' | ||
- | |||
define(' | define(' | ||
- | include(' | + | include(' |
- | </ | + | include(' |
+ | include(' | ||
- | < | ||
- | </ | ||
- | < | + | $c = newADOConnection('mysql'); |
- | </code> | + | $c->pConnect(' |
- | <pre>$c = NewADOConnection('mysql'); | + | $rs=$c-> |
- | </ | + | |
- | <pre>$c-> | + | if ($rs) |
+ | rs2html($rs); | ||
</ | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
The following message will be logged in the error.log file: | The following message will be logged in the error.log file: | ||
+ | 2001-10-28 14:20:38 mysql error: [1146: Table ' | ||
+ | in EXECUTE(" | ||
- | + | ===== PEAR_ERROR | |
- | + | ||
- | + | ||
- | <code php> | + | |
- | + | ||
- | EXECUTE(" | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | + | ||
- | ### PEAR_ERROR | + | |
The second error handler is **adodb-errorpear.inc.php**. This will create a PEAR_Error derived object whenever an error occurs. The last PEAR_Error object created can be retrieved using ADODB_Pear_Error(). | The second error handler is **adodb-errorpear.inc.php**. This will create a PEAR_Error derived object whenever an error occurs. The last PEAR_Error object created can be retrieved using ADODB_Pear_Error(). | ||
+ | <code php> | ||
+ | include(' | ||
+ | include(' | ||
+ | include(' | ||
+ | $c = newADOConnection(' | ||
+ | $c-> | ||
- | <pre><? | + | $rs=$c->execute(' |
- | **include(' | + | if ($rs) |
- | </ | + | |
- | <pre>include('adodb.inc.php'); | + | else |
+ | { | ||
+ | $e = ADODB_Pear_Error(); | ||
+ | echo '<p>', | ||
+ | } | ||
</ | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | |||
- | echo '< | ||
- | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | < | ||
- | </ | ||
- | |||
- | |||
You can use a PEAR_Error derived class by defining the constant ADODB_PEAR_ERROR_CLASS before the adodb-errorpear.inc.php file is included. For easy debugging, you can set the default error handler in the beginning of the PHP script to PEAR_ERROR_DIE, | You can use a PEAR_Error derived class by defining the constant ADODB_PEAR_ERROR_CLASS before the adodb-errorpear.inc.php file is included. For easy debugging, you can set the default error handler in the beginning of the PHP script to PEAR_ERROR_DIE, | ||
- | + | <code php> | |
- | + | include(' | |
- | + | ||
- | + | ||
- | <code php> | + | |
PEAR:: | PEAR:: | ||
Line 199: | Line 204: | ||
+ | Note that we do not explicitly return a PEAR_Error object to you when an error occurs. We return false instead. You have to call ADODB_Pear_Error() to get the last error or use the PEAR_ERROR_DIE technique. | ||
- | Note that we do not explicitly return a PEAR_Error object to you when an error occurs. We return false instead. You have to call ADODB_Pear_Error() to get the last error or use the PEAR_ERROR_DIE technique. | + | ===== MetaError and MetaErrMsg ===== |
- | ### MetaError and MetaErrMsg | + | If you need error messages that work across multiple databases, then use [[v5: |
- | If you need error messages that work across multiple databases, then use [MetaError()](# | + | ===== Error Messages ===== |
- | #### Error Messages | + | Error messages are outputted using the static method |
- | Error messages are outputted using the static method | + | |
+ | |||
+ | By default, it sends the messages to the standard output. You can override this to perform error logging. |
v5/userguide/error_handling.txt · Last modified: 2018/06/27 16:18 by dregad