JS_SetErrorReporter 编辑

Obsolete since JSAPI 52
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

 

Get or specify the error reporting mechanism for an application.

Syntax

JSErrorReporter
JS_GetErrorReporter(JSRuntime *rt);

JSErrorReporter
JS_SetErrorReporter(JSRuntime *rt, JSErrorReporter er);
NameTypeDescription
cxJSRuntime *Pointer to a JS runtime whose errors should be reported via your function. 
erJSErrorReporterThe user-defined error reporting function to use in your application, described below.

Callback Syntax

typedef void
(* JSErrorReporter)(JSContext *cx, const char *message, JSErrorReport *report);
NameTypeDescription
cxJSContext *The context in which the error happened.
messageconst char *An error message.
reportJSErrorReport *An error report record containing additional details about the error.

Description

JS_SetErrorReporter enables you to define and use your own error reporting mechanism in your applications. The reporter you define is automatically passed a JSErrorReport structure when an error occurs and has been parsed by JS_ReportError. JS_SetErrorReporter

JS_GetErrorReporter returns the value set by JS_SetErrorReporter.

Typically, the error reporting mechanism you define should log the error where appropriate (such as to a log file), and display an error to the user of your application. The error you log and display can make use of the information passed about the error condition in the JSErrorReport structure.

The error reporter callback must not reenter the JSAPI. Like all other SpiderMonkey callbacks, the error reporter callback must not throw a C++ exception.

Note that JS_SetErrorReporter has no facility to directly pass a user data pointer to the error reporting function. You can work around this by using the JS_SetRuntimePrivate and JS_GetRuntimePrivate methods. Example code with error handling omitted:

    class MyRequest {
    public:

      void execute() {
        auto rt = JS_NewRuntime(memlimit);
        JS_SetRuntimePrivate(rt, this);
        JS_SetErrorReporter(rt, &MyRequest::dispatchError);
        // execute JS
      }

      void onError(const std::string& error) {
        // handle error
      }

      static void dispatchError(
          JSContext* ctx,
          const char* message,
          JSErrorReport* report) {
        auto rt = JS_GetRuntime(ctx);
        auto rt_userdata = JS_GetRuntimePrivate(rt);
        if (rt_userdata) {
          auto req = static_cast<MyRequest*>(rt_userdata);
          req->onError(message);
        }
      }

    };

 

See Also

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

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

发布评论

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

词条统计

浏览:91 次

字数:5085

最后编辑:7年前

编辑次数:0 次

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