BigW Consortium Gitlab

raven_config.js 2.61 KB
Newer Older
1
import Raven from 'raven-js';
2
import $ from 'jquery';
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
const IGNORE_ERRORS = [
  // Random plugins/extensions
  'top.GLOBALS',
  // See: http://blog.errorception.com/2012/03/tale-of-unfindable-js-error. html
  'originalCreateNotification',
  'canvas.contentDocument',
  'MyApp_RemoveAllHighlights',
  'http://tt.epicplay.com',
  'Can\'t find variable: ZiteReader',
  'jigsaw is not defined',
  'ComboSearch is not defined',
  'http://loading.retry.widdit.com/',
  'atomicFindClose',
  // Facebook borked
  'fb_xd_fragment',
  // ISP "optimizing" proxy - `Cache-Control: no-transform` seems to
  // reduce this. (thanks @acdha)
  // See http://stackoverflow.com/questions/4113268
  'bmi_SafeAddOnload',
  'EBCallBackMessageReceived',
  // See http://toolbar.conduit.com/Developer/HtmlAndGadget/Methods/JSInjection.aspx
  'conduitPage',
];

const IGNORE_URLS = [
  // Facebook flakiness
  /graph\.facebook\.com/i,
  // Facebook blocked
  /connect\.facebook\.net\/en_US\/all\.js/i,
  // Woopra flakiness
  /eatdifferent\.com\.woopra-ns\.com/i,
  /static\.woopra\.com\/js\/woopra\.js/i,
  // Chrome extensions
  /extensions\//i,
  /^chrome:\/\//i,
  // Other plugins
  /127\.0\.0\.1:4001\/isrunning/i,  // Cacaoweb
  /webappstoolbarba\.texthelp\.com\//i,
  /metrics\.itunes\.apple\.com\.edgesuite\.net\//i,
];

const SAMPLE_RATE = 95;

47
const RavenConfig = {
48 49 50
  IGNORE_ERRORS,
  IGNORE_URLS,
  SAMPLE_RATE,
51
  init(options = {}) {
52 53 54 55 56
    this.options = options;

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

  configure() {
60
    Raven.config(this.options.sentryDsn, {
61 62
      release: this.options.release,
      tags: this.options.tags,
63 64
      whitelistUrls: this.options.whitelistUrls,
      environment: this.options.isProduction ? 'production' : 'development',
65 66
      ignoreErrors: this.IGNORE_ERRORS,
      ignoreUrls: this.IGNORE_URLS,
Luke "Jared" Bennett committed
67
      shouldSendCallback: this.shouldSendSample.bind(this),
68
    }).install();
69
  },
70

71
  setUser() {
72 73 74
    Raven.setUserContext({
      id: this.options.currentUserId,
    });
75
  },
76

77
  bindRavenErrors() {
78
    $(document).on('ajaxError.raven', this.handleRavenErrors);
79
  },
80

81
  handleRavenErrors(event, req, config, err) {
82
    const error = err || req.statusText;
83
    const responseText = req.responseText || 'Unknown response text';
84 85 86 87 88 89 90

    Raven.captureMessage(error, {
      extra: {
        type: config.type,
        url: config.url,
        data: config.data,
        status: req.status,
91
        response: responseText,
92 93 94 95
        error,
        event,
      },
    });
96
  },
97 98 99 100

  shouldSendSample() {
    return Math.random() * 100 <= this.SAMPLE_RATE;
  },
Luke "Jared" Bennett committed
101
};
102 103

export default RavenConfig;