" ReferenceError:未定义StackTrace"在Vue应用程序中使用" stackdriver-errors-js"

时间:2017-12-07 12:20:54

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

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

代码和设置:

package.json中的

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

在bootstrap.js

   import errorHandler from './error/error-reporting';
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

和line(stackdriver-errors.js:109)

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

2 个答案:

答案 0 :(得分:0)

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

stackdriver-errors期望存在StackTrace对象。

答案 1 :(得分:0)

由于您要使用的库是实验性的,因此无法在生产环境中使用,因此最好使用已经过测试和验证的不同库以供生产使用。

我建议使用其他library,其中包括与Node.js和JavaScript的Stackdriver错误报告相关的功能。

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

Regex.IsMatch(UserNumber, @"^\d+$")

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

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

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

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

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

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