¿Cómo hacer que la consola de errores de JavaScript imprima errores dentro de las devoluciones de llamada?

If I have a javascript error afuera of a callback, Firebug console prints out the error. However, if I have ant error dentro a callback, I will not see any printout.

document.observe('dom:loaded', function() {
    // not notified in console
    alert(undefinedVariable);
});

// notified in console
alert(anotherUndefinedVariable);

Almost all of my code is inside callbacks, so it is making it hard for me to track down errors. How do I print out cada error in the console no matter where the error occurs?

preguntado el 01 de febrero de 12 a las 01:02

Since it seems you're using Prototype, could it be related to this? : stackoverflow.com/questions/2601411/… -

Did you try the same code in Chrome ? -

2 Respuestas

I needed to tell Prototype to not swallow the exceptions.

Ajax.Responders.register({ 
        onException: function(request, exception) { 
                (function() { throw exception; }).defer(); 
        } 
});

respondido 07 mar '12, 00:03

You can try printing to the console explicitly -- window.console.log(...something...) should work from anywhere.

Respondido 01 Feb 12, 07:02

I would like to be notified of errors automatically via the error console without having to manually console every variable in existence. - JoJo

I'm not sure what you're saying here? whether you write "alert(...)" or "window.console.log(...)" are equivalent in the sense that you wrote that line, so if you don't want to write all those lines yourself, it doesn't matter what the lines is? As for automatic notification without writing any code: JavaScript engines only do this for things that throw, and consequently terminate-if-not-caught your script. So... can you better explain what you really want? - Mike 'Pomax' Kamermans

The alert was just an example. I want to print out all errors, regardless of whether it's alerted or not. For example, this line does not alert anything: var foo = undefinedVar + 5, but it should print an error to the console. - JoJo

you'll want to rephrase your question then. This code, for instance, generates two errors, both of which show up in the various browser's debugging tools: setTimeout(function(){alert(undefinedVariable);},1000); alert(anotherUndefinedVariable); If you're seeing something else, create a demonstrator jsfiddle.net post and add it to your question as a demonstrator of what actually goes wrong. - Mike 'Pomax' Kamermans

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.