Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I ran into a issue when using apex:actionSupport that was swallowing JavaScript errors.

Snippet from page:

<apex:inputField value="{!controllerOli.CustomFieldStartDate__c}" id="start_date" required="true" >
    <apex:actionSupport event="onchange"                                                
        action="{!updateStartDate}"
        rerender="fooOutputPanel"
        oncomplete="setupValidation()"
        status="statusStart">
    </apex:actionSupport>
</apex:inputField>
<apex:actionStatus startText="applying value..." id="statusStart"/>

// The rendered property will only render this outputpanel via ajax. Not on initial page load
<apex:outputPanel id="fooOutputPanel" rendered="{! someControllerProperty }">
    <script>

        // Note: the ready function will never occur with ajax rendering
        //$j(document).ready(function() {
        //    console.log('jquery ready');
        //    setupValidation();
        //});

        function setupValidation() {
            console.log('DEBUG - Validating.');
            // This example exception won't appear in the Chrome JavaScript console
            // Runtime exceptions, i.e. method doesn't exist, are also swallowed.
            throw 'bang';
            console.log('DEBUG - Unreachable.');
        }

    </script>
</apex:outputPanel>

I'd expect to see some indication of the JavaScript exception in the Chrome Console. Instead the oncomplete JavaScript method is called, the first log message is recorded, and then nothing.

Chrome JavaScript Console not showing expected exception

Right now I've reverted to wrapping my custom JavaScript in a try/catch and writing the exception to the console.

Is there a better way to handle exceptions with an apex:actionSupport?


I manually called the same JavaScript method via the console and was able to see the exception.

Chrome JavaScript Console not showing expected exception. Manual call to same method shows the exception

share|improve this question
    
Rather than throwing an exception, have you tried introducing a runtime exception such as invoking a non existent method or such? –  techtrekker Nov 29 '12 at 0:45
    
@techtrekker, this question originated from a runtime exception that occured due to a non existent method that was defined in a jQuery plugin. For some reason in one clients org the plugin methods didn't exist. I suspect there were conflicts with other packages and/or custom code. It did take me some time to track down the bug due to disappearing exceptions. –  Daniel Ballinger Nov 29 '12 at 0:55
    
Seems to me that since setupValidation() is called on dom ready in an outputPanel that is always rendered, that the error (or lack thereof) you've taken a screenshot of doesn't necessarily have anything to do with the actionSupport. Does the same problem occur if you comment out everything besides "fooOutputPanel"? –  Phil Rymek Nov 29 '12 at 4:28
    
@PhilR I'm fairly certain it is the actionSupport. The outputPanel in the example was conditionally rendered, but I had initially excluded this from the example code. I've added another screenshot. The first console log message occurs via the actionSupport call to the onComplete method. If I then manually call the same method I see the uncaught exception in the console. –  Daniel Ballinger Nov 29 '12 at 20:20
add comment

1 Answer

This isn't much better than your logging solution from an end users point of view, but if for some reason you absolutely need to have a JavaScript Error generated you could wrap your callback in a setTimeout.

<apex:actionSupport event="onchange"                                                
    action="{!updateStartDate}"
    rerender="fooOutputPanel"
    oncomplete="setTimeout('setupValidation()',100)"
    status="statusStart">
</apex:actionSupport>

Behind the scenes the oncomplete callback will only schedule the setupValidation() for future execution and then it will complete. Because the future execution will be outside of the swallowing parent context that you observed, the Error will not get swallowed. However, the stacktrace will not preserved either, although I'm not sure how useful that would be anyway. In chrome you'll get:

Uncaught Error: bang
setupValidation
(anonymous function)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.