Skip to content

Commit 3a40f58

Browse files
committed
Merge branch 'master' of github.com:GoogleCloudPlatform/stackdriver-errors-js
2 parents f20c93a + 13b210d commit 3a40f58

3 files changed

Lines changed: 69 additions & 9 deletions

File tree

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Here's an introductory video:
1616

1717
1. You need a [Google Cloud project](https://console.cloud.google.com).
1818
1. [Enable the Stackdriver Error Reporting API](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) for your project. We highly recommend to restrict the usage of the key to your website URL only using an 'HTTP referrer' restriction.
19-
1. Create a browser API key:
19+
1. Create a browser API key:
2020

2121
- Follow [these instructions](https://support.google.com/cloud/answer/6158862) to get an API key for your project.
2222
- Recommended: Use **Application restrictions** to restrict this key to your website.
@@ -75,11 +75,12 @@ window.addEventListener('DOMContentLoaded', function() {
7575
errorHandler.start({
7676
key: '<my-api-key>',
7777
projectId: '<my-project-id>',
78-
service: '<my-service>', // (optional)
79-
version: '<my-service-version>', // (optional)
80-
// reportUncaughtExceptions: false // (optional) Set to false to stop reporting unhandled exceptions.
81-
// disabled: true // (optional) Set to true to not report errors when calling report(), this can be used when developping locally.
82-
// context: {user: 'user1'} // (optional) You can set the user later using setUser()
78+
service: '<my-service>', // (optional)
79+
version: '<my-service-version>', // (optional)
80+
// reportUncaughtExceptions: false // (optional) Set to false to stop reporting unhandled exceptions.
81+
// reportUnhandledPromiseRejections: false // (optional) Set to false to stop reporting unhandled promise rejections.
82+
// disabled: true // (optional) Set to true to not report errors when calling report(), this can be used when developping locally.
83+
// context: {user: 'user1'} // (optional) You can set the user later using setUser()
8384
});
8485
});
8586
</script>

stackdriver-errors.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
this.serviceContext.version = config.version;
6161
}
6262
this.reportUncaughtExceptions = config.reportUncaughtExceptions !== false;
63+
this.reportUnhandledPromiseRejections = config.reportUnhandledPromiseRejections !== false;
6364
this.disabled = config.disabled || false;
6465

6566
// Register as global error handler if requested
@@ -75,6 +76,17 @@
7576
return true;
7677
};
7778
}
79+
if(this.reportUnhandledPromiseRejections) {
80+
var oldPromiseRejectionHandler = window.onunhandledrejection || function(){};
81+
82+
window.onunhandledrejection = function(promiseRejectionEvent) {
83+
if(promiseRejectionEvent){
84+
that.report(promiseRejectionEvent.reason);
85+
}
86+
oldPromiseRejectionHandler(promiseRejectionEvent.reason);
87+
return true;
88+
};
89+
}
7890
};
7991

8092
/**

test/test.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var expect = chai.expect;
1717

1818
var errorHandler;
1919
var xhr, requests;
20+
var WAIT_FOR_STACKTRACE_FROMERROR = 15;
2021

2122
/**
2223
* Helper function testing if a given message has been reported
@@ -37,6 +38,7 @@ function throwError(message) {
3738

3839
beforeEach(function() {
3940
window.onerror= function(){};
41+
window.onunhandledrejection = function(){};
4042

4143
errorHandler = new StackdriverErrorReporter();
4244

@@ -69,6 +71,11 @@ describe('Initialization', function () {
6971
expect(errorHandler.reportUncaughtExceptions).to.equal(true);
7072
});
7173

74+
it('should by default report unhandled promise rejections', function () {
75+
errorHandler.start({key:'key', projectId:'projectId'});
76+
expect(errorHandler.reportUnhandledPromiseRejections).to.equal(true);
77+
});
78+
7279
it('should fail if no API key or custom url', function () {
7380
expect(function() {errorHandler.start({projectId:'projectId'});}).to.throw(Error, /API/);
7481
});
@@ -198,7 +205,7 @@ describe('Unhandled exceptions', function () {
198205
setTimeout(function(){
199206
expectRequestWithMessage(message);
200207
done();
201-
}, 10);
208+
}, WAIT_FOR_STACKTRACE_FROMERROR);
202209
}
203210
});
204211

@@ -213,11 +220,51 @@ describe('Unhandled exceptions', function () {
213220
throw new TypeError(message);
214221
} catch(e) {
215222
window.onerror(message, 'test.js', 42, 42, e);
216-
expect(originalOnErrorCalled).to.be.true;
217-
done();
223+
224+
setTimeout(function(){
225+
expect(originalOnErrorCalled).to.be.true;
226+
done();
227+
}, WAIT_FOR_STACKTRACE_FROMERROR);
228+
}
229+
});
230+
});
231+
232+
describe('Unhandled promise rejections', function () {
233+
234+
it('should be reported by default', function (done) {
235+
errorHandler.start({key:'key', projectId:'projectId'});
236+
237+
var message = 'custom promise rejection message';
238+
try {
239+
throwError(message);
240+
} catch(e) {
241+
var promiseRejectionEvent = {reason: e};
242+
243+
window.onunhandledrejection(promiseRejectionEvent);
244+
245+
setTimeout(function(){
246+
expectRequestWithMessage(message);
247+
done();
248+
}, WAIT_FOR_STACKTRACE_FROMERROR);
218249
}
219250
});
220251

252+
it('should keep calling previous promise rejection handler if already present', function (done) {
253+
var originalOnUnhandledRejectionCalled = false;
254+
window.onunhandledrejection = function(){ originalOnUnhandledRejectionCalled = true;};
255+
256+
errorHandler.start({key:'key', projectId:'projectId'});
257+
258+
var message = 'custom promise rejection message';
259+
var promiseRejectionEvent = {reason: new TypeError(message)};
260+
261+
window.onunhandledrejection(promiseRejectionEvent);
262+
263+
setTimeout(function(){
264+
expect(originalOnUnhandledRejectionCalled).to.be.true;
265+
done();
266+
}, WAIT_FOR_STACKTRACE_FROMERROR);
267+
});
221268
});
222269

223270
describe('Setting user', function() {

0 commit comments

Comments
 (0)