SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error Handling – Display Function Name

    I am trying to code a reusable error handling function for my JavaScript objects. I thought is would be possible to display the name of the function. So this way, when an error occurs, I can display the error along with the name of the function the error occurred in. Is it possible to display the name of the function from within the function?

    These examples do not work but I did try them.

    Code:
    <html>
    <head>
    <script language="JavaScript">
    function display()
    {
       alert( this.toString() );
       alert( this.name );
       alert( this );
    }
    </script>
    </head>
    
    <body>
       <a href="javascript: display();">Display</a>
    </body>
    
    </html>

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can get a reference to the current function, using arguments.callee. I believe it's deprecated, but it should still be widely supported by browsers. You can use use arguments.callee.toString(), to get the function declaration. However, there is not a 1:1 relationship between a function and it's name in javascript, so you can't defer the functions name. The exception to this is, if the function is declared using static syntax and you are using a gecko-based browser (Eg. FireFox). In that case, you can use arguments.callee.name. This doesn't work on functions, if they were declared with dynamic syntax.

    A much better idea, would probably be, to use the firebug extension for FireFox or the MS script debugger for IE, since these tools does that and much more for you.

  3. #3
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    arguments.callee is not deprecated. However Function.arguments.callee is.

    You can try something like this. I'm not sure on the regexp for the function name though, i don't remember the valid chars in a function name.


    Code JavaScript:
     
    function containserror(){
    	var error=true;
    	if (error) {
    		trigger_error(arguments.callee); //arguments.callee passes the function to the error-thingy.
    		return;
    	}
     
    }
     
    function trigger_error(strfunc) {
    	var callerfunc;
    	if ('undefined' !== typeof strfunc.name) { // for gecko
    		callerfunc=strfunc.name;
    	} else { //for everyone else
    		callerfunc=/^function\s*([a-z0-9_\-\$]+)/im.exec(strfunc)[1]
    	}
    	alert(callerfunc);
    }
     
    containserror()
    Last edited by ollelundberg; Dec 13, 2007 at 15:34. Reason: spelling

  4. #4
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is awesome and it works in FireFox, IE7 & IE6!!! If it is not too much to ask, could you explain to me what this regular expression is doing? I am not the greatest when it comes to regular expressions?

    /^function\s*([a-z0-9_\-\$]+)/im

    Thanks again!!!

  5. #5
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Opera and safari too..

    /^function\s*([a-z0-9_\-\$]+)/im

    ^ marks beginning of a string
    function matches the string "function"
    \s matches ny whitespace character the asterisk after tells in to match it any number of times. it is probably better to use a + sign here instead to force at least one space.

    the parentesis creates a capturing group, which means that we can access the things matched inside of the parenthesis

    the brackets lets us define a group of chars to match, in this case a-z 0-9 _ - and $
    the plus makes it match either of the chars in the brackets one or more time.

    the i after the last slash makes it case insensitive
    the m makes i multiline.

    You shold change the regexp to:
    Code JavaScript:
    callerfunc=/^function\s+([a-z0-9_\-\$]+)/im.exec(strfunc)[1]

  6. #6
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK… I am having an issue with this code when I put the code in an object. Would you know why the function name is not displayed when I put the code in an object like in the example below?

    Thanks in advance for your time.

    Code:
    <html>
    
    <head>
    <script language="JavaScript">
    
    function ooErrorTest()
    {
       this.containserror = function()
       {
          var error=true;
    	  
          if (error)
          {
             alert( "function = " + arguments.callee ); 
             trigger_error(arguments.callee); //arguments.callee passes the function to the error-thingy.
             return;
          }
       }
    
       function trigger_error( strfunc )
       {
          var callerfunc;
    	  
          if ('undefined' !== typeof strfunc.name) 
          { // for gecko
             callerfunc=strfunc.name;
          } 
          else 
          { //for everyone else
             callerfunc=/^function\s*([a-z0-9_\-\$]+)/im.exec(strfunc)[1]
          }
          alert(callerfunc);
       }
       
    }
    testError = new ooErrorTest();
    testError.containserror();
    
    </script>
    </head>
    
    <body>
    
    Test
    
    </body>
    </html>

  7. #7
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you do this:

    this.containserror = function()

    you create an anonymous function and arguments.callee holds the the function in which it is called in.

    And since an anonymous funtion don't have a name (except for in ie, where it is called function anonymous (i think) ) it won't fetch any name.

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code returns the name of the function, as it was declared, which may not be the same as it was accessed with. Assume the following:
    Code:
    function foo() {
      console.log(arguments.callee.name);
    }
    var bar = foo;
    foo();
    bar();
    The output in both cases, is foo.

  9. #9
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to get the class name (ooErrorTest) when triggering an error you could use the constructor property of the anonymous function:

    Code JavaScript:
    function ooErrorTest() {
       this.containserror = function() {
          var error=true;
     
          if (error) {
             trigger_error(this.constructor); //this.constructor passes the "class" to the error-thingy.
             return;
          }
       }
     
       function trigger_error( strfunc ) {
          var callerfunc;
     
          if ('undefined' !== typeof strfunc.name) { // for gecko
             callerfunc=strfunc.name;
          } else { //for everyone else
             callerfunc=/^function\s+([a-z0-9_\-\$]+)/im.exec(strfunc)[1]
          }
          alert(callerfunc);
       }
     
    }
    testError = new ooErrorTest();
    testError.containserror();

    However if you would like to get testError you would need to modify your code and make it look something like this (who said it had to look good ):

    Code JavaScript:
    function ooErrorTest(name){
    	var inst=this;
    	inst.instname=name;
    	window[name] = inst;
     
       this.containserror = function() {
          var error=true;
     
          if (error) {
             trigger_error(inst.instname);
             return;
          }
       }
     
       function trigger_error( name ) {
          alert(name);
       }
     
    }
    new ooErrorTest('testError');
    testError.containserror();

    But as stated before it is probable better to use Firebug or similar
    (function(){arguments.callee()})()

  10. #10
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the feedback. What I was initially hoping for was to display the function within the objects name. So for example, the function name this.containserror (or just containserror) would be displayed. But if this is not possible, I might just have to display the object class name (ooErrorTest).

    I did display arguments.callee and it is weird that it changes

    this.containserror = function()

    into

    function = function()

    I guess this is the product of the anonymous function that you were talking about.

    Also, I tried arguments.callee.name and in IE6 it returns undefined.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •