summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/raven/raven_config.js
blob: 9c756a4130b74eaec291480b6ea45169de8b3811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Raven from 'raven-js';

const RavenConfig = {
  init(options = {}) {
    this.options = options;

    this.configure();
    this.bindRavenErrors();
    if (this.options.currentUserId) this.setUser();
  },

  configure() {
    Raven.config(this.options.sentryDsn, {
      whitelistUrls: this.options.whitelistUrls,
      environment: this.options.isProduction ? 'production' : 'development',
    }).install();
  },

  setUser() {
    Raven.setUserContext({
      id: this.options.currentUserId,
    });
  },

  bindRavenErrors() {
    window.$(document).on('ajaxError.raven', this.handleRavenErrors);
  },

  handleRavenErrors(event, req, config, err) {
    const error = err || req.statusText;
    const responseText = req.responseText || 'Unknown response text';

    Raven.captureMessage(error, {
      extra: {
        type: config.type,
        url: config.url,
        data: config.data,
        status: req.status,
        response: responseText.substring(0, 100),
        error,
        event,
      },
    });
  },
};

export default RavenConfig;