Skip to content
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

add custom window constructor #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 11 additions & 7 deletions stackdriver-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ var StackTrace = require('stacktrace-js');
* URL endpoint of the Stackdriver Error Reporting report API.
*/
var baseAPIUrl = 'https://clouderrorreporting.googleapis.com/v1beta1/projects/';
var globalWindow;

/**
* An Error handler that sends errors to the Stackdriver Error Reporting API.
* @param {Window} [overrideWindowObject] an optional argument in case we would like to use different object to represent the window object
*/
var StackdriverErrorReporter = function() {};
var StackdriverErrorReporter = function(overrideWindowObject) {
globalWindow = overrideWindowObject || (typeof window !== 'undefined' && window);
};

/**
* Initialize the StackdriverErrorReporter object.
Expand Down Expand Up @@ -67,9 +71,9 @@ function registerHandlers(reporter) {
// Register as global error handler if requested
var noop = function() {};
if (reporter.reportUncaughtExceptions) {
var oldErrorHandler = window.onerror || noop;
var oldErrorHandler = globalWindow.onerror || noop;

window.onerror = function(message, source, lineno, colno, error) {
globalWindow.onerror = function(message, source, lineno, colno, error) {
if (error) {
reporter.report(error).catch(noop);
}
Expand All @@ -78,9 +82,9 @@ function registerHandlers(reporter) {
};
}
if (reporter.reportUnhandledPromiseRejections) {
var oldPromiseRejectionHandler = window.onunhandledrejection || noop;
var oldPromiseRejectionHandler = globalWindow.onunhandledrejection || noop;

window.onunhandledrejection = function(promiseRejectionEvent) {
globalWindow.onunhandledrejection = function(promiseRejectionEvent) {
if (promiseRejectionEvent) {
reporter.report(promiseRejectionEvent.reason).catch(noop);
}
Expand Down Expand Up @@ -110,8 +114,8 @@ StackdriverErrorReporter.prototype.report = function(err, options) {
payload.serviceContext = this.serviceContext;
payload.context = this.context;
payload.context.httpRequest = {
userAgent: window.navigator.userAgent,
url: window.location.href,
userAgent: globalWindow.navigator.userAgent,
url: globalWindow.location.href,
};

var firstFrameIndex = 0;
Expand Down
Loading