Send email when error occurs

Send email when error occurs

rfitzwaterrfitzwater Posts: 57Questions: 5Answers: 1
edited March 2012 in DataTables 1.9
How would I go about setting up data tables to send me an email when an data tables error/warning occurs (basically whenever data tables alerts you something went wrong)? Ideally, i'd like the email to contain some variables in the message. For example, I'd like to see error/warning message, the URL, and UserAgent info.

Has anyone setup anything like this? I'm guessing the best method would be to use an ajax call to a php to actually send the mail. What I need to know I guess is how to catch and do the processing when one of these warning/error messages occurs.

Thanks,

-Rhon

Replies

  • rfitzwaterrfitzwater Posts: 57Questions: 5Answers: 1
    I was able to get this working by modifying the data tables javascript:
    [code]
    function _fnLog( oSettings, iLevel, sMesg )
    {
    var settings = {
    from: "blah@server.com",
    website: document.domain,
    }
    $.ajax({
    type:"GET",
    cache:false,
    url:"/js/errorhandler.php",
    data: $.param({'message':sMesg, userAgent: navigator.userAgent, 'from':settings.from, 'website': settings.website}),
    success: function(test){
    //alert("Report sent about the javascript error");
    }
    })
    var sAlert = (oSettings===null) ?
    [/code]

    My error handler.php script looks like this:
    [code]
    <?php

    $to = "towhom@web.com";
    $subject = 'A javascript error has been detected on '. $_GET['website'];
    $message = 'Error: '. $_GET['message']. '
    ';
    $message .= 'UserAgent: '. $_GET['userAgent']. '
    ';

    $headers = "From: ". $_GET['from'] ."\r\n"; // Or sendmail_username@hostname by default
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    mail($to, $subject, $message, $headers);
    die("message sent")

    ?>
    [/code]

    I am curious looking at the data tables code though. Is it possible to disable the warning alerts with an easy setting?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    You can set

    [code]
    $.fn.dataTableExt.sErrMode = 'throw';
    [/code]

    to have DataTables throw an error rather then alert it.

    Allan
  • mathiemathie Posts: 36Questions: 0Answers: 0
    Is there a way to overwrite _fnLog() externally instead of applying the customization directly to the source every time? I also want to POST to a URL when any error happens. Throwing is silent and good for developer but if it's a production bug I won't know about it. Just want to ensure the best experience for users. Thanks.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Currently no - there is no way to replace _fnLog other than by changing the source. I've just added it to my list of candidates for firing an event so you would be able to listen for that.

    Allan
This discussion has been closed.