Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions plugins/electron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Electron plugin for Raven
*
* Usage:
* var Raven = require('raven-js');
* require('raven-js/plugins/electron')(Raven);
*
* Options:
*
* pathMatch: A RegExp that matches the portions of a file URI that should be
* send to Sentry.
*
*/
'use strict';

var PATH_MATCH_RE = /[^/]+$/;

/**
* Strip device-specific url part from Electron file:// paths
*/
function normalizeUrl(url, pathMatchRe) {
var match = url.match(pathMatchRe);
return match ? match[0] : '';
}

/**
* Initializes Electron plugin
*/
function electronPlugin(Raven, options) {
options = options || {};

// Use data callback to strip device-specific paths from stack traces
Raven.setDataCallback(function(data) {
electronPlugin._normalizeData(data, options.pathMatch)
});
}

/**
* Strip device-specific url paths found in culprit and frame filenames
*/
electronPlugin._normalizeData = function (data, pathMatchRe) {
if (!pathMatchRe) {
pathMatchRe = PATH_MATCH_RE;
}

if (data.culprit) {
data.culprit = normalizeUrl(data.culprit, pathMatchRe);
}

if (data.exception) {
// if data.exception exists, all of the other keys are guaranteed to exist
data.exception.values[0].stacktrace.frames.forEach(function (frame) {
frame.filename = normalizeUrl(frame.filename, pathMatchRe);
});
}
};

module.exports = electronPlugin;
66 changes: 66 additions & 0 deletions test/plugins/electron.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var electronPlugin = require('../../plugins/electron');

describe('React Native plugin', function () {

describe('_normalizeData()', function () {

it('default pathMatch', function () {
var data = {
culprit: 'file:///x/yy/zzz/Electron.app/Contents/app.asar/app.js',
exception: {
values: [{
stacktrace: {
frames: [
{
filename: 'file:///x/yy/zzz/Electron.app/Contents/app.asar/file1.js',
},
{
filename: 'file:///x/yy/zzz/Electron.app/Contents/app.asar/file2.js',
}
]
}
}],
}
};

electronPlugin._normalizeData(data);

assert.equal(data.culprit, 'app.js');

var frames = data.exception.values[0].stacktrace.frames;
assert.equal(frames[0].filename, 'file1.js');
assert.equal(frames[1].filename, 'file2.js');
});

it('custom pathMatch', function () {
var customPathMatch = /folder\/.+$/;
var data = {
culprit: 'file:///x/yy/zzz/Electron.app/Contents/app.asar/folder/app.js',
exception: {
values: [{
stacktrace: {
frames: [
{
filename: 'file:///x/yy/zzz/Electron.app/Contents/app.asar/folder/file1.js',
},
{
filename: 'file:///x/yy/zzz/Electron.app/Contents/app.asar/folder/file2.js',
}
]
}
}],
}
};

electronPlugin._normalizeData(data, customPathMatch);

assert.equal(data.culprit, 'folder/app.js');

var frames = data.exception.values[0].stacktrace.frames;
assert.equal(frames[0].filename, 'folder/file1.js');
assert.equal(frames[1].filename, 'folder/file2.js');
});

});

});