MozillaZine

Error Console

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 18:58, 24 May 2005
Asqueella (Talk | contribs)
(Components.reportError)
<-- Previous diff
Revision as of 12:31, 26 August 2005
Asqueella (Talk | contribs)
(dots in a line)
Next diff -->
Line 6: Line 6:
==Manipulating data displayed in JavaScript Console== ==Manipulating data displayed in JavaScript Console==
Information displayed in JavaScript Console can be accessed and manipulated through <code>nsIConsoleService</code> interface. To get it use the code similar to the following (you can read more about creating XPCOM components and interfaces [http://xulplanet.com/references/xpcomref/creatingcomps.html here]): Information displayed in JavaScript Console can be accessed and manipulated through <code>nsIConsoleService</code> interface. To get it use the code similar to the following (you can read more about creating XPCOM components and interfaces [http://xulplanet.com/references/xpcomref/creatingcomps.html here]):
-<pre>var consoleService = Components.classes['@mozilla.org/consoleservice;1']+ var consoleService = Components.classes['@mozilla.org/consoleservice;1']
- .getService(Components.interfaces.nsIConsoleService);</pre>+ .getService(Components.interfaces.nsIConsoleService);
===Logging custom messages in JavaScript Console=== ===Logging custom messages in JavaScript Console===
Line 14: Line 14:
====The easy way==== ====The easy way====
To print simple string messages to JavaScript Console, use the following code: To print simple string messages to JavaScript Console, use the following code:
-<pre>consoleService.logStringMessage("Hello world!");</pre>+ consoleService.logStringMessage("Hello world!");
You may want to create a helper function like this: You may want to create a helper function like this:
-<pre>+ function myDump(aMessage) {
-function myDump(aMessage) {+ var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
- var consoleService = Components.classes["@mozilla.org/consoleservice;1"]+ .getService(Components.interfaces.nsIConsoleService);
- .getService(Components.interfaces.nsIConsoleService);+ consoleService.logStringMessage("My extension: " + aMessage);
- consoleService.logStringMessage("My extension: " + aMessage);+ }
-}</pre>+
- +
You can also use the <code>dump()</code> function for that. For more details see [[Viewing dump() output]]. You can also use the <code>dump()</code> function for that. For more details see [[Viewing dump() output]].
Line 29: Line 27:
The more tricky way is to pass an <code>nsIScriptError</code> instance to <code>nsIConsoleService.logMessage()</code> method: The more tricky way is to pass an <code>nsIScriptError</code> instance to <code>nsIConsoleService.logMessage()</code> method:
-<pre>function myLogToConsole(aMessage, aSourceName, aSourceLine, aLineNumber, + function myLogToConsole(aMessage, aSourceName, aSourceLine, aLineNumber,
- aColumnNumber, aFlags, aCategory)+ aColumnNumber, aFlags, aCategory)
-{+ {
- var consoleService = Components.classes["@mozilla.org/consoleservice;1"]+ var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
- .getService(Components.interfaces.nsIConsoleService);+ .getService(Components.interfaces.nsIConsoleService);
- var scriptError = Components.classes["@mozilla.org/scripterror;1"]+ var scriptError = Components.classes["@mozilla.org/scripterror;1"]
- .createInstance(Components.interfaces.nsIScriptError);+ .createInstance(Components.interfaces.nsIScriptError);
- scriptError.init(aMessage, aSourceName, aSourceLine, aLineNumber, + scriptError.init(aMessage, aSourceName, aSourceLine, aLineNumber,
- aColumnNumber, aFlags, aCategory);+ aColumnNumber, aFlags, aCategory);
- consoleService.logMessage(scriptError);+ consoleService.logMessage(scriptError);
-}</pre>+ }
The above function logs a message like this to JavaScript Console: The above function logs a message like this to JavaScript Console:
-<pre>Warning: function goQuitApplication does not always return a value+ Warning: function goQuitApplication does not always return a value
-Source File: chrome://global/content/globalOverlay.js Line: 68+ Source File: chrome://global/content/globalOverlay.js Line: 68
- return true;+ return true;
---------------^</pre>+ --------------^
Now to the meaning of function's parameters: Now to the meaning of function's parameters:
Line 56: Line 54:
An example call, that generates the above warning: An example call, that generates the above warning:
-<pre>myLogToConsole("function goQuitApplication does not always return a value", + myLogToConsole("function goQuitApplication does not always return a value",
- "chrome://global/content/globalOverlay.js", " return true;", 68, 13, + "chrome://global/content/globalOverlay.js", " return true;", 68, 13,
- Components.interfaces.nsIScriptError.warningFlag, "XUL javascript");</pre>+ Components.interfaces.nsIScriptError.warningFlag, "XUL javascript");
==== Using <code>Components.reportError</code> ==== ==== Using <code>Components.reportError</code> ====
Line 64: Line 62:
Example 1. This is how <code>Components.reportError()</code> is intended to be used. Example 1. This is how <code>Components.reportError()</code> is intended to be used.
-<pre>+ try {
-try {+ ...
- ...+ } catch(e) {
-} catch(e) {+ // handle the exception
- // handle the exception+ Components.reportError(e); // still report it to the console
- Components.reportError(e); // still report it to the console+ }
-}+
-</pre>+
- +
Example 2. This is a very easy way to print a string to the JS Console, which you can use for debugging, if you can't be bothered to obtain a reference to console service (as described above). Example 2. This is a very easy way to print a string to the JS Console, which you can use for debugging, if you can't be bothered to obtain a reference to console service (as described above).

Revision as of 12:31, 26 August 2005

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

Contents

What is JavaScript Console

JavaScript Console is a tool available in most Mozilla-based applications that is used for reporting errors in the application chrome and in web pages user opens. Despite its name it reports not only JavaScript-related errors and warnings, but also CSS errors and arbitrary messages from chrome code.

Manipulating data displayed in JavaScript Console

Information displayed in JavaScript Console can be accessed and manipulated through nsIConsoleService interface. To get it use the code similar to the following (you can read more about creating XPCOM components and interfaces here):

var consoleService = Components.classes['@mozilla.org/consoleservice;1']
                               .getService(Components.interfaces.nsIConsoleService);

Logging custom messages in JavaScript Console

There are two functions in nsIConsoleService that may be used to log messages from your code: logMessage() and logStringMessage(). The former is more flexible, while the latter is easier to use. How to use each of them is explained below.

The easy way

To print simple string messages to JavaScript Console, use the following code:

consoleService.logStringMessage("Hello world!");

You may want to create a helper function like this:

function myDump(aMessage) {
  var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                 .getService(Components.interfaces.nsIConsoleService);
  consoleService.logStringMessage("My extension: " + aMessage);
}

You can also use the dump() function for that. For more details see Viewing dump() output.

The advanced way

The more tricky way is to pass an nsIScriptError instance to nsIConsoleService.logMessage() method:

function myLogToConsole(aMessage, aSourceName, aSourceLine, aLineNumber, 
                        aColumnNumber, aFlags, aCategory)
{
  var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                 .getService(Components.interfaces.nsIConsoleService);
  var scriptError = Components.classes["@mozilla.org/scripterror;1"]
                              .createInstance(Components.interfaces.nsIScriptError);
  scriptError.init(aMessage, aSourceName, aSourceLine, aLineNumber, 
                   aColumnNumber, aFlags, aCategory);
  consoleService.logMessage(scriptError);
}

The above function logs a message like this to JavaScript Console:

Warning: function goQuitApplication does not always return a value
Source File: chrome://global/content/globalOverlay.js     Line: 68
  return true;
--------------^

Now to the meaning of function's parameters:

  • aMessage — the string to be logged. You must provide this.
  • aSourceName — the URL of file with error. This will be a hyperlink in the JavaScript Console, so you'd better use real URL. You may pass null if it's not applicable.
  • aSourceLine — the line #aLineNumber from aSourceName file. You are responsible for providing that line. You may pass null if you are lazy; that will prevent showing the source line in JavaScript Console.
  • aLineNumber and aColumnNumber — specify the exact location of error. aColumnNumber is used to draw the arrow pointing to the problem character.
  • aFlags — one of flags declared in nsIScriptError. At the time of writing, possible values are: nsIScriptError.errorFlag = 0, nsIScriptError.warningFlag = 1, nsIScriptError.exceptionFlag = 2 and nsIScriptError.strictFlag = 4.
  • aCategory — a string indicating what kind of code caused the message. There are quite a few category strings and they don't seem to be listed in a single place. Hopefully, they will all be listed in nsIScriptError.idl eventually.

An example call, that generates the above warning:

myLogToConsole("function goQuitApplication does not always return a value", 
  "chrome://global/content/globalOverlay.js", "  return true;", 68, 13, 
  Components.interfaces.nsIScriptError.warningFlag, "XUL javascript");

Using Components.reportError

In Firefox 1.1a and later (and other products) there's a new method on globally available Components object is implemented: Components.reportError(). It is intended to be used in a catch block to report caught errors in the JS Console, but you can also use it to log arbitrary messages. Note, that unlike nsIConsoleService.log[String]Message(), Components.reportError() reports the message with severity "error".

Example 1. This is how Components.reportError() is intended to be used.

try {
  ...
} catch(e) {
  // handle the exception
  Components.reportError(e); // still report it to the console
}

Example 2. This is a very easy way to print a string to the JS Console, which you can use for debugging, if you can't be bothered to obtain a reference to console service (as described above).

Components.reportError("hi!")

Again, this only works on Firefox/Thunderbird 1.1a or later.

Using console listeners

This section is a stub. You can help MozillaZine knowledge base by writing it.
  • What are console listeners. When they are used (in JS Console implementation, for example).
  • An example of console listener, registered with nsIConsoleService.registerListener().

Accessing available messages

This section is a stub. You can help MozillaZine knowledge base by writing it.
  • An example using nsIConsoleService.getMessageArray().
  • Possible performance problems.

Miscellaneous

toJavaScriptConsole()

If your code is executed in a browser or mail overlay, the toJavaScriptConsole() helper function is available. It opens the JavaScript Console, useful when debugging an extension.

Resources