From df8ad46daef960accd1e307f74e7bcb705e9fc7b Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sun, 21 Jul 2013 22:29:50 +0200 Subject: Change exit calls to throwing an exception This way the error won't be fatal in a unit test --- lib/app.php | 3 +-- lib/files.php | 4 ++-- lib/helper.php | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/app.php b/lib/app.php index baacf508d8e..115b7d60f5e 100644 --- a/lib/app.php +++ b/lib/app.php @@ -839,9 +839,8 @@ class OC_App{ OC_Hook::emit('update', 'success', 'Updated '.$info['name'].' app'); } catch (Exception $e) { - echo 'Failed to upgrade "'.$app.'". Exception="'.$e->getMessage().'"'; OC_Hook::emit('update', 'failure', 'Failed to update '.$info['name'].' app: '.$e->getMessage()); - die; + throw new RuntimeException('Failed to upgrade "'.$app.'". Exception="'.$e->getMessage().'"'); } OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app)); } diff --git a/lib/files.php b/lib/files.php index f5dffd970d2..ce46d13da41 100644 --- a/lib/files.php +++ b/lib/files.php @@ -62,7 +62,7 @@ class OC_Files { $zip = new ZipArchive(); $filename = OC_Helper::tmpFile('.zip'); if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==true) { - exit("cannot open <$filename>\n"); + throw new Exception("cannot open '$filename'\n"); } foreach ($files as $file) { $file = $dir . '/' . $file; @@ -93,7 +93,7 @@ class OC_Files { $zip = new ZipArchive(); $filename = OC_Helper::tmpFile('.zip'); if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==true) { - exit("cannot open <$filename>\n"); + throw new Exception("cannot open '$filename'\n"); } $file = $dir . '/' . $files; self::zipAddDir($file, $zip); diff --git a/lib/helper.php b/lib/helper.php index df0d120976d..ca508e1d933 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -176,8 +176,7 @@ class OC_Helper { }elseif( file_exists( OC::$SERVERROOT."/core/img/$image" )) { return OC::$WEBROOT."/core/img/$image"; }else{ - echo('image not found: image:'.$image.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT); - die(); + throw new RuntimeException('image not found: image:'.$image.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT); } } -- cgit v1.2.3 From 8c5df31c4951423857e394de02ae25ea5cb53698 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sun, 21 Jul 2013 22:30:32 +0200 Subject: DRY use OC_Template::printErrorPage --- lib/files.php | 28 +++++++++------------------- lib/setup.php | 4 +--- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/lib/files.php b/lib/files.php index ce46d13da41..0e5b5d54ff2 100644 --- a/lib/files.php +++ b/lib/files.php @@ -220,16 +220,11 @@ class OC_Files { if (!OC_Config::getValue('allowZipDownload', true)) { $l = OC_L10N::get('lib'); header("HTTP/1.0 409 Conflict"); - $tmpl = new OC_Template('', 'error', 'user'); - $errors = array( - array( - 'error' => $l->t('ZIP download is turned off.'), - 'hint' => $l->t('Files need to be downloaded one by one.') - . '
' . $l->t('Back to Files') . '', - ) + OC_Template::printErrorPage( + $l->t('ZIP download is turned off.'), + $l->t('Files need to be downloaded one by one.') + . '
' . $l->t('Back to Files') . '' ); - $tmpl->assign('errors', $errors); - $tmpl->printPage(); exit; } @@ -252,17 +247,12 @@ class OC_Files { if ($totalsize > $zipLimit) { $l = OC_L10N::get('lib'); header("HTTP/1.0 409 Conflict"); - $tmpl = new OC_Template('', 'error', 'user'); - $errors = array( - array( - 'error' => $l->t('Selected files too large to generate zip file.'), - 'hint' => 'Download the files in smaller chunks, seperately' - .' or kindly ask your administrator.
' - . $l->t('Back to Files') . '', - ) + OC_Template::printErrorPage( + $l->t('Selected files too large to generate zip file.'), + 'Download the files in smaller chunks, seperately' + .' or kindly ask your administrator.
' + . $l->t('Back to Files') . '' ); - $tmpl->assign('errors', $errors); - $tmpl->printPage(); exit; } } diff --git a/lib/setup.php b/lib/setup.php index 59f4cab75de..05a49890976 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -185,9 +185,7 @@ class OC_Setup { $hint = $l->t('Please double check the installation guides.', 'http://doc.owncloud.org/server/5.0/admin_manual/installation.html'); - $tmpl = new OC_Template('', 'error', 'guest'); - $tmpl->assign('errors', array(1 => array('error' => $error, 'hint' => $hint))); - $tmpl->printPage(); + OC_Template::printErrorPage($error, $hint); exit(); } } -- cgit v1.2.3 From aa22194a09538572e55b36f76f846c33b0016813 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sun, 21 Jul 2013 22:40:35 +0200 Subject: In debug mode uncaught exceptions should display a nice page --- lib/base.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/base.php b/lib/base.php index 1ff462819db..df57fe979f3 100644 --- a/lib/base.php +++ b/lib/base.php @@ -422,9 +422,13 @@ class OC { } } - if (!defined('PHPUNIT_RUN') and !(defined('DEBUG') and DEBUG)) { - OC\Log\ErrorHandler::register(); - OC\Log\ErrorHandler::setLogger(OC_Log::$object); + if (!defined('PHPUNIT_RUN')) { + if (defined('DEBUG') and DEBUG) { + set_exception_handler(array('OC_Template', 'printExceptionErrorPage')); + } else { + OC\Log\ErrorHandler::register(); + OC\Log\ErrorHandler::setLogger(OC_Log::$object); + } } // register the stream wrappers -- cgit v1.2.3 From 25e3c9cbeb7b6e643c748fcf393509c3a32b68d6 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sun, 21 Jul 2013 22:56:21 +0200 Subject: Only display call trace in debug mode --- lib/template.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/template.php b/lib/template.php index d48e3b36821..2b94fb6725c 100644 --- a/lib/template.php +++ b/lib/template.php @@ -531,17 +531,24 @@ class OC_Template{ if ($exception->getCode()) { $error_msg = '['.$exception->getCode().'] '.$error_msg; } - $hint = $exception->getTraceAsString(); - if (!empty($hint)) { - $hint = '
'.$hint.'
'; - } - while (method_exists($exception,'previous') && $exception = $exception->previous()) { - $error_msg .= '
Caused by: '; - if ($exception->getCode()) { - $error_msg .= '['.$exception->getCode().'] '; + if (defined('DEBUG') and DEBUG) { + $hint = $exception->getTraceAsString(); + if (!empty($hint)) { + $hint = '
'.$hint.'
'; + } + while (method_exists($exception,'previous') && $exception = $exception->previous()) { + $error_msg .= '
Caused by: '; + if ($exception->getCode()) { + $error_msg .= '['.$exception->getCode().'] '; + } + $error_msg .= $exception->getMessage(); + }; + } else { + $hint = ''; + if ($exception instanceof \OC\HintException) { + $hint = $exception->getHint(); } - $error_msg .= $exception->getMessage(); - }; + } self::printErrorPage($error_msg, $hint); } } -- cgit v1.2.3 From 179b42c56deeb37cf8c3f782bcd145daac64404b Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 22 Jul 2013 23:04:14 +0200 Subject: Fixed review issues --- lib/app.php | 3 ++- lib/files.php | 10 ++++++---- lib/template.php | 5 +++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/app.php b/lib/app.php index 115b7d60f5e..2437896157a 100644 --- a/lib/app.php +++ b/lib/app.php @@ -840,7 +840,8 @@ class OC_App{ } catch (Exception $e) { OC_Hook::emit('update', 'failure', 'Failed to update '.$info['name'].' app: '.$e->getMessage()); - throw new RuntimeException('Failed to upgrade "'.$app.'". Exception="'.$e->getMessage().'"'); + $l = OC_L10N::get('lib'); + throw new RuntimeException($l->t('Failed to upgrade "%s".', array($app)), 0, $e); } OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app)); } diff --git a/lib/files.php b/lib/files.php index 0e5b5d54ff2..c705d2adb1a 100644 --- a/lib/files.php +++ b/lib/files.php @@ -62,7 +62,8 @@ class OC_Files { $zip = new ZipArchive(); $filename = OC_Helper::tmpFile('.zip'); if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==true) { - throw new Exception("cannot open '$filename'\n"); + $l = OC_L10N::get('lib'); + throw new Exception($l->t('cannot open "%s"', array($filename))); } foreach ($files as $file) { $file = $dir . '/' . $file; @@ -93,7 +94,8 @@ class OC_Files { $zip = new ZipArchive(); $filename = OC_Helper::tmpFile('.zip'); if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==true) { - throw new Exception("cannot open '$filename'\n"); + $l = OC_L10N::get('lib'); + throw new Exception($l->t('cannot open "%s"', array($filename))); } $file = $dir . '/' . $files; self::zipAddDir($file, $zip); @@ -249,8 +251,8 @@ class OC_Files { header("HTTP/1.0 409 Conflict"); OC_Template::printErrorPage( $l->t('Selected files too large to generate zip file.'), - 'Download the files in smaller chunks, seperately' - .' or kindly ask your administrator.
' + $l->t('Download the files in smaller chunks, seperately or kindly ask your administrator.') + .'
' . $l->t('Back to Files') . '' ); exit; diff --git a/lib/template.php b/lib/template.php index 2b94fb6725c..339b30eb414 100644 --- a/lib/template.php +++ b/lib/template.php @@ -536,8 +536,9 @@ class OC_Template{ if (!empty($hint)) { $hint = '
'.$hint.'
'; } - while (method_exists($exception,'previous') && $exception = $exception->previous()) { - $error_msg .= '
Caused by: '; + $l = OC_L10N::get('lib'); + while (method_exists($exception, 'previous') && $exception = $exception->previous()) { + $error_msg .= '
'.$l->t('Caused by:').' '; if ($exception->getCode()) { $error_msg .= '['.$exception->getCode().'] '; } -- cgit v1.2.3