I am developing a web application and am using PHP and javascript/jQuery the problem I have is with output of Warnings/Errors from XDebug. I am calling PHP scripts from AJAX to retrieve data and most of these are returning arrays which I am using JSON strings for the transfer. The problem I have is if there is a Warning/Error XDebug writes HTML tags in the output and when I try to decode the JSON string in my javascript it fails. Is there a way to configure XDebug to output these into a variable so I can still display them if I need without messing up my JSON decoding? I tried to show a quick example:
RemoveFile.php:
if (!unlink($_GET['Filename'])){ //if file not found XDebug will output warning here
$errors[] = "File not found";
echo json_encode($errors);
}
RemoveFile.js:
function RemoveFile(filePath){
$.get('RemoveFile.php', {
Filename: filePath
}).done(function (data){
var dataArray = JQuery.parseJSON(data); //Fails if XDebug outputs HTML
}
}
Update
I figured out what I needed to do, Since I didn't want to have to open the log file to check for errors and have the errors displayed at the time they occurred to make debugging easier I created a custom error handler to include in files I didn't want to output errors but still output them other files:
VarErrorHandler.php:
function VarErrorHandler($errno, $errstr, $errfile, $errline){
switch ($errno) {
case E_USER_ERROR:
$errors[] = "ERROR: Fatal error on line $errline in file $errfile [$errno] $errstr";
exit(1);
break;
case E_USER_WARNING:
$errors[] = "WARNING: [$errno] $errstr";
break;
case E_USER_NOTICE:
$errors[] = "NOTICE: [$errno] $errstr";
break;
default:
$errors[] = "Unknown error type: [$errno] $errstr";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
$old_error_handler = set_error_handler("VarErrorHandler");
So now in any files I don't want to output errors I add:
//include and set custom error handler
require_once("VarErrorHandler.php");
//to stuff here
//Reset original error handler
restore_error_handler();
Then display them on the client side by displaying the error array i.e.
function RemoveFile(filePath){
$.get('RemoveFile.php', {
Filename: filePath
}).done(function (data){
var dataArray = JQuery.parseJSON(data); //Fails if XDebug outputs HTML
//Alert Errors
alert(dataArray.errors.join("\n"));
}
}