Skip to content

Commit 362e281

Browse files
bz2steren
authored andcommitted
Refactor (#67)
* Refactor error sending checkpoint * Refactor start method and limit complexity to 10
1 parent 78b49c9 commit 362e281

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"camelcase": ["error", {"properties": "never"}],
2020
"comma-dangle": ["error", "always-multiline"],
2121
"comma-spacing": "error",
22-
"complexity": ["error", 15],
22+
"complexity": ["error", 10],
2323
"computed-property-spacing": "error",
2424
"curly": "error",
2525
"eqeqeq": ["error", "smart"],

stackdriver-errors.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,37 @@ StackdriverErrorReporter.prototype.start = function(config) {
5656
}
5757
this.reportUncaughtExceptions = config.reportUncaughtExceptions !== false;
5858
this.reportUnhandledPromiseRejections = config.reportUnhandledPromiseRejections !== false;
59-
this.disabled = config.disabled || false;
59+
this.disabled = !!config.disabled;
6060

61+
registerHandlers(this);
62+
};
63+
64+
function registerHandlers(reporter) {
6165
// Register as global error handler if requested
6266
var noop = function() {};
63-
var that = this;
64-
if (this.reportUncaughtExceptions) {
67+
if (reporter.reportUncaughtExceptions) {
6568
var oldErrorHandler = window.onerror || noop;
6669

6770
window.onerror = function(message, source, lineno, colno, error) {
6871
if (error) {
69-
that.report(error).catch(noop);
72+
reporter.report(error).catch(noop);
7073
}
7174
oldErrorHandler(message, source, lineno, colno, error);
7275
return true;
7376
};
7477
}
75-
if (this.reportUnhandledPromiseRejections) {
78+
if (reporter.reportUnhandledPromiseRejections) {
7679
var oldPromiseRejectionHandler = window.onunhandledrejection || noop;
7780

7881
window.onunhandledrejection = function(promiseRejectionEvent) {
7982
if (promiseRejectionEvent) {
80-
that.report(promiseRejectionEvent.reason).catch(noop);
83+
reporter.report(promiseRejectionEvent.reason).catch(noop);
8184
}
8285
oldPromiseRejectionHandler(promiseRejectionEvent.reason);
8386
return true;
8487
};
8588
}
86-
};
89+
}
8790

8891
/**
8992
* Report an error to the Stackdriver Error Reporting API
@@ -120,9 +123,19 @@ StackdriverErrorReporter.prototype.report = function(err, options) {
120123
// the first frame when using report() is always this library
121124
firstFrameIndex = options.skipLocalFrames || 1;
122125
}
123-
var that = this;
126+
127+
var reportUrl = this.targetUrl || (
128+
baseAPIUrl + this.projectId + '/events:report?key=' + this.apiKey);
129+
130+
return resolveError(err, firstFrameIndex)
131+
.then(function(message) {
132+
payload.message = message;
133+
return sendErrorPayload(reportUrl, payload);
134+
});
135+
};
136+
137+
function resolveError(err, firstFrameIndex) {
124138
// This will use sourcemaps and normalize the stack frames
125-
// eslint-disable-next-line no-undef
126139
return StackTrace.fromError(err).then(function(stack) {
127140
var lines = [err.toString()];
128141
// Reconstruct to a JS stackframe as expected by Error Reporting parsers.
@@ -145,16 +158,10 @@ StackdriverErrorReporter.prototype.report = function(err, options) {
145158
err.toString(), '\n',
146159
' (', err.file, ':', err.line, ':', err.column, ')',
147160
].join('');
148-
}).then(function(message) {
149-
payload.message = message;
150-
return that.sendErrorPayload(payload);
151161
});
152-
};
153-
154-
StackdriverErrorReporter.prototype.sendErrorPayload = function(payload) {
155-
var defaultUrl = baseAPIUrl + this.projectId + '/events:report?key=' + this.apiKey;
156-
var url = this.targetUrl || defaultUrl;
162+
}
157163

164+
function sendErrorPayload(url, payload) {
158165
var xhr = new XMLHttpRequest();
159166
xhr.open('POST', url, true);
160167
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
@@ -173,7 +180,7 @@ StackdriverErrorReporter.prototype.sendErrorPayload = function(payload) {
173180
};
174181
xhr.send(JSON.stringify(payload));
175182
});
176-
};
183+
}
177184

178185
/**
179186
* Set the user for the current context.

0 commit comments

Comments
 (0)