summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Andrews <candrews@integralblue.com>2010-03-01 21:40:42 -0500
committerCraig Andrews <candrews@integralblue.com>2010-03-01 21:44:41 -0500
commitd8212977ce7f911d4f9bd6e55f94aea059a86782 (patch)
treef541e9e740ead662519405fd09869d23f8d7f4c9
parentf9dd83caa72a799916725888a631725d532d780e (diff)
Use PHP exceptions for PEAR error handling.
Allows for the common try/catch construct, which makes error handling cleaner and easier.
-rw-r--r--index.php97
-rw-r--r--lib/common.php12
2 files changed, 63 insertions, 46 deletions
diff --git a/index.php b/index.php
index 06ff9900f..88658a3ad 100644
--- a/index.php
+++ b/index.php
@@ -37,8 +37,6 @@ define('INSTALLDIR', dirname(__FILE__));
define('STATUSNET', true);
define('LACONICA', true); // compatibility
-require_once INSTALLDIR . '/lib/common.php';
-
$user = null;
$action = null;
@@ -68,52 +66,63 @@ function getPath($req)
*/
function handleError($error)
{
- if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
- return;
- }
+ try {
- $logmsg = "PEAR error: " . $error->getMessage();
- if (common_config('site', 'logdebug')) {
- $logmsg .= " : ". $error->getDebugInfo();
- }
- // DB queries often end up with a lot of newlines; merge to a single line
- // for easier grepability...
- $logmsg = str_replace("\n", " ", $logmsg);
- common_log(LOG_ERR, $logmsg);
-
- // @fixme backtrace output should be consistent with exception handling
- if (common_config('site', 'logdebug')) {
- $bt = $error->getBacktrace();
- foreach ($bt as $n => $line) {
- common_log(LOG_ERR, formatBacktraceLine($n, $line));
+ if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
+ return;
}
- }
- if ($error instanceof DB_DataObject_Error
- || $error instanceof DB_Error
- ) {
- $msg = sprintf(
- _(
- 'The database for %s isn\'t responding correctly, '.
- 'so the site won\'t work properly. '.
- 'The site admins probably know about the problem, '.
- 'but you can contact them at %s to make sure. '.
- 'Otherwise, wait a few minutes and try again.'
- ),
- common_config('site', 'name'),
- common_config('site', 'email')
- );
- } else {
- $msg = _(
- 'An important error occured, probably related to email setup. '.
- 'Check logfiles for more info..'
- );
- }
- $dac = new DBErrorAction($msg, 500);
- $dac->showPage();
+ $logmsg = "PEAR error: " . $error->getMessage();
+ if ($error instanceof PEAR_Exception && common_config('site', 'logdebug')) {
+ $logmsg .= " : ". $error->toText();
+ }
+ // DB queries often end up with a lot of newlines; merge to a single line
+ // for easier grepability...
+ $logmsg = str_replace("\n", " ", $logmsg);
+ common_log(LOG_ERR, $logmsg);
+
+ // @fixme backtrace output should be consistent with exception handling
+ if (common_config('site', 'logdebug')) {
+ $bt = $error->getTrace();
+ foreach ($bt as $n => $line) {
+ common_log(LOG_ERR, formatBacktraceLine($n, $line));
+ }
+ }
+ if ($error instanceof DB_DataObject_Error
+ || $error instanceof DB_Error
+ || ($error instanceof PEAR_Exception && $error->getCode() == -24)
+ ) {
+ $msg = sprintf(
+ _(
+ 'The database for %s isn\'t responding correctly, '.
+ 'so the site won\'t work properly. '.
+ 'The site admins probably know about the problem, '.
+ 'but you can contact them at %s to make sure. '.
+ 'Otherwise, wait a few minutes and try again.'
+ ),
+ common_config('site', 'name'),
+ common_config('site', 'email')
+ );
+ } else {
+ $msg = _(
+ 'An important error occured, probably related to email setup. '.
+ 'Check logfiles for more info..'
+ );
+ }
+
+ $dac = new DBErrorAction($msg, 500);
+ $dac->showPage();
+
+ } catch (Exception $e) {
+ echo _('An error occurred.');
+ }
exit(-1);
}
+set_exception_handler('handleError');
+
+require_once INSTALLDIR . '/lib/common.php';
+
/**
* Format a backtrace line for debug output roughly like debug_print_backtrace() does.
* Exceptions already have this built in, but PEAR error objects just give us the array.
@@ -238,10 +247,6 @@ function main()
return;
}
- // For database errors
-
- PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
-
// Make sure RW database is setup
setupRW();
diff --git a/lib/common.php b/lib/common.php
index 546f6bbe4..6c01c7db4 100644
--- a/lib/common.php
+++ b/lib/common.php
@@ -71,6 +71,7 @@ if (!function_exists('dl')) {
# global configuration object
require_once('PEAR.php');
+require_once('PEAR/Exception.php');
require_once('DB/DataObject.php');
require_once('DB/DataObject/Cast.php'); # for dates
@@ -128,6 +129,17 @@ require_once INSTALLDIR.'/lib/activity.php';
require_once INSTALLDIR.'/lib/clientexception.php';
require_once INSTALLDIR.'/lib/serverexception.php';
+
+//set PEAR error handling to use regular PHP exceptions
+function PEAR_ErrorToPEAR_Exception($err)
+{
+ if ($err->getCode()) {
+ throw new PEAR_Exception($err->getMessage(), $err->getCode());
+ }
+ throw new PEAR_Exception($err->getMessage());
+}
+PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'PEAR_ErrorToPEAR_Exception');
+
try {
StatusNet::init(@$server, @$path, @$conffile);
} catch (NoConfigException $e) {