nsIScriptError 编辑

js/xpconnect/idl/nsIScriptError.idlScriptable Represents JavaScript errors and warnings for use by the console service. 66 Introduced Gecko 1.0 Inherits from: nsIConsoleMessage Last changed in Gecko 1.9 (Firefox 3)

Implemented by: @mozilla.org/scripterror;1. To create an instance, use:

var scriptError = Components.classes["@mozilla.org/scripterror;1"]
                  .createInstance(Components.interfaces.nsIScriptError);

Note: The nsIScriptError2 interface was merged into this interface in Gecko 12.0. Prior to that release, if you wanted to associate an outer window with a script error, you had to use nsIScriptError2 instead. That subclass offered the nsIScriptError.initWithWindowID() method for that purpose; that method is now available in this interface, however.

Method overview

void init(in wstring message, in wstring sourceName, in wstring sourceLine, in PRUint32 lineNumber, in PRUint32 columnNumber, in PRUint32 flags, in string category);
void initWithWindowID(in wstring message, in wstring sourceName, in wstring sourceLine, in PRUint32 lineNumber, in PRUint32 columnNumber, in PRUint32 flags, in string category, in unsigned long long innerWindowID);
AUTF8String toString();

Attributes

AttributeTypeDescription
categorystringA string indicating the category of error that occurred See Categories for a list. Read only.
columnNumberPRUint32The column number where the error occurred. This is used to draw the arrow pointing to the problem character. Read only.
errorMessageAString

The error message in a string format without any context/line number information.

Note: nsIConsoleMessage.message will return the error formatted with file/line information.

Read only.
flagsPRUint32Flags; see Flag constants for a list. Read only.
innerWindowIDunsigned long longThe inner window ID with which the error is associated. This is zero if the error was initialized by calling nsIScriptError.init() instead of initWithWindowID(). Read only.
lineNumberPRUint32The number of the line where the error occurred. Read only.
outerWindowIDunsigned long longThe window ID with which the error is associated. This is zero if the error was initialized by calling nsIScriptError.init() instead of initWithWindowID(). Read only.
sourceLineAStringThe line from the file specified by sourceName. 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. Read only.
sourceNameAStringThe URL of the file in which the error occurred. This will be a hyperlink in the JavaScript Console, so you should use a real URL. You may pass null if it's not applicable. Read only.
timeStamplong longElapsed time, in milliseconds, from a platform-specific zero time to the time the message was created. Read only.

Flag constants

ConstantValueDescription
errorFlag0x0Error messages. A pseudo-flag for the default, error case.
warningFlag0x1Warning messages.
exceptionFlag0x2An exception was thrown for this case - exception-aware hosts can ignore this.
strictFlag0x4One of the flags declared in nsIScriptError.
infoFlag0x8Just a log message

Methods

init()

Initializes the object with information describing the error which occurred.

void init(
  in wstring message,
  in wstring sourceName,
  in wstring sourceLine,
  in PRUint32 lineNumber,
  in PRUint32 columnNumber,
  in PRUint32 flags,
  in string category
);
Parameters
message
The text of the message to add to the log.
sourceName
The URL of the file in which the error occurred. This will be a hyperlink in the JavaScript Console, so you should use a real URL. You may pass null if it's not applicable.
sourceLine
The line number in the source file on which the error occurred. 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.
lineNumber
The number of the line where the error occurred.
columnNumber
The column number where the error occurred. This is used to draw an arrow pointing to the problem character.
flags
One of flags listed in Flag constants.
category
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. See Categories for a partial list.

initWithWindowID()

Initializes the nsIScriptError object with the given inner window's ID.

Note: Prior to Gecko 12.0, this method was provided by the nsIScriptError2 interface, which has now been merged into this one.

void initWithWindowID(
  in wstring message,
  in wstring sourceName,
  in wstring sourceLine,
  in PRUint32 lineNumber,
  in PRUint32 columnNumber,
  in PRUint32 flags,
  in string category,
  in unsigned long long innerWindowID
);
Parameters
message
The text of the message to add to the log.
sourceName
The URL of the file in which the error occurred. This will be a hyperlink in the JavaScript Console, so you should use a real URL. You may pass null if it's not applicable.
sourceLine
The line number in the source file on which the error occurred. 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.
lineNumber
The number of the line where the error occurred.
columnNumber
The column number where the error occurred. This is used to draw an arrow pointing to the problem character.
flags
One of the script error Flag constants
category
A string indicating what kind of code caused the message. There are quite a few category strings and they aren't listed in a single place, so you may need to search the Firefox code to find the one you want. See Categories for a partial list.
innerWindowID
The ID of the inner window associated with the error.

toString()

Outputs a string representing the error message described by the object.

AUTF8String toString();
Parameters

None.

Return value

A string representing the error message described by the nsIScriptError object.

Examples

Logging a message with additional information

In this example nsIScriptError, which implements nsIConsoleMessage, is used to log information to the console including information about the source file and line number of the error.

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);
}

Categories

There are a lot of categories, and they're not all maintained in a single list anywhere in the code. This is an attempt to group them together for reference. Note that these are strings. If you want to log an error at chrome level you should set a Category from the first section (leaving it empty might log it as content error). As Addon author I would recommend using "chrome javascript" for logging exceptions caused by addon code.

Categories the Web Console does not display

  • "XPConnect JavaScript"
  • "component javascript"
  • "chrome javascript"
  • "chrome registration"
  • "XBL"
  • "XBL Prototype Handler"
  • "XBL Content Sink"
  • "xbl javascript"
  • "FrameConstructor"

Categories the Web Console displays

  • "HUDConsole"
  • "CSS Parser"
  • "CSS Loader"
  • "content javascript"
  • "DOM Events"
  • "DOM:HTML"
  • "DOM Window"
  • "SVG"
  • "ImageMap"
  • "HTML"
  • "Canvas"
  • "DOM3 Load"
  • "DOM"
  • "malformed-xml"
  • "DOM Worker javascript"
  • "Mixed Content Blocker"
  • "CSP"
  • "Invalid HSTS Headers"
  • "Insecure Password Field"

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:82 次

字数:15056

最后编辑:8 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文