From 1d37e81adeea9b2a1bc1028f2bf45b47ac045ec4 Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 3 Aug 2013 00:22:28 -0400 Subject: [PATCH] Better error handling/displaying with $config['debug'] and $config['verbose_errors'] --- inc/config.php | 2 +- inc/display.php | 22 +++++++++++----------- inc/functions.php | 16 ++++++++++++++-- inc/image.php | 10 +++++----- templates/error.html | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 templates/error.html diff --git a/inc/config.php b/inc/config.php index 337d92cb..8d1c05d7 100644 --- a/inc/config.php +++ b/inc/config.php @@ -42,7 +42,7 @@ // Shows some extra information at the bottom of pages. Good for development/debugging. $config['debug'] = false; - // For development purposes. All this does is turn 'display_errors' on. + // For development purposes. Displays (and "dies" on) all errors and warnings. Turn on with the above. $config['verbose_errors'] = true; // Directory where temporary files will be created. diff --git a/inc/display.php b/inc/display.php index 7c2665e1..4972d563 100644 --- a/inc/display.php +++ b/inc/display.php @@ -57,7 +57,7 @@ function createBoardlist($mod=false) { ); } -function error($message, $priority = true) { +function error($message, $priority = true, $debug_stuff = false) { global $board, $mod, $config; if ($config['syslog'] && $priority !== false) { @@ -71,16 +71,16 @@ function error($message, $priority = true) { } die(Element('page.html', array( - 'config'=>$config, - 'title'=>_('Error'), - 'subtitle'=>_('An error has occured.'), - 'body'=>'
' . - '

' . _($message) . '

' . - (isset($board) ? - "

"._("Go back").".

" : '') . - '
' + 'config' => $config, + 'title' => _('Error'), + 'subtitle' => _('An error has occured.'), + 'body' => Element('error.html', array( + 'config' => $config, + 'message' => $message, + 'mod' => $mod, + 'board' => isset($board) ? $board : false, + 'debug' => is_array($debug_stuff) ? str_replace("\n", ' ', utf8tohtml(print_r($debug_stuff, true))) : utf8tohtml($debug_stuff) + )) ))); } diff --git a/inc/functions.php b/inc/functions.php index f000dc3c..b6bdd809 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -155,10 +155,22 @@ function loadConfig() { } if ($config['verbose_errors']) { + set_error_handler(function($errno, $errstr, $errfile, $errline) { + if (error_reporting() == 0) + return false; // Looks like this warning was suppressed by the @ operator. + error(utf8tohtml($errstr), true, array( + 'file' => $errfile, + 'line' => $errline, + 'errno' => $errno, + 'error' => $errstr, + 'backtrace' => array_slice(debug_backtrace(), 1) + )); + }); error_reporting(E_ALL); - ini_set('display_errors', 1); + ini_set('display_errors', true); + ini_set('html_errors', false); } - + // Keep the original address to properly comply with other board configurations if (!isset($__ip)) $__ip = $_SERVER['REMOTE_ADDR']; diff --git a/inc/image.php b/inc/image.php index 141973ee..4fc9d018 100644 --- a/inc/image.php +++ b/inc/image.php @@ -287,16 +287,16 @@ class ImageConvert extends ImageBase { if (trim($error = shell_exec("gifsicle --unoptimize -O2 --resize {$this->width}x{$this->height} < " . escapeshellarg($this->src . '') . " \"#0-{$config['thumb_keep_animation_frames']}\" > " . escapeshellarg($this->temp) . '2>&1 &&echo $?') !== '0') || !file_exists($this->temp)) - error($error); + error('Failed to resize image!', null, $error); } else { - if (trim($error = shell_exec('convert ' . sprintf($config['convert_args'], '', $this->width, $this->height) . ' ' . + if (trim($error = shell_exec('aconvert ' . sprintf($config['convert_args'], '', $this->width, $this->height) . ' ' . escapeshellarg($this->src) . ' ' . escapeshellarg($this->temp) . ' 2>&1 &&echo $?')) !== '0' || !file_exists($this->temp)) - error($error); + error('Failed to resize image!', null, $error); } } else { - if (trim($error = shell_exec('convert ' . sprintf($config['convert_args'], '-flatten', $this->width, $this->height) . ' ' . + if (trim($error = shell_exec('aconvert ' . sprintf($config['convert_args'], '-flatten', $this->width, $this->height) . ' ' . escapeshellarg($this->src . '[0]') . " " . escapeshellarg($this->temp) . ' 2>&1 &&echo $?')) !== '0' || !file_exists($this->temp)) - error($error); + error('Failed to resize image!', null, $error); } } } diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 00000000..3b8eb693 --- /dev/null +++ b/templates/error.html @@ -0,0 +1,18 @@ +
+

{{ message }}

+ {% if board %} +

+ + {% trans 'Go back' %} + +

+ {% endif %} +
+{% if debug and config.debug %} +
+

{% trans 'Error information' %}

+
+		{{ debug }}
+	
+
+{% endif %} \ No newline at end of file