【问题标题】:"ReferenceError: StackTrace is not defined" in Vue app with "stackdriver-errors-js"带有“stackdriver-errors-js”的 Vue 应用程序中的“ReferenceError:StackTrace 未定义”
【发布时间】:2017-12-07 12:20:54
【问题描述】:

我正在尝试将 stackdriver-error-js library 作为模块集成到我的 Vue 项目中。

代码和设置:

在 package.json 中

    "stackdriver-errors-js": "^0.2.0"

在 bootstrap.js 中

   import errorHandler from './error/error-reporting';

在 error-reporting.js 中

import { StackdriverErrorReporter } from 'stackdriver-errors-js';   

let errorHandler;

errorHandler = new StackdriverErrorReporter();   
errorHandler.start({
    key: "{{.Config.StackDriverApiKey}}",
    projectId: "{{.Config.StackDriverProject}}",
    service: "{{.Config.GoogleCloudProjectID}}",
    version: "{{.Copacknfig.GaeEnv}}",
    disabled: false
});

export default errorHandler;

实际错误

我现在得到的错误是(控制台输出和测试)

[vue-devtools] Ready. Detected Vue v2.4.2
(function testErrorReporting() {window.onerror(null, null, null, null, new Error('Test: Something broke!'));})();

stackdriver-errors.js:109 Uncaught ReferenceError: StackTrace is not defined
    at StackdriverErrorReporter.webpackJsonp.556.StackdriverErrorReporter.report (stackdriver-errors.js:109)
    at window.onerror (stackdriver-errors.js:67)
    at testErrorReporting (<anonymous>:1:40)
    at <anonymous>:1:111

和行(stackdriver-errors.js:109)

...    
StackTrace.fromError(err).then(function(stack){
...

【问题讨论】:

    标签: javascript google-cloud-platform stackdriver google-cloud-error-reporting


    【解决方案1】:

    如果不加载stackdriver-errors-concat.min.js文件,还需要手动加载stacktrace-js模块。

    stackdriver-errors 期望存在 StackTrace 对象。

    【讨论】:

    【解决方案2】:

    由于您要使用的库是实验性的,因此不能在生产环境中使用,因此最好使用经过测试和验证可用于生产的其他库。

    我建议改用另一个 library,它包含与 Node.js 和 JavaScript 的 Stackdriver 错误报告相关的功能。

    首先,通过运行以下命令安装依赖项:

      npm install --save @google-cloud/error-reporting
    

    这会自动将依赖添加到 package.json。

    在error-reporting.js中,你可以通过将这个添加到你的代码中来添加依赖(所有参数都是可选的):

    var errors = require('@google-cloud/error-reporting')({
      projectId: 'my-project-id',
      keyFilename: '/path/to/keyfile.json',
      credentials: require('./path/to/keyfile.json'),
      // if true library will attempt to report errors to the service regardless
      // of the value of NODE_ENV
      // defaults to false
      ignoreEnvironmentCheck: false,
      // determines the logging level internal to the library; levels range 0-5
      // where 0 indicates no logs should be reported and 5 indicates all logs
      // should be reported
      // defaults to 2 (warnings)
      logLevel: 2,
      // determines whether or not unhandled rejections are reported to the
      // error-reporting console
      reportUnhandledRejections: true,
          serviceContext: {
          service: 'my-service',
          version: 'my-service-version'
      }
    });
    

    之后,使用此代码测试 Stackdriver 是否正确报告错误:

    errors.report(new Error('Something broke!'));
    

    请注意,此库目前处于测试阶段,因此将来可能会对其进行一些更改。

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 2018-11-20
      • 2019-12-04
      • 1970-01-01
      • 2016-10-23
      • 2019-04-29
      • 2017-10-30
      • 1970-01-01
      • 2019-12-23
      相关资源
      最近更新 更多