123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- goog.provide('goog.debug.ErrorHandler');
- goog.provide('goog.debug.ErrorHandler.ProtectedFunctionError');
- goog.require('goog.Disposable');
- goog.require('goog.asserts');
- goog.require('goog.debug');
- goog.require('goog.debug.EntryPointMonitor');
- goog.require('goog.debug.Error');
- goog.require('goog.debug.Trace');
- goog.debug.ErrorHandler = function(handler) {
- goog.debug.ErrorHandler.base(this, 'constructor');
-
- this.errorHandlerFn_ = handler;
-
- this.wrapErrors_ = true;
-
- this.prefixErrorMessages_ = false;
- };
- goog.inherits(goog.debug.ErrorHandler, goog.Disposable);
- goog.debug.ErrorHandler.prototype.addTracersToProtectedFunctions_ = false;
- goog.debug.ErrorHandler.prototype.setAddTracersToProtectedFunctions = function(
- newVal) {
- this.addTracersToProtectedFunctions_ = newVal;
- };
- goog.debug.ErrorHandler.prototype.wrap = function(fn) {
- return this.protectEntryPoint(goog.asserts.assertFunction(fn));
- };
- goog.debug.ErrorHandler.prototype.unwrap = function(fn) {
- goog.asserts.assertFunction(fn);
- return fn[this.getFunctionIndex_(false)] || fn;
- };
- goog.debug.ErrorHandler.prototype.getStackTraceHolder_ = function(stackTrace) {
- var buffer = [];
- buffer.push('##PE_STACK_START##');
- buffer.push(stackTrace.replace(/(\r\n|\r|\n)/g, '##STACK_BR##'));
- buffer.push('##PE_STACK_END##');
- return buffer.join('');
- };
- goog.debug.ErrorHandler.prototype.getFunctionIndex_ = function(wrapper) {
- return (wrapper ? '__wrapper_' : '__protected_') + goog.getUid(this) + '__';
- };
- goog.debug.ErrorHandler.prototype.protectEntryPoint = function(fn) {
- var protectedFnName = this.getFunctionIndex_(true);
- if (!fn[protectedFnName]) {
- var wrapper = fn[protectedFnName] = this.getProtectedFunction(fn);
- wrapper[this.getFunctionIndex_(false)] = fn;
- }
- return fn[protectedFnName];
- };
- goog.debug.ErrorHandler.prototype.getProtectedFunction = function(fn) {
- var that = this;
- var tracers = this.addTracersToProtectedFunctions_;
- if (tracers) {
- var stackTrace = goog.debug.getStacktraceSimple(15);
- }
- var googDebugErrorHandlerProtectedFunction = function() {
- if (that.isDisposed()) {
- return fn.apply(this, arguments);
- }
- if (tracers) {
- var tracer = goog.debug.Trace.startTracer(
- 'protectedEntryPoint: ' + that.getStackTraceHolder_(stackTrace));
- }
- try {
- return fn.apply(this, arguments);
- } catch (e) {
-
- var MESSAGE_PREFIX =
- goog.debug.ErrorHandler.ProtectedFunctionError.MESSAGE_PREFIX;
- if ((e && typeof e === 'object' && e.message &&
- e.message.indexOf(MESSAGE_PREFIX) == 0) ||
- (typeof e === 'string' && e.indexOf(MESSAGE_PREFIX) == 0)) {
- return;
- }
- that.errorHandlerFn_(e);
- if (!that.wrapErrors_) {
-
- if (that.prefixErrorMessages_) {
- if (typeof e === 'object' && e && 'message' in e) {
- e.message = MESSAGE_PREFIX + e.message;
- } else {
- e = MESSAGE_PREFIX + e;
- }
- }
- if (goog.DEBUG) {
-
-
-
-
-
-
- if (e && e.stack && Error.captureStackTrace &&
- goog.global['console']) {
- goog.global['console']['error'](e.message, e.stack);
- }
- }
-
-
- throw e;
- }
-
- throw new goog.debug.ErrorHandler.ProtectedFunctionError(e);
- } finally {
- if (tracers) {
- goog.debug.Trace.stopTracer(tracer);
- }
- }
- };
- googDebugErrorHandlerProtectedFunction[this.getFunctionIndex_(false)] = fn;
- return googDebugErrorHandlerProtectedFunction;
- };
- goog.debug.ErrorHandler.prototype.protectWindowSetTimeout = function() {
- this.protectWindowFunctionsHelper_('setTimeout');
- };
- goog.debug.ErrorHandler.prototype.protectWindowSetInterval = function() {
- this.protectWindowFunctionsHelper_('setInterval');
- };
- goog.debug.ErrorHandler.prototype.protectWindowRequestAnimationFrame =
- function() {
- var win = goog.getObjectByName('window');
- var fnNames = [
- 'requestAnimationFrame', 'mozRequestAnimationFrame', 'webkitAnimationFrame',
- 'msRequestAnimationFrame'
- ];
- for (var i = 0; i < fnNames.length; i++) {
- var fnName = fnNames[i];
- if (fnNames[i] in win) {
- this.protectWindowFunctionsHelper_(fnName);
- }
- }
- };
- goog.debug.ErrorHandler.prototype.protectWindowFunctionsHelper_ = function(
- fnName) {
- var win = goog.getObjectByName('window');
- var originalFn = win[fnName];
- var that = this;
- win[fnName] = function(fn, time) {
-
-
- if (goog.isString(fn)) {
- fn = goog.partial(goog.globalEval, fn);
- }
- arguments[0] = fn = that.protectEntryPoint(fn);
-
-
-
- if (originalFn.apply) {
- return originalFn.apply(this, arguments);
- } else {
- var callback = fn;
- if (arguments.length > 2) {
- var args = Array.prototype.slice.call(arguments, 2);
- callback = function() { fn.apply(this, args); };
- }
- return originalFn(callback, time);
- }
- };
- win[fnName][this.getFunctionIndex_(false)] = originalFn;
- };
- goog.debug.ErrorHandler.prototype.setWrapErrors = function(wrapErrors) {
- this.wrapErrors_ = wrapErrors;
- };
- goog.debug.ErrorHandler.prototype.setPrefixErrorMessages = function(
- prefixErrorMessages) {
- this.prefixErrorMessages_ = prefixErrorMessages;
- };
- goog.debug.ErrorHandler.prototype.disposeInternal = function() {
-
- var win = goog.getObjectByName('window');
- win.setTimeout = this.unwrap(win.setTimeout);
- win.setInterval = this.unwrap(win.setInterval);
- goog.debug.ErrorHandler.base(this, 'disposeInternal');
- };
- goog.debug.ErrorHandler.ProtectedFunctionError = function(cause) {
- var message = goog.debug.ErrorHandler.ProtectedFunctionError.MESSAGE_PREFIX +
- (cause && cause.message ? String(cause.message) : String(cause));
- goog.debug.ErrorHandler.ProtectedFunctionError.base(
- this, 'constructor', message);
-
- this.cause = cause;
- var stack = cause && cause.stack;
- if (stack && goog.isString(stack)) {
- this.stack = (stack);
- }
- };
- goog.inherits(goog.debug.ErrorHandler.ProtectedFunctionError, goog.debug.Error);
- goog.debug.ErrorHandler.ProtectedFunctionError.MESSAGE_PREFIX =
- 'Error in protected function: ';
|