Skip to content

Refactor raven-js to use CommonJS modules, class instances (2.x) #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Nov 24, 2015
Merged
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
4 changes: 2 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"globalstrict": true,
"browser": true,
"predef": [
"TraceKit",
"console",
"_slice"
"module",
"require"
]
}
79 changes: 57 additions & 22 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
var proxyquire = require('proxyquireify');

module.exports = function(grunt) {
"use strict";

var _ = require('lodash');
var path = require('path');

var coreFiles = [
'template/_header.js',
'vendor/**/*.js',
'src/**/*.js',
'template/_footer.js'
];
var through = require('through2');

var excludedPlugins = [
'react-native'
Expand All @@ -26,6 +22,21 @@ module.exports = function(grunt) {
return path;
});

// custom browserify transformer to re-write plugins to
// self-register with Raven via addPlugin
function AddPluginBrowserifyTransformer() {
return function (file) {
return through(function (buf, enc, next) {
buf = buf.toString('utf8');
if (/plugins/.test(file)) {
buf += "\nrequire('../src/singleton').addPlugin(module.exports);";
}
this.push(buf);
next();
});
};
}

// Taken from http://dzone.com/snippets/calculate-all-combinations
var combine = function (a) {
var fn = function (n, src, got, all) {
Expand Down Expand Up @@ -65,7 +76,7 @@ module.exports = function(grunt) {
key.sort();

var dest = path.join('build/', key.join(','), '/raven.js');
dict[dest] = coreFiles.concat(comb);
dict[dest] = ['src/singleton.js'].concat(comb);

return dict;
}, {});
Expand All @@ -75,18 +86,35 @@ module.exports = function(grunt) {
aws: grunt.file.exists('aws.json') ? grunt.file.readJSON('aws.json'): {},

clean: ['build'],
concat: {

browserify: {
options: {
separator: '\n',
banner: grunt.file.read('template/_copyright.js'),
process: true
browserifyOptions: {
banner: grunt.file.read('template/_copyright.js'),
standalone: 'Raven' // umd
}
},
core: {
src: coreFiles.concat(plugins),
src: 'src/singleton.js',
dest: 'build/raven.js'
},
all: {
files: pluginConcatFiles
plugins: {
files: pluginConcatFiles,
options: {
transform: [
[ new AddPluginBrowserifyTransformer() ]
]
}
},
test: {
src: 'test/**/*.test.js',
dest: 'build/raven.test.js',
options: {
browserifyOptions: {
debug: true // source maps
},
plugin: [proxyquire.plugin]
}
}
},

Expand All @@ -100,7 +128,13 @@ module.exports = function(grunt) {
sourceMappingURL: function (dest) {
return path.basename(dest, '.js') + '.map';
},
preserveComments: 'some'
preserveComments: 'some',
compress: {
dead_code: true,
global_defs: {
"TEST": false
}
}
},
dist: {
src: ['build/**/*.js'],
Expand Down Expand Up @@ -251,13 +285,13 @@ module.exports = function(grunt) {

// Grunt contrib tasks
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');

// 3rd party Grunt tasks
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-mocha');
grunt.loadNpmTasks('grunt-release');
grunt.loadNpmTasks('grunt-s3');
Expand All @@ -266,15 +300,16 @@ module.exports = function(grunt) {

// Build tasks
grunt.registerTask('_prep', ['clean', 'gitinfo', 'version']);
grunt.registerTask('concat.core', ['_prep', 'concat:core']);
grunt.registerTask('concat.all', ['_prep', 'concat:all']);
grunt.registerTask('build.core', ['concat.core', 'uglify', 'fixSourceMaps', 'sri:dist']);
grunt.registerTask('build.all', ['concat.all', 'uglify', 'fixSourceMaps', 'sri:dist', 'sri:build']);
grunt.registerTask('browserify.core', ['_prep', 'browserify:core']);
grunt.registerTask('browserify.plugins', ['_prep', 'browserify:plugins']);
grunt.registerTask('build.test', ['_prep', 'browserify:test']);
grunt.registerTask('build.core', ['browserify.core', 'uglify', 'fixSourceMaps', 'sri:dist']);
grunt.registerTask('build.all', ['browserify.plugins', 'uglify', 'fixSourceMaps', 'sri:dist', 'sri:build']);
grunt.registerTask('build', ['build.all']);
grunt.registerTask('dist', ['build.core', 'copy:dist']);

// Test task
grunt.registerTask('test', ['jshint', 'mocha']);
grunt.registerTask('test', ['jshint', 'browserify.core', 'browserify:test', 'mocha']);

// Webserver tasks
grunt.registerTask('run:test', ['connect:test']);
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"type": "git",
"url": "git://github.com/getsentry/raven-js.git"
},
"main": "dist/raven.js",
"main": "src/singleton.js",
"devDependencies": {
"chai": "~1.8.1",
"grunt": "^0.4.5",
"grunt-browserify": "^4.0.1",
"grunt-cli": "~0.1.9",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-concat": "~0.3.0",
Expand All @@ -29,7 +30,9 @@
"grunt-sri": "mattrobenolt/grunt-sri#pretty",
"jquery": "^2.1.4",
"lodash": "~2.4.0",
"sinon": "~1.7.3"
"proxyquireify": "^3.0.0",
"sinon": "~1.7.3",
"through2": "^2.0.0"
},
"keywords": [
"exceptions",
Expand Down
86 changes: 42 additions & 44 deletions plugins/angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,58 @@
*
* Provides an $exceptionHandler for Angular.js
*/
;(function(window) {
'use strict';

var angular = window.angular,
Raven = window.Raven;
// See https://github.com/angular/angular.js/blob/v1.4.7/src/minErr.js
var angularPattern = /^\[((?:[$a-zA-Z0-9]+:)?(?:[$a-zA-Z0-9]+))\] (.+?)\n(\S+)$/;

// quit if angular isn't on the page
if (!(angular && Raven)) return;
function angularPlugin(Raven, angular) {
/*jshint validthis:true*/
angular = angular || window.angular;

function RavenProvider() {
this.$get = ['$window', function($window, $log) {
return $window.Raven;
}];
}
if (!angular) return;

function ExceptionHandlerProvider($provide) {
$provide.decorator('$exceptionHandler',
['Raven', '$delegate', exceptionHandler]);
}
function RavenProvider() {
this.$get = ['$window', function($window) {
return Raven;
}];
}

function exceptionHandler(Raven, $delegate) {
return function (ex, cause) {
Raven.captureException(ex, {
extra: { cause: cause }
});
$delegate(ex, cause);
};
}
function ExceptionHandlerProvider($provide) {
$provide.decorator('$exceptionHandler',
['Raven', '$delegate', exceptionHandler]);
}

// See https://github.com/angular/angular.js/blob/v1.4.7/src/minErr.js
var angularPattern = /^\[((?:[$a-zA-Z0-9]+:)?(?:[$a-zA-Z0-9]+))\] (.+?)\n(\S+)$/;
function exceptionHandler(Raven, $delegate) {
return function (ex, cause) {
Raven.captureException(ex, {
extra: { cause: cause }
});
$delegate(ex, cause);
};
}

Raven.addPlugin(function () {
angular.module('ngRaven', [])
.provider('Raven', RavenProvider)
.config(['$provide', ExceptionHandlerProvider]);
});

Raven.setDataCallback(function(data) {
// We only care about mutating an exception
var exception = data.exception;
if (exception) {
exception = exception.values[0];
var matches = angularPattern.exec(exception.value);

if (matches) {
// This type now becomes something like: $rootScope:inprog
exception.type = matches[1];
exception.value = matches[2];
data.message = exception.type + ': ' + exception.value;
// auto set a new tag specifically for the angular error url
data.extra.angularDocs = matches[3].substr(0, 250);

Raven.setDataCallback(function(data) {
// We only care about mutating an exception
var exception = data.exception;
if (exception) {
exception = exception.values[0];
var matches = angularPattern.exec(exception.value);

if (matches) {
// This type now becomes something like: $rootScope:inprog
exception.type = matches[1];
exception.value = matches[2];
data.message = exception.type + ': ' + exception.value;
// auto set a new tag specifically for the angular error url
data.extra.angularDocs = matches[3].substr(0, 250);
}
}
}
});
});
}

}(typeof window !== 'undefined' ? window : this));
module.exports = angularPlugin;
89 changes: 43 additions & 46 deletions plugins/backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,54 @@
*
* Patches Backbone.Events callbacks.
*/
;(function(window) {
'use strict';

if (window.Raven) Raven.addPlugin(function backbonePlugin() {
function backbonePlugin(Raven, Backbone) {
Backbone = Backbone || window.Backbone;

var Backbone = window.Backbone;
// quit if Backbone isn't on the page
if (!Backbone) return;

// quit if Backbone isn't on the page
if (!Backbone) return;

function makeBackboneEventsOn(oldOn) {
return function BackboneEventsOn(name, callback, context) {
var wrapCallback = function (cb) {
if (Object.prototype.toString.call(cb) === '[object Function]') {
var _callback = cb._callback || cb;
cb = Raven.wrap(cb);
cb._callback = _callback;
}
return cb;
};
if (Object.prototype.toString.call(name) === '[object Object]') {
// Handle event maps.
for (var key in name) {
if (name.hasOwnProperty(key)) {
name[key] = wrapCallback(name[key]);
function makeBackboneEventsOn(oldOn) {
return function BackboneEventsOn(name, callback, context) {
var wrapCallback = function (cb) {
if (Object.prototype.toString.call(cb) === '[object Function]') {
var _callback = cb._callback || cb;
cb = Raven.wrap(cb);
cb._callback = _callback;
}
return cb;
};
if (Object.prototype.toString.call(name) === '[object Object]') {
// Handle event maps.
for (var key in name) {
if (name.hasOwnProperty(key)) {
name[key] = wrapCallback(name[key]);
}
}
} else {
callback = wrapCallback(callback);
}
} else {
callback = wrapCallback(callback);
}
return oldOn.call(this, name, callback, context);
};
}

// We're too late to catch all of these by simply patching Backbone.Events.on
var affectedObjects = [
Backbone.Events,
Backbone,
Backbone.Model.prototype,
Backbone.Collection.prototype,
Backbone.View.prototype,
Backbone.Router.prototype,
Backbone.History.prototype
], i = 0, l = affectedObjects.length;

for (; i < l; i++) {
var affected = affectedObjects[i];
affected.on = makeBackboneEventsOn(affected.on);
affected.bind = affected.on;
return oldOn.call(this, name, callback, context);
};
}

// We're too late to catch all of these by simply patching Backbone.Events.on
var affectedObjects = [
Backbone.Events,
Backbone,
Backbone.Model.prototype,
Backbone.Collection.prototype,
Backbone.View.prototype,
Backbone.Router.prototype,
Backbone.History.prototype
], i = 0, l = affectedObjects.length;

for (; i < l; i++) {
var affected = affectedObjects[i];
affected.on = makeBackboneEventsOn(affected.on);
affected.bind = affected.on;
}
}

});

}(typeof window !== 'undefined' ? window : this));
module.exports = backbonePlugin;
Loading