View Issue Details

IDProjectCategoryView StatusLast Update
394Exception_DefaultHandlerAllpublic27 Feb 2012 21:29
Reportermrosenquist Assigned Totimj  
PrioritynormalSeveritycrashReproducibilityhave not tried
Status closedResolutionfixed 
Product Version0.8.1 
Target Version0.8.2Fixed in Version0.8.2 
Summary394: Notice error when showing an error on a page when the content type has been set to other than text/html
DescriptionWhen ErrorHandler is used on a page which sets a content-type header other than "text/html" and an error occurs the output is written to STDERR

Steps To Reproduce<?php
// Page served via web

require_once 'Exception/DefaultHandler.php';

// Handle normal exceptions/errors
set_exception_handler(array('Exception_DefaultHandler', 'handleException'));
set_error_handler(array('Exception_DefaultHandler', 'handleError'));

header('Content-type: application/xml');

// Create a simple error
echo $foo;
Additional InformationGernerated Error:
===============


Notice: Use of undefined constant STDERR - assumed 'STDERR' in /Users/mattrq/Work/Dropbox/XDEV_Library/Exception_DefaultHandler/DefaultHandler.php on line 259



Warning: fwrite(): supplied argument is not a valid stream resource in /Users/mattrq/Work/Dropbox/XDEV_Library/Exception_DefaultHandler/DefaultHandler.php on line 259
TagsNo tags attached.
Attached Files
DefaultHandler.php.bug394.patch (531 bytes)   
Index: DefaultHandler.php
===================================================================
--- DefaultHandler.php	(revision 50386)
+++ DefaultHandler.php	(working copy)
@@ -249,7 +249,11 @@
 			print "<pre style='text-align:left;'>" . nl2br(htmlspecialchars($backtrace, ENT_QUOTES)) . '</pre>';
 		} else {
 			$output = "$errtype: $errmsg in $filename on line $linenum\n\n" . $backtrace;
-			fwrite(STDERR, $output);
+			if (PHP_SAPI == 'cli') {
+				fwrite(STDERR, $output);
+			} else {
+				print $output;
+			}
 		}
 	}
 	

Activities

mrosenquist

4 Nov 2011 11:14

reporter   ~399

A possible fix has been attached, it is not the most elegant fix but it will work

timj

4 Nov 2011 17:26

administrator   ~400

Thanks for the report! Actually the bug is with the use of STDERR - fwrite(STDERR, 'foo') is just bad code - it's not the context (CLI or otherwise) that it is called from. But anyway I think the logic would be better like this - what do you think?

if (output_is_html) {
  show_html_page_on_normal_output
} else if (running_from_cli) {
  write_to_stderr // the correct way ;-)
} else {
  write_plain_text_to_normal_output
}

mrosenquist

4 Nov 2011 19:06

reporter   ~401

The fix you suggest looks much better.

timj

5 Nov 2011 22:45

administrator   ~404

(Info) The error may not be seen when using a normal web browser, but this may help to reproduce:

curl -H 'Accept: image/foo' http://server/test.php

timj

5 Nov 2011 22:46

administrator   ~405

Fixed in SVN r2247

Issue History

Date Modified Username Field Change
4 Nov 2011 11:08 mrosenquist New Issue
4 Nov 2011 11:13 mrosenquist File Added: DefaultHandler.php.bug394.patch
4 Nov 2011 11:14 mrosenquist Note Added: 399
4 Nov 2011 17:26 timj Note Added: 400
4 Nov 2011 17:26 timj Assigned To => timj
4 Nov 2011 17:26 timj Status new => assigned
4 Nov 2011 17:26 timj Product Version => 0.8.1
4 Nov 2011 17:26 timj Target Version => 0.8.2
4 Nov 2011 19:06 mrosenquist Note Added: 401
5 Nov 2011 22:45 timj Note Added: 404
5 Nov 2011 22:46 timj Note Added: 405
5 Nov 2011 22:46 timj Status assigned => resolved
5 Nov 2011 22:46 timj Resolution open => fixed
5 Nov 2011 22:46 timj Fixed in Version => 0.8.2
27 Feb 2012 21:29 timj Status resolved => closed