Skip to content

Commit ff717d5

Browse files
committed
code improvement
1 parent 4696c81 commit ff717d5

4 files changed

Lines changed: 34 additions & 34 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Here's an introductory video:
3232
<!-- Warning: This is an experimental library, do not use it on production environments -->
3333
<script src="https://cdn.rawgit.com/GoogleCloudPlatform/stackdriver-errors-js/v0.0.3/dist/stackdriver-errors-concat.min.js"></script>
3434
<script type="text/javascript">
35-
var errorHandler = new StackdriverErrorReporting();
36-
errorHandler.init({
35+
var errorHandler = new StackdriverErrorReporter();
36+
errorHandler.start({
3737
key: '<my-api-key>',
3838
projectId: '<my-project-id>'
3939
});
@@ -63,8 +63,8 @@ Here are all the initialization options available:
6363
<!-- Warning: This is an experimental library, do not use it on production environments -->
6464
<script src="https://cdn.rawgit.com/GoogleCloudPlatform/stackdriver-errors-js/v0.0.3/dist/stackdriver-errors-concat.min.js"></script>
6565
<script type="text/javascript">
66-
var errorHandler = new StackdriverErrorReporting();
67-
errorHandler.init({
66+
var errorHandler = new StackdriverErrorReporter();
67+
errorHandler.start({
6868
key: '<my-api-key>',
6969
projectId: '<my-project-id>',
7070
service: '<my-service>', // (optional)

demo/demo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var StackdriverErrors = new StackdriverErrorReporting();
16+
var StackdriverErrors = new StackdriverErrorReporter();
1717

1818
function updateConfig() {
1919
var key = document.getElementById('input-apikey').value;
@@ -22,7 +22,7 @@ function updateConfig() {
2222
localStorage.setItem('key', key);
2323
localStorage.setItem('projectId', projectId);
2424

25-
StackdriverErrors.init({
25+
StackdriverErrors.start({
2626
key: key,
2727
projectId: projectId,
2828
service: 'webapp',

stackdriver-errors.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616
(function(exports) {
1717
"use strict";
1818

19+
/**
20+
* URL endpoint of the Stackdriver Error Reporting report API.
21+
*/
22+
var baseAPIUrl = "https://clouderrorreporting.googleapis.com/v1beta1/projects/";
23+
1924
/**
2025
* An Error handler that sends errors to the Stackdriver Error Reporting API.
2126
*/
22-
var StackdriverErrorReporting = function() {};
23-
exports.StackdriverErrorReporting = StackdriverErrorReporting;
27+
var StackdriverErrorReporter = function() {};
28+
exports.StackdriverErrorReporter = StackdriverErrorReporter;
2429

2530
/**
26-
* Initialize the StackdriverErrorReporting object.
31+
* Initialize the StackdriverErrorReporter object.
2732
* @param {Object} config - the init configuration.
2833
* @param {String} config.key - the API key to use to call the API.
2934
* @param {String} config.projectId - the Google Cloud Platform project ID to report errors to.
@@ -32,7 +37,7 @@
3237
* @param {Boolean} [config.reportUncaughtExceptions=true] - Set to false to stop reporting unhandled exceptions.
3338
* @param {Boolean} [config.disabled=false] - Set to true to not report errors when calling report(), this can be used when developping locally.
3439
*/
35-
StackdriverErrorReporting.prototype.init = function(config) {
40+
StackdriverErrorReporter.prototype.start = function(config) {
3641
if(!config.key) {
3742
throw new Error('Cannot initialize: No API key provided.');
3843
}
@@ -42,23 +47,17 @@
4247

4348
this.apiKey = config.key;
4449
this.projectId = config.projectId;
45-
this.serviceContext = {service: 'web'};
46-
if(config.service) {
47-
this.serviceContext.service = config.service;
48-
}
50+
this.serviceContext = {service: config.service || 'web'};
4951
if(config.version) {
5052
this.serviceContext.version = config.version;
5153
}
52-
this.reportUncaughtExceptions = config.reportUncaughtExceptions || true;
54+
this.reportUncaughtExceptions = !(config.reportUncaughtExceptions === false);
5355
this.disabled = config.disabled || false;
5456

5557
// Register as global error handler if requested
5658
var that = this;
5759
if(this.reportUncaughtExceptions) {
58-
var oldErrorHandler = function(){};
59-
if(window.onerror) {
60-
oldErrorHandler = window.onerror;
61-
}
60+
var oldErrorHandler = window.onerror || function(){};
6261

6362
window.onerror = function(message, source, lineno, colno, error) {
6463
if(error){
@@ -73,8 +72,9 @@
7372
/**
7473
* Report an error to the Stackdriver Error Reporting API
7574
* @param {Error|String} err - The Error object or message string to report.
75+
* @param callback - Calback function to be called once error has been reported.
7676
*/
77-
StackdriverErrorReporting.prototype.report = function(err, callback) {
77+
StackdriverErrorReporter.prototype.report = function(err, callback) {
7878
if(this.disabled) {
7979
return typeof callback === 'function' && callback();
8080
}
@@ -103,21 +103,21 @@
103103
firstFrameIndex = 1;
104104
}
105105
var that = this;
106+
// This will use sourcemaps and normalize the stack frames
106107
StackTrace.fromError(err).then(function(stack){
107108
payload.message = err.toString();
108109
for(var s = firstFrameIndex; s < stack.length; s++) {
109110
payload.message += '\n';
110-
// reconstruct the stackframe to look like a JS stackframe.
111-
// stack[s].source should not be used because not populated created from source map.
111+
// Reconstruct the stackframe to a JS stackframe as expected by Error Reporting parsers.
112+
// stack[s].source should not be used because not populated when created from source map.
112113
payload.message += [' at ', stack[s].getFunctionName(), ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join('');
113114
}
114115
that.sendErrorPayload(payload, callback);
115116
});
116117
};
117118

118-
StackdriverErrorReporting.prototype.sendErrorPayload = function(payload, callback) {
119-
var baseUrl = "https://clouderrorreporting.googleapis.com/v1beta1/projects/";
120-
var url = baseUrl + this.projectId + "/events:report?key=" + this.apiKey;
119+
StackdriverErrorReporter.prototype.sendErrorPayload = function(payload, callback) {
120+
var url = baseAPIUrl + this.projectId + "/events:report?key=" + this.apiKey;
121121

122122
var xhr = new XMLHttpRequest();
123123
xhr.open('POST', url, true);

test/test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function expectRequestWithMessage(message) {
2929
beforeEach(function() {
3030
window.onerror= function(){};
3131

32-
errorHandler = new StackdriverErrorReporting();
32+
errorHandler = new StackdriverErrorReporter();
3333

3434
xhr = sinon.useFakeXMLHttpRequest();
3535
xhr.useFilters = true;
@@ -51,29 +51,29 @@ beforeEach(function() {
5151

5252
describe('Initialization', function () {
5353
it('should have default service', function () {
54-
errorHandler.init({key:'key', projectId:'projectId'});
54+
errorHandler.start({key:'key', projectId:'projectId'});
5555
expect(errorHandler.serviceContext.service).to.equal('web');
5656
});
5757

5858
it('should by default report uncaught exceptions', function () {
59-
errorHandler.init({key:'key', projectId:'projectId'});
59+
errorHandler.start({key:'key', projectId:'projectId'});
6060
expect(errorHandler.reportUncaughtExceptions).to.equal(true);
6161
});
6262

6363
it('should fail if no API key', function () {
64-
expect(function() {errorHandler.init({projectId:'projectId'});}).to.throw(Error, /API/);
64+
expect(function() {errorHandler.start({projectId:'projectId'});}).to.throw(Error, /API/);
6565
});
6666

6767
it('should fail if no project ID', function () {
68-
expect(function() {errorHandler.init({key:'key'});}).to.throw(Error, /project/);
68+
expect(function() {errorHandler.start({key:'key'});}).to.throw(Error, /project/);
6969
});
7070

7171
});
7272

7373
describe('Disabling', function () {
7474

7575
it('should not report errors if disabled', function (done) {
76-
errorHandler.init({key:'key', projectId:'projectId', disabled: true});
76+
errorHandler.start({key:'key', projectId:'projectId', disabled: true});
7777
errorHandler.report('do not report', function() {
7878
expect(requests.length).to.equal(0);
7979
done();
@@ -84,7 +84,7 @@ describe('Disabling', function () {
8484

8585
describe('Reporting errors', function () {
8686
beforeEach(function() {
87-
errorHandler.init({key:'key', projectId:'projectId'});
87+
errorHandler.start({key:'key', projectId:'projectId'});
8888
});
8989

9090
it('should report error messages with location', function (done) {
@@ -114,7 +114,7 @@ describe('Reporting errors', function () {
114114
describe('Unhandled exceptions', function () {
115115

116116
it('should be reported by default', function (done) {
117-
errorHandler.init({key:'key', projectId:'projectId'});
117+
errorHandler.start({key:'key', projectId:'projectId'});
118118

119119
var message = 'custom message';
120120
try {
@@ -133,7 +133,7 @@ describe('Unhandled exceptions', function () {
133133
var originalOnErrorCalled = false;
134134
window.onerror = function(){ originalOnErrorCalled = true;};
135135

136-
errorHandler.init({key:'key', projectId:'projectId'});
136+
errorHandler.start({key:'key', projectId:'projectId'});
137137

138138
var message = 'custom message';
139139
try {

0 commit comments

Comments
 (0)