感谢@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;
}
});
}
),
但是,我确实在异常时尝试在我的logger 服务中只使用geting 我关心的道具,并且效果很好。这样做会更好,因为您不会在每次要记录属性更改时都不必要地执行代码:
// 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,而不是 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);