Merge "Catch and report unhandled rejections"
This commit is contained in:
		@@ -64,6 +64,13 @@
 | 
			
		||||
  const catchErrors = function(opt_context) {
 | 
			
		||||
    const context = opt_context || window;
 | 
			
		||||
    context.onerror = onError.bind(null, context.onerror);
 | 
			
		||||
    context.addEventListener('unhandledrejection', e => {
 | 
			
		||||
      const msg = e.reason.message;
 | 
			
		||||
      const payload = {
 | 
			
		||||
        error: e.reason,
 | 
			
		||||
      };
 | 
			
		||||
      GrReporting.prototype.reporter(ERROR.TYPE, ERROR.CATEGORY, msg, payload);
 | 
			
		||||
    });
 | 
			
		||||
  };
 | 
			
		||||
  catchErrors();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -183,7 +183,12 @@ limitations under the License.
 | 
			
		||||
 | 
			
		||||
      setup(() => {
 | 
			
		||||
        reporter = sandbox.stub(GrReporting.prototype, 'reporter');
 | 
			
		||||
        fakeWindow = {};
 | 
			
		||||
        fakeWindow = {
 | 
			
		||||
          handlers: {},
 | 
			
		||||
          addEventListener(type, handler) {
 | 
			
		||||
            this.handlers[type] = handler;
 | 
			
		||||
          },
 | 
			
		||||
        };
 | 
			
		||||
        sandbox.stub(console, 'error');
 | 
			
		||||
        window.GrReporting._catchErrors(fakeWindow);
 | 
			
		||||
      });
 | 
			
		||||
@@ -204,6 +209,15 @@ limitations under the License.
 | 
			
		||||
      test('prevent default event handler', () => {
 | 
			
		||||
        assert.isTrue(emulateThrow());
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      test('unhandled rejection', () => {
 | 
			
		||||
        fakeWindow.handlers['unhandledrejection']({
 | 
			
		||||
          reason: {
 | 
			
		||||
            message: 'bar',
 | 
			
		||||
          },
 | 
			
		||||
        });
 | 
			
		||||
        assert.isTrue(reporter.calledWith('error', 'exception', 'bar'));
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
</script>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user