summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/raven_config.js.es6
blob: e15eeb9f9cd754db787847bc464693715cd8ac16 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* global Raven */

/*= require lib/utils/load_script */

(() => {
  const global = window.gl || (window.gl = {});

  class RavenConfig {
    static init(options = {}) {
      this.options = options;
      if (!this.options.sentryDsn || !this.options.ravenAssetUrl) return Promise.reject('sentry dsn and raven asset url is required');
      return global.LoadScript.load(this.options.ravenAssetUrl, 'raven-js')
        .then(() => {
          this.configure();
          this.bindRavenErrors();
          if (this.options.currentUserId) this.setUser();
        });
    }

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

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

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

    static handleRavenErrors(event, req, config, err) {
      const error = err || req.statusText;
      Raven.captureMessage(error, {
        extra: {
          type: config.type,
          url: config.url,
          data: config.data,
          status: req.status,
          response: req.responseText.substring(0, 100),
          error,
          event,
        },
      });
    }
  }

  global.RavenConfig = RavenConfig;

  document.addEventListener('DOMContentLoaded', () => {
    if (!window.gon) return;

    global.RavenConfig.init({
      sentryDsn: gon.sentry_dsn,
      ravenAssetUrl: gon.raven_asset_url,
      currentUserId: gon.current_user_id,
      whitelistUrls: [gon.gitlab_url],
      isProduction: gon.is_production,
    }).catch($.noop);
  });
})();