Ember sentry / raven:在用户模型加载时设置用户上下文?

时间:2018-01-18 19:48:45

标签: ember.js ember-data sentry raven

%Preamble%

我正在设置ember-cli-sentry并希望识别用户,以便在我们报告异常时,我们知道这些异常与哪些用户相关联,以防它们是特定于用户的。 (比方说,我们部署到分段,我们从后端获得了一些不好的数据,导致只有那些将错误数据输入其帐户的用户出现了某些错误。)

&安培;实际问题&

我想在加载用户模型后立即执行一些代码。我不知道将触发用户模型加载的路由。

理论上,实际上,我可以在完成对该数据的请求之后立即获得用户模型数据的最早位置。我想知道,劫持XHR方法会不会很疯狂,当请求用户ID时,运行我的代码告诉raven / sentry这个用户是谁?

如果只是一个"加载"那会更好。挂钩模型文件,但我从来没有听说过这样的事情。也许这是一个需要的用例?

google / seo的h1:

Ember:在从任何路线加载模型后挂钩执行代码?如何从任何路径加载模型后执行代码?

3 个答案:

答案 0 :(得分:1)

也许在你的main application route你可以这样做:

user: alias('model')

userModelLoaded: observer('user.id', function() {
  // execute code to set up Raven
})

答案 1 :(得分:1)

我建议

Raven.setUserContext({
    email: 'matt@example.com',
    id: '123'
});

答案 2 :(得分:0)

谢谢@duizendnegen和@Isaac Askew,观察员是一个很好的起点,并引导我到最后的解决方案。放置在我的应用程序路径中的这个观察者代码帮助我弄清楚我可以添加为用户上下文的内容:

window.serviceState = {};
// eslint-disable-next-line
let serviceState = window.serviceState;

export default Route.extend(ApplicationRouteMixin, {
  // observes services for state changes we care about and stores them in
  // a separate variable so when an exception occurs, we can simply include that
  // variable and avoid the risk of causing another exception by trying
  // to read them in `captureException`
  serviceStateTracker: observer(
    'service1.prop1',
    'service2.propA',
    function() {
      [
        'service1.prop1',
        'service2.propA',
      ].forEach((prop) => {
        let newValue = this.get(prop);
        if (serviceState[prop] !== newValue) {
          if (serviceState[prop] === undefined) {
            console.log(prop, newValue);
          } else {
            console.log(prop, 'changed from:', serviceState[prop], 'to:', newValue);
          }
          serviceState[prop] = newValue;
        }
      });
    }
  ),

但是,我确实在异常时间尝试getlogger服务中关注的道具,这很有效。它更好,因为每次要记录的属性发生更改时,您都不会不必要地执行代码:

// app/services/logr.js
// initially generated with ember-cli-sentry's `ember g logger logr`
import RavenLogger from 'ember-cli-deploy-sentry/services/raven';
import Ember from 'ember';

const { Logger, inject: { service } } = Ember;

export default RavenLogger.extend({

  unhandledPromiseErrorMessage: '',
  user: service(),
  i18n: service(),

  addContext() {
    if (this == null) {
      // eslint-disable-next-line
      console.warn('logr cannot addContext because this is null/undefined, continuing without');
      return;
    }
    let tagsContext = {
      locale: this.get('i18n.locale'),
      id: this.get('user.id'),
      firstName: this.get('user.firstName'),
      lastName: this.get('user.lastName'),
      email: this.get('user.email')
    };
    // console.log('tagsContext', tagsContext);
    // For some reason setUserContext was not working, but setTagsContext was,
    // so I am using tags for all the user info. Maybe upgrading raven could fix this?
    this.callRaven('setTagsContext', tagsContext);
    // why use callRaven: https://github.com/damiencaselli/ember-cli-sentry/issues/58
  },

  // work around for unmerged PR:
  // https://github.com/damiencaselli/ember-cli-sentry/pull/97
  captureException(/* error */) {
    if (this.get('isRavenUsable')) {
      this.addContext();
      window.Raven.captureException(...arguments);
    } else {
      Logger.debug(...arguments);
    }
    return true;
  },

  captureMessage(/* message */) {
    if (this.get('isRavenUsable')) {
      this.addContext();
      window.Raven.captureMessage(...arguments);
    } else {
      Logger.debug(...arguments);
    }
    return true;
  }
});

出于某种原因,观察者无法在我的logr服务中工作..

额外说明:

  • 使用ember-cli-deploy-sentry插件,不要忘记在config/DEPLOY.js中放置THAT配置,而不是config/environment.js

在某个应用程序路由挂钩中的某处触发错误:

setTimeout(() => {
  // I was having some issue with `throw new Error()` so I did this instead
  // It's actually just a better way to manually 'throw an exception'
  // because it will not forcibly crash the application, but instead
  // at least attempt to continue running
  this.get('logr').captureException(new Error('sentry user context test'));
}, 10000);
  • 不要忘记设置development: false让raven实际将此测试错误发送给哨兵(除非您没有端到端测试)(我通常有这样的设置:development: environment === 'development'

在应用程序路由操作中,将其添加到错误操作处理程序:

error(error) {
  // Trap unhandled failures during routing:
  // Should maybe be handled by the sentry addon, but is not as of now:
  // https://github.com/damiencaselli/ember-cli-sentry/issues/105
  this.get('logr').captureException(error);