SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Location
    USA
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is Smart Error Handling Possible?

    Over the past week I've been scouring forums, manuals, and code examples to find the type of error handling I'm looking for, but I just haven't found it.

    My aim is to be able to report individual errors found in each block of code, and display a pop up message with details including: the object of origin, line number, error name, and error description.

    So far, I have successfully created a few objects which work, except for reporting Javascript's 6 types of errors:

    • EvalError: signals the global eval function was used incorrectly.
    • RangeError: indicates a numeric value has exceeded its allowable range of values.
    • ReferenceError: an illegal reference has been detected.
    • SyntaxError: a parsing error occurred.
    • TypeError: the actual type of an operand is different than its expected type.
    • URIError: one of the global URI functions (encodeURI or decodeURI) was used incorrectly.

    source: "Graceful error handling in JavaScript" by Tony Patton - techrepublic.com

    Each error generates its own dynamic, absolutely-positioned div with a close button to hide it. If multiple errors are found, the div boxes stack up offset top and left by 20 pixels, creating the cascading window effect.

    I've tried multiple solutions, and have come up with lots of variations using the standard approaches. These work, but are nowhere near as powerful as the what I need. There's window.onerror for error suppression:

    Code JavaScript:
    <script type="text/javascript">
        function suppressErrors(){
            return true;
        }
        window.onerror = suppressErrors;
    </script>

    .. and for error reporting:

    Code JavaScript:
        <script type="text/javascript">
       function reportErrors(msg, url, line){
     
            alert('message: ' + msg + '\n' +
            'url: ' + url + '\n' +
            'line: ' + line);
            return true;
        }
        window.onerror = reportErrors;
    </script>

    And then, there is the try/catch method for testing individual blocks of code. It can both suppress and report errors:

    Code JavaScript:
        <script type="text/javascript">
        function someFunction(){
            try {
                arr[0] = 9;
            }
            catch(err){
                alert('error name: ' + err.name + '\n' +
                'error message: ' + err.message);
            }
        }
    </script>

    The try/catch method doesn't report syntax errors. This is very important for my purposes.

    The solution I have been researching is to somehow trap the event using addEventListener and attachEvent for individual objects. The onerror event is fired for:

    • HTML tags: <img>, <object>, <style>
    • Javascript objects: window, image


    I want to bypass both window.onerror and try/catch by directly referencing the onerror event for <object> tags. When I construct an object, it doesn't have the onerror event, since it isn't an actual <object> type HTML element.

    Code JavaScript:
        <script type="text/javascript>
        function dataObject(){
            // CODE BLOCK
            this.onerror = errorHandler;
        }
     
        window.onload = function(){
            var oData = new dataObject();
        }
    </script>

    Is it possible to add the onerror event to an object which isn't attached to the DOM tree?

  2. #2
    SitePoint Member
    Join Date
    Sep 2007
    Location
    USA
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After looking at my code - I noticed I was attempting to reference this.addEventListener without first creating the method. I falsely assumed I could do this.

    I have to first write a method together with the this keyword before I can call it...hence the "this.addEventListener is not a function" error. It's treating "addEventListener" as a user-defined method in my object and assumes it's a reference error.

    Ok, so moving on from here...is there a way to add an event listener to an object so I can raise events with the onerror event type?

    The purpose would be for detecting errors inside of objects without enclosing the entire code block with a try/catch.

    If this is possible, error reporting/suppression would be much easier - since I would be able to report the name of the object from which the onerror event was fired with arguments.callee.name. And, it would allow me to suppress any/all errors within the object, and it would simply die without kicking and screaming.

    Maybe I'm dreaming too big here.


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
  •