Skip to content

Commit 7b60bc8

Browse files
committed
Auto merge of #3444 - Turbo87:sentry, r=pichfl
Extract `sentry` service This makes it easier to mock the Sentry code during tests to ensure that whether we send certain types of errors to Sentry or not.
2 parents 5769bc6 + 37219a1 commit 7b60bc8

File tree

8 files changed

+65
-11
lines changed

8 files changed

+65
-11
lines changed

app/components/crate-sidebar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { gt, readOnly } from '@ember/object/computed';
33
import { inject as service } from '@ember/service';
44
import Component from '@glimmer/component';
55

6-
import * as Sentry from '@sentry/browser';
76
import { didCancel } from 'ember-concurrency';
87

98
import { simplifyUrl } from './crate-sidebar/link';
@@ -12,6 +11,7 @@ const NUM_VERSIONS = 5;
1211

1312
export default class DownloadGraph extends Component {
1413
@service playground;
14+
@service sentry;
1515

1616
@readOnly('args.crate.versions') sortedVersions;
1717

@@ -53,7 +53,7 @@ export default class DownloadGraph extends Component {
5353
this.playground.loadCratesTask.perform().catch(error => {
5454
if (!(didCancel(error) || error.isServerError || error.isNetworkError)) {
5555
// report unexpected errors to Sentry
56-
Sentry.captureException(error);
56+
this.sentry.captureException(error);
5757
}
5858
});
5959
}

app/routes/application.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { action } from '@ember/object';
22
import Route from '@ember/routing/route';
33
import { inject as service } from '@ember/service';
44

5-
import * as Sentry from '@sentry/browser';
65
import { rawTimeout, task } from 'ember-concurrency';
76

87
export default class ApplicationRoute extends Route {
98
@service progress;
109
@service router;
1110
@service session;
1211
@service playground;
12+
@service sentry;
1313

1414
beforeModel() {
1515
this.router.on('routeDidChange', () => {
16-
Sentry.configureScope(scope => {
16+
this.sentry.configureScope(scope => {
1717
scope.setTag('routeName', this.router.currentRouteName);
1818
});
1919
});

app/routes/crate/version.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Route from '@ember/routing/route';
22
import { inject as service } from '@ember/service';
33

4-
import * as Sentry from '@sentry/browser';
54
import { didCancel } from 'ember-concurrency';
65

76
import { AjaxError } from '../../utils/ajax';
87

98
export default class VersionRoute extends Route {
109
@service notifications;
10+
@service sentry;
1111

1212
async model(params) {
1313
let crate = this.modelFor('crate');
@@ -41,7 +41,7 @@ export default class VersionRoute extends Route {
4141
version.loadDocsBuildsTask.perform().catch(error => {
4242
// report unexpected errors to Sentry and ignore `ajax()` errors
4343
if (!didCancel(error) && !(error instanceof AjaxError)) {
44-
Sentry.captureException(error);
44+
this.sentry.captureException(error);
4545
}
4646
});
4747
}

app/services/progress.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { htmlSafe } from '@ember/template';
33
import { tracked } from '@glimmer/tracking';
44
import Ember from 'ember';
55

6-
import * as Sentry from '@sentry/browser';
76
import { didCancel, rawTimeout, task } from 'ember-concurrency';
87

98
const SPEED = 200;
109

1110
export default class ProgressService extends Service {
1211
@service router;
12+
@service sentry;
1313

1414
@tracked _style = '';
1515

@@ -28,7 +28,7 @@ export default class ProgressService extends Service {
2828
this.updateTask.perform().catch(error => {
2929
if (!didCancel(error)) {
3030
// this task shouldn't be able to fail, but if it does we'll let Sentry know
31-
Sentry.captureException(error);
31+
this.sentry.captureException(error);
3232
}
3333
});
3434

app/services/sentry.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Service from '@ember/service';
2+
3+
import * as Sentry from '@sentry/browser';
4+
5+
export default class SentryService extends Service {
6+
captureException(error, captureContext) {
7+
Sentry.captureException(error, captureContext);
8+
}
9+
10+
configureScope(callback) {
11+
Sentry.configureScope(callback);
12+
}
13+
14+
setUser(user) {
15+
Sentry.setUser(user);
16+
}
17+
}

app/services/session.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { alias } from '@ember/object/computed';
22
import Service, { inject as service } from '@ember/service';
33

4-
import * as Sentry from '@sentry/browser';
54
import { race, rawTimeout, task, waitForEvent } from 'ember-concurrency';
65
import window from 'ember-window-mock';
76

@@ -12,6 +11,7 @@ export default class SessionService extends Service {
1211
@service store;
1312
@service notifications;
1413
@service router;
14+
@service sentry;
1515

1616
savedTransition = null;
1717

@@ -129,7 +129,7 @@ export default class SessionService extends Service {
129129
this.isLoggedIn = false;
130130

131131
yield this.loadUserTask.cancelAll({ resetState: true });
132-
Sentry.setUser(null);
132+
this.sentry.setUser(null);
133133

134134
this.router.transitionTo('index');
135135
})
@@ -149,7 +149,7 @@ export default class SessionService extends Service {
149149
let ownedCrates = response.owned_crates.map(c => this.store.push(this.store.normalize('owned-crate', c)));
150150

151151
let { id } = currentUser;
152-
Sentry.setUser({ id });
152+
this.sentry.setUser({ id });
153153

154154
return { currentUser, ownedCrates };
155155
}).drop())

tests/helpers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { setupApplicationTest as upstreamSetupApplicationTest } from 'ember-qunit';
22

3+
import { setupSentryMock } from './sentry';
34
import setupMirage from './setup-mirage';
45

56
export { setupTest, setupRenderingTest } from 'ember-qunit';
@@ -8,4 +9,5 @@ export { setupTest, setupRenderingTest } from 'ember-qunit';
89
export function setupApplicationTest(hooks, options) {
910
upstreamSetupApplicationTest(hooks, options);
1011
setupMirage(hooks);
12+
setupSentryMock(hooks);
1113
}

tests/helpers/sentry.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Service from '@ember/service';
2+
3+
class MockSentryService extends Service {
4+
events = [];
5+
scope = new MockScope();
6+
7+
captureException(error) {
8+
let { scope, user } = this;
9+
let { tags } = scope;
10+
let event = { error, tags, user };
11+
this.events.push(event);
12+
}
13+
14+
configureScope(callback) {
15+
callback(this.scope);
16+
}
17+
18+
setUser(user) {
19+
this.user = user;
20+
}
21+
}
22+
23+
class MockScope {
24+
tags = {};
25+
26+
setTag(key, value) {
27+
this.tags[key] = value;
28+
}
29+
}
30+
31+
export function setupSentryMock(hooks) {
32+
hooks.beforeEach(function () {
33+
this.owner.register('service:sentry', MockSentryService);
34+
});
35+
}

0 commit comments

Comments
 (0)