Reputation: 309
I use several jQuery AJAX calls in my application and sometimes encounter errors during those AJAX calls such as 500 internal server error. To get to the bottom of the error, I log into the server and check the error log which provides me with the PHP error.
I'm looking for a way to get notifications when these errors happen and what the PHP error was that caused the error. At least a way to get notified instead of having to receive an email from an user and then go in and check the logs.
I checked out this tutorial but am not sure how to integrate this into the AJAX error: http://net.tutsplus.com/tutorials/php/quick-tip-email-error-logs-to-yourself-with-php/ and also I'm not sure if this will return the PHP error or just the 500 internal server error.
Any ideas, existing services, or solutions are greatly appreciated! Here is one of my AJAX calls for example:
$.ajax ({
url: report,
cache: false,
success: function(html) {
// continue with event
},
error: function(x, status, error) {
// report error here
},
async:true
});
EDIT:
Here is the updated AJAX call that makes an AJAX requests to the php error notification...
$.ajax ({
url: report,
success: function(html) {
// success
},
error: function(x, status, error) {
$.ajax ({
url: 'admin/error_notification.php',
type: "POST",
data:{
error: error
}
});
},
async:true
});
This works great, however, only reports that an internal server error occurred instead of the actual PHP error that caused the AJAX error. Here is the PHP error handling file:
$error = $_POST['error'];
$sysadmin_message = $error;
$sysadmin_to = '[email protected]';
$sysadmin_subject = 'Error Notification';
$sysadmin_headers = "From: Site <[email protected]>\r\n";
$sysadmin_headers .= "MIME-Version: 1.0\r\n";
$sysadmin_headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($sysadmin_to, $sysadmin_subject, $sysadmin_message, $sysadmin_headers);
Obviously this will only send the AJAX error reported, but how do I add the actually PHP error to the notification?
Upvotes: 1
Views: 1502
Reputation: 2837
You would need to make a second ajax call inside of the error function to report to php
From the front end:
$.ajax ({
url: report,
cache: false,
success: function(html) {
// continue with event
},
error: function(x, status, error) {
// report error here
$.ajax ({
url: errorlogUrl,
data:{
message:'ajax error from [report]'
}
});
},
async:true
});
Or use a try/catch statement on the backend:
try{
// code that might throw an error
}
catch(Exception $e){
// send notification based on the content of $e
}
Upvotes: 3