Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
ref: Unify code style using Prettier and update ESLint rules
Browse files Browse the repository at this point in the history
* Add Prettier, precommit hook using Husky and update ESLint to play nicely with Prettier.
* Run new code-style setup on all relevant .js files, without any changes in the code itself.
* Update ESLint version and unify package.json with the one from raven-js.
  • Loading branch information
kamilogorek authored Sep 1, 2017
1 parent c7819f4 commit 98353fa
Show file tree
Hide file tree
Showing 23 changed files with 804 additions and 630 deletions.
33 changes: 4 additions & 29 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,16 @@
"node": true,
"mocha": true,
},
"extends": "eslint:recommended",
"rules": {
"array-bracket-spacing": [2, "never"],
"block-scoped-var": 2,
"brace-style": [2, "1tbs"],
"camelcase": [1, "properties": "never"],
"comma-dangle": 0,
"comma-spacing": [2, {"before": false, "after": true}],
"comma-style": [2, "last"],
"computed-property-spacing": [2, "never"],
"consistent-return": 2,
"consistent-this": [2, "self"],
"curly": [2, "multi-line"],
"default-case": 2,
"dot-notation": 2,
"eol-last": 2,
"eqeqeq": [2, "allow-null"],
"guard-for-in": 2,
"indent": [2, 2, {"SwitchCase": 1, "VariableDeclarator": { "var": 2, "let": 2, "const": 3}}],
"key-spacing": [1, {"beforeColon": false, "afterColon": true}],
"keyword-spacing": 2,
"max-len": [1, 120],
"new-cap": 2,
"no-alert": 2,
"no-caller": 2,
Expand All @@ -36,17 +24,13 @@
"no-eval": 2,
"no-extra-bind": 2,
"no-extend-native": 2,
"no-extra-parens": 2,
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-implied-eval": 2,
"no-inner-declarations": 2,
"no-iterator": 2,
"no-lonely-if": 2,
"no-loop-func": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multiple-empty-lines": [2, {"max": 2}],
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-new": 2,
"no-param-reassign": 1,
Expand All @@ -58,8 +42,6 @@
"no-sequences": 2,
"no-shadow": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-trailing-spaces": 2,
"no-undef": 2,
"no-undefined": 2,
"no-underscore-dangle": 0,
Expand All @@ -69,20 +51,13 @@
"no-useless-escape": 2,
"no-warning-comments": 0,
"no-with": 2,
"object-curly-spacing": [2, "always"],
"quotes": [2, "single", "avoid-escape"],
"semi": [2, "always"],
"semi-spacing": [2, {"before": false, "after": true}],
"space-before-blocks": 2,
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
"space-in-parens": 2,
"space-infix-ops": 2,
"space-unary-ops": 2,
"spaced-comment": [2, "always"],
"strict": [2, "global"],
"valid-jsdoc": 0,
"valid-typeof": 2,
"wrap-iife": [2, "inside"],
"yoda": [2, "never"]
}
},
"extends": [
"prettier"
]
}
114 changes: 70 additions & 44 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ nodeUtil.inherits(Raven, events.EventEmitter);
extend(Raven.prototype, {
config: function config(dsn, options) {
// We get lots of users using raven-node when they want raven-js, hence this warning if it seems like a browser
if (typeof window !== 'undefined' && typeof document !== 'undefined' && typeof navigator !== 'undefined') {
utils.consoleAlertOnce('This looks like a browser environment; are you sure you don\'t want Raven.js for browser JavaScript? https://sentry.io/for/javascript');
if (
typeof window !== 'undefined' &&
typeof document !== 'undefined' &&
typeof navigator !== 'undefined'
) {
utils.consoleAlertOnce(
"This looks like a browser environment; are you sure you don't want Raven.js for browser JavaScript? https://sentry.io/for/javascript"
);
}

if (arguments.length === 0) {
Expand Down Expand Up @@ -75,21 +81,21 @@ extend(Raven.prototype, {
// enabled if a dsn is set
this._enabled = !!this.dsn;

var globalContext = this._globalContext = {};
var globalContext = (this._globalContext = {});
if (options.tags) {
globalContext.tags = options.tags;
}
if (options.extra) {
globalContext.extra = options.extra;
}

this.onFatalError = this.defaultOnFatalError = function (err, sendErr, eventId) {
this.onFatalError = this.defaultOnFatalError = function(err, sendErr, eventId) {
console.error(err && err.stack ? err.stack : err);
process.exit(1);
};
this.uncaughtErrorHandler = this.makeErrorHandler();

this.on('error', function (err) {
this.on('error', function(err) {
utils.consoleAlert('failed to send exception to sentry: ' + err.message);
});

Expand All @@ -107,8 +113,8 @@ extend(Raven.prototype, {

if (this.captureUnhandledRejections) {
var self = this;
process.on('unhandledRejection', function (reason) {
self.captureException(reason, function (sendErr, eventId) {
process.on('unhandledRejection', function(reason) {
self.captureException(reason, function(sendErr, eventId) {
if (!sendErr) utils.consoleAlert('unhandledRejection captured: ' + eventId);
});
});
Expand All @@ -135,28 +141,30 @@ extend(Raven.prototype, {
return this;
},

makeErrorHandler: function () {
makeErrorHandler: function() {
var self = this;
var caughtFirstError = false;
var caughtSecondError = false;
var calledFatalError = false;
var firstError;
return function (err) {
return function(err) {
if (!caughtFirstError) {
// this is the first uncaught error and the ultimate reason for shutting down
// we want to do absolutely everything possible to ensure it gets captured
// also we want to make sure we don't go recursion crazy if more errors happen after this one
firstError = err;
caughtFirstError = true;
self.captureException(err, function (sendErr, eventId) {
self.captureException(err, function(sendErr, eventId) {
if (!calledFatalError) {
calledFatalError = true;
self.onFatalError(err, sendErr, eventId);
}
});
} else if (calledFatalError) {
// we hit an error *after* calling onFatalError - pretty boned at this point, just shut it down
utils.consoleAlert('uncaught exception after calling fatal error shutdown callback - this is bad! forcing shutdown');
utils.consoleAlert(
'uncaught exception after calling fatal error shutdown callback - this is bad! forcing shutdown'
);
self.defaultOnFatalError(err);
} else if (!caughtSecondError) {
// two cases for how we can hit this branch:
Expand All @@ -174,7 +182,7 @@ extend(Raven.prototype, {
// note that after hitting this branch, we might catch more errors where (caughtSecondError && !calledFatalError)
// we ignore them - they don't matter to us, we're just waiting for the second error timeout to finish
caughtSecondError = true;
setTimeout(function () {
setTimeout(function() {
if (!calledFatalError) {
// it was probably case 1, let's treat err as the sendErr and call onFatalError
calledFatalError = true;
Expand All @@ -199,10 +207,15 @@ extend(Raven.prototype, {
eventId = this.generateEventId();
}

var domainContext = domain.active && domain.active.sentryContext || {};
var domainContext = (domain.active && domain.active.sentryContext) || {};
kwargs.user = extend({}, this._globalContext.user, domainContext.user, kwargs.user);
kwargs.tags = extend({}, this._globalContext.tags, domainContext.tags, kwargs.tags);
kwargs.extra = extend({}, this._globalContext.extra, domainContext.extra, kwargs.extra);
kwargs.extra = extend(
{},
this._globalContext.extra,
domainContext.extra,
kwargs.extra
);
kwargs.breadcrumbs = {
values: domainContext.breadcrumbs || this._globalContext.breadcrumbs || []
};
Expand All @@ -215,7 +228,12 @@ extend(Raven.prototype, {
so we only parse a `req` property if the `request` property is absent/empty (and hence we won't clobber)
parseUser returns a partial kwargs object with a `request` property and possibly a `user` property
*/
kwargs.request = extend({}, this._globalContext.request, domainContext.request, kwargs.request);
kwargs.request = extend(
{},
this._globalContext.request,
domainContext.request,
kwargs.request
);
if (Object.keys(kwargs.request).length === 0) {
var req = extend({}, this._globalContext.req, domainContext.req, kwargs.req);
if (Object.keys(req).length > 0) {
Expand Down Expand Up @@ -259,9 +277,10 @@ extend(Raven.prototype, {
// wish there was a good way to communicate to cb why we didn't send; worth considering cb api change?
// could be shouldSendCallback, could be disabled, could be sample rate
// avoiding setImmediate here because node 0.8
cb && setTimeout(function () {
cb(null, eventId);
}, 0);
cb &&
setTimeout(function() {
cb(null, eventId);
}, 0);
}
},

Expand All @@ -270,14 +289,18 @@ extend(Raven.prototype, {
var skwargs = stringify(kwargs);
var eventId = kwargs.event_id;

zlib.deflate(skwargs, function (err, buff) {
zlib.deflate(skwargs, function(err, buff) {
var message = buff.toString('base64'),
timestamp = new Date().getTime(),
headers = {
'X-Sentry-Auth': utils.getAuthHeader(timestamp, self.dsn.public_key, self.dsn.private_key),
'Content-Type': 'application/octet-stream',
'Content-Length': message.length
};
timestamp = new Date().getTime(),
headers = {
'X-Sentry-Auth': utils.getAuthHeader(
timestamp,
self.dsn.public_key,
self.dsn.private_key
),
'Content-Type': 'application/octet-stream',
'Content-Length': message.length
};

self.transport.send(self, message, headers, eventId, cb);
});
Expand Down Expand Up @@ -313,14 +336,14 @@ extend(Raven.prototype, {

var self = this;
var eventId = this.generateEventId();
parsers.parseError(err, kwargs, function (kw) {
parsers.parseError(err, kwargs, function(kw) {
self.process(eventId, kw, cb);
});

return eventId;
},

context: function (ctx, func) {
context: function(ctx, func) {
if (!func && typeof ctx === 'function') {
func = ctx;
ctx = {};
Expand All @@ -332,7 +355,7 @@ extend(Raven.prototype, {
return this.wrap(ctx, func).apply(null);
},

wrap: function (options, func) {
wrap: function(options, func) {
if (!func && typeof options === 'function') {
func = options;
options = {};
Expand All @@ -359,13 +382,13 @@ extend(Raven.prototype, {
return wrapped;
},

interceptErr: function (options, func) {
interceptErr: function(options, func) {
if (!func && typeof options === 'function') {
func = options;
options = {};
}
var self = this;
var wrapped = function () {
var wrapped = function() {
var err = arguments[0];
if (err instanceof Error) {
self.captureException(err, options);
Expand Down Expand Up @@ -412,10 +435,10 @@ extend(Raven.prototype, {
return this._globalContext;
},

setCallbackHelper: function (propertyName, callback) {
setCallbackHelper: function(propertyName, callback) {
var original = this[propertyName];
if (typeof callback === 'function') {
this[propertyName] = function (data) {
this[propertyName] = function(data) {
return callback(data, original);
};
} else {
Expand All @@ -432,7 +455,7 @@ extend(Raven.prototype, {
* data blob to be mutated before sending
* @return {Raven}
*/
setDataCallback: function (callback) {
setDataCallback: function(callback) {
return this.setCallbackHelper('dataCallback', callback);
},

Expand All @@ -443,42 +466,45 @@ extend(Raven.prototype, {
* introspecting the blob before sending
* @return {Raven}
*/
setShouldSendCallback: function (callback) {
setShouldSendCallback: function(callback) {
return this.setCallbackHelper('shouldSendCallback', callback);
},

requestHandler: function () {
requestHandler: function() {
var self = this;
return function (req, res, next) {
self.context({ req: req }, function () {
return function(req, res, next) {
self.context({req: req}, function() {
domain.active.add(req);
domain.active.add(res);
next();
});
};
},

errorHandler: function () {
errorHandler: function() {
var self = this;
return function (err, req, res, next) {
return function(err, req, res, next) {
var status = err.status || err.statusCode || err.status_code || 500;

// skip anything not marked as an internal server error
if (status < 500) return next(err);

var eventId = self.captureException(err, { req: req });
var eventId = self.captureException(err, {req: req});
res.sentry = eventId;
return next(err);
};
},

captureBreadcrumb: function (breadcrumb) {
captureBreadcrumb: function(breadcrumb) {
// Avoid capturing global-scoped breadcrumbs before instrumentation finishes
if (!this.installed) return;

breadcrumb = extend({
timestamp: +new Date / 1000
}, breadcrumb);
breadcrumb = extend(
{
timestamp: +new Date() / 1000
},
breadcrumb
);
var currCtx = this.getContext();
if (!currCtx.breadcrumbs) currCtx.breadcrumbs = [];
currCtx.breadcrumbs.push(breadcrumb);
Expand Down
Loading

0 comments on commit 98353fa

Please sign in to comment.