0

I'm using my first global error handler for ajax calls where I get the error information, send it off to a PHP script and then log the results:

$(document).ajaxError(function(event, request, settings){

    //get the error and log the results
    var file = window.location.href;
    var verbose_error;
    verbose_error = 'Ajax error: ' + request.responseText + '\n\n';
    verbose_error += 'Status:' + request.status + '\n\n';
    verbose_error += 'Status Text:' + request.statusText + '\n\n';
    verbose_error += 'Url: ' + settings.url  + '\n\n';
    verbose_error += 'Data: ' + settings.data  + '\n\n';
    verbose_error += 'Data Type: ' + settings.dataType  + '\n\n';
$.post(getUrl() + 'error/log-ajax-error',{file : file,verbose_error : verbose_error},function(o){

});
});

When I do error handling for my PHP scripts, I can get a stack trace to see which function actually caused the error. Assuming that all of my ajax calls are performed within other functions, can I get the name of the function that caused the problematic ajax call?

For example, in the code below, if the $.post returns an error, I'd like to be able to access the name badAjaxCall, and then put it in my log to help with debugging.

function badAjaxCall() {
$.post(url,data,function(o){

});
}
Eric
  • 1,209
  • 1
  • 17
  • 34
  • http://stackoverflow.com/questions/2648293/javascript-get-function-name – A.B. Jul 09 '15 at 11:20
  • I don't quite understand, you want to know which PHP function handled the call? HOw do you decide which PHP function handles the call? Is this based on the post data or on the URL? Or do you mean something different? – Air2 Jul 09 '15 at 11:23
  • Sorry for not being clear; I actually want the javascript function which contained the ajax call which led to an ajax error: I'll then use PHP to log my error. – Eric Jul 09 '15 at 11:26
  • This is a hack, but may just work. Pass the function name as one of the data key/value pairs and access it from settings. Something like `{'funcName': 'badAjaxCall'}`. During the actual ajax call, you can simply ignore that key on server side. – lshettyl Jul 09 '15 at 11:44
  • That sounds like it would work, but I'd have to add it to all of my ajax calls. – Eric Jul 09 '15 at 11:50

1 Answers1

2

Create a new Error() and get the stack. Yo will have all details. As long as you don't throw the error, the end user will not notice it. You could also do throw error, if you want a feedback in the browser console.

function foo() {
  baz();
}
function baz() {
  foobar();
}
function foobar() {
  var error = new Error();
  alert(error.stack)
}
foo();

In case of async, it is a bit different.
If you create the error in the XHR handler you wont get any stack.
So you have to create the error before sending the XHR and then getting the stack (if needed) in the XHR handler.

function foo() {
  baz();
}
function baz() {
  foobar();
}
function foobar() {
  var error = new Error();
  //setTimeout represent your XHR call
  setTimeout(function() {
      alert(error.stack)
  }, 100)
}
foo();

In your case, that would be something like:

function ajaxCallWrapper(url, data, handler) {
    //never call direct $.post
    //instead always call this function to do an xhr call

    var error = new Error();
    $.post(url, data, function(o){
        if(o.error) {
            //in case of error, show the stack
            alert(error.stack);
            return;
        }
        //otherwise call the handler
        handler(o);
    });
}
function badAjaxCall() {
    ajaxCallWrapper(url,data,function(o){});
}
function normalAjaxCall() {
    ajaxCallWrapper(url,data,function(o){});
}
ben
  • 3,558
  • 1
  • 15
  • 28
  • With your solution, it looks like I'd have to implement it in every function that makes an ajax call. Is that correct? What I'm really hoping for is to have something in .ajaxError that will spit out the originating function which sent the ajax request which caused the error. – Eric Jul 09 '15 at 11:34
  • You can make a wrapper function for your ajax call and prepare the error object in that wrapper. Then you could also make a default error wrapper to handle it. Like here, `foobar` would be the wrapper for doing any ajax call – ben Jul 09 '15 at 11:37
  • Would you be able to clarify with some code? This would be my first time using the error object; I'm more of a PHP developer. – Eric Jul 09 '15 at 11:38