mirror of https://github.com/status-im/metro.git
Cleanup console and ErrorUtils
Reviewed By: davidaurelio Differential Revision: D3981005 fbshipit-source-id: 3c95e62177f78785c7f971dd632126dbfb83746b
This commit is contained in:
parent
47a7a9ee80
commit
89d4f26846
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
|
|
||||||
var inspect = (function() {
|
const inspect = (function() {
|
||||||
// Copyright Joyent, Inc. and other Node contributors.
|
// Copyright Joyent, Inc. and other Node contributors.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
@ -356,8 +356,8 @@ var inspect = (function() {
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
var OBJECT_COLUMN_NAME = '(index)';
|
const OBJECT_COLUMN_NAME = '(index)';
|
||||||
var LOG_LEVELS = {
|
const LOG_LEVELS = {
|
||||||
trace: 0,
|
trace: 0,
|
||||||
info: 1,
|
info: 1,
|
||||||
warn: 2,
|
warn: 2,
|
||||||
|
@ -371,7 +371,7 @@ function setupConsole(global) {
|
||||||
|
|
||||||
function getNativeLogFunction(level) {
|
function getNativeLogFunction(level) {
|
||||||
return function() {
|
return function() {
|
||||||
var str;
|
let str;
|
||||||
if (arguments.length === 1 && typeof arguments[0] === 'string') {
|
if (arguments.length === 1 && typeof arguments[0] === 'string') {
|
||||||
str = arguments[0];
|
str = arguments[0];
|
||||||
} else {
|
} else {
|
||||||
|
@ -380,7 +380,7 @@ function setupConsole(global) {
|
||||||
}).join(', ');
|
}).join(', ');
|
||||||
}
|
}
|
||||||
|
|
||||||
var logLevel = level;
|
let logLevel = level;
|
||||||
if (str.slice(0, 9) === 'Warning: ' && logLevel >= LOG_LEVELS.error) {
|
if (str.slice(0, 9) === 'Warning: ' && logLevel >= LOG_LEVELS.error) {
|
||||||
// React warnings use console.error so that a stack trace is shown,
|
// React warnings use console.error so that a stack trace is shown,
|
||||||
// but we don't (currently) want these to show a redbox
|
// but we don't (currently) want these to show a redbox
|
||||||
|
@ -391,7 +391,7 @@ function setupConsole(global) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var repeat = function(element, n) {
|
function repeat(element, n) {
|
||||||
return Array.apply(null, Array(n)).map(function() { return element; });
|
return Array.apply(null, Array(n)).map(function() { return element; });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -431,7 +431,7 @@ function setupConsole(global) {
|
||||||
|
|
||||||
// Join all elements in the row into a single string with | separators
|
// Join all elements in the row into a single string with | separators
|
||||||
// (appends extra spaces to each cell to make separators | alligned)
|
// (appends extra spaces to each cell to make separators | alligned)
|
||||||
var joinRow = function(row, space) {
|
function joinRow(row, space) {
|
||||||
var cells = row.map(function(cell, i) {
|
var cells = row.map(function(cell, i) {
|
||||||
var extraSpaces = repeat(' ', columnWidths[i] - cell.length).join('');
|
var extraSpaces = repeat(' ', columnWidths[i] - cell.length).join('');
|
||||||
return cell + extraSpaces;
|
return cell + extraSpaces;
|
||||||
|
@ -465,7 +465,7 @@ function setupConsole(global) {
|
||||||
Object.defineProperty(global, 'originalConsole', descriptor);
|
Object.defineProperty(global, 'originalConsole', descriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
var console = {
|
global.console = {
|
||||||
error: getNativeLogFunction(LOG_LEVELS.error),
|
error: getNativeLogFunction(LOG_LEVELS.error),
|
||||||
info: getNativeLogFunction(LOG_LEVELS.info),
|
info: getNativeLogFunction(LOG_LEVELS.info),
|
||||||
log: getNativeLogFunction(LOG_LEVELS.info),
|
log: getNativeLogFunction(LOG_LEVELS.info),
|
||||||
|
@ -475,14 +475,6 @@ function setupConsole(global) {
|
||||||
table: consoleTablePolyfill
|
table: consoleTablePolyfill
|
||||||
};
|
};
|
||||||
|
|
||||||
// don't reassign to the original descriptor. breaks on ios7
|
|
||||||
Object.defineProperty(global, 'console', {
|
|
||||||
value: console,
|
|
||||||
configurable: descriptor ? descriptor.configurable : true,
|
|
||||||
enumerable: descriptor ? descriptor.enumerable : true,
|
|
||||||
writable: descriptor ? descriptor.writable : true,
|
|
||||||
});
|
|
||||||
|
|
||||||
// If available, also call the original `console` method since that is
|
// If available, also call the original `console` method since that is
|
||||||
// sometimes useful. Ex: on OS X, this will let you see rich output in
|
// sometimes useful. Ex: on OS X, this will let you see rich output in
|
||||||
// the Safari Web Inspector console.
|
// the Safari Web Inspector console.
|
||||||
|
|
|
@ -13,30 +13,40 @@
|
||||||
* before any of the modules, this ErrorUtils must be defined (and the handler
|
* before any of the modules, this ErrorUtils must be defined (and the handler
|
||||||
* set) globally before requiring anything.
|
* set) globally before requiring anything.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* eslint strict:0 */
|
/* eslint strict:0 */
|
||||||
var ErrorUtils = {
|
const _inGuard = 0;
|
||||||
_inGuard: 0,
|
|
||||||
_globalHandler: null,
|
/**
|
||||||
|
* This is the error handler that is called when we encounter an exception
|
||||||
|
* when loading a module. This will report any errors encountered before
|
||||||
|
* ExceptionsManager is configured.
|
||||||
|
*/
|
||||||
|
const _globalHandler = function onError(e) {
|
||||||
|
throw e;
|
||||||
|
};
|
||||||
|
|
||||||
|
const ErrorUtils = {
|
||||||
setGlobalHandler: function(fun) {
|
setGlobalHandler: function(fun) {
|
||||||
ErrorUtils._globalHandler = fun;
|
_globalHandler = fun;
|
||||||
},
|
},
|
||||||
getGlobalHandler: function() {
|
getGlobalHandler: function() {
|
||||||
return ErrorUtils._globalHandler;
|
return _globalHandler;
|
||||||
},
|
},
|
||||||
reportError: function(error) {
|
reportError: function(error) {
|
||||||
ErrorUtils._globalHandler && ErrorUtils._globalHandler(error);
|
_globalHandler && _globalHandler(error);
|
||||||
},
|
},
|
||||||
reportFatalError: function(error) {
|
reportFatalError: function(error) {
|
||||||
ErrorUtils._globalHandler && ErrorUtils._globalHandler(error, true);
|
_globalHandler && _globalHandler(error, true);
|
||||||
},
|
},
|
||||||
applyWithGuard: function(fun, context, args) {
|
applyWithGuard: function(fun, context, args) {
|
||||||
try {
|
try {
|
||||||
ErrorUtils._inGuard++;
|
_inGuard++;
|
||||||
return fun.apply(context, args);
|
return fun.apply(context, args);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ErrorUtils.reportError(e);
|
ErrorUtils.reportError(e);
|
||||||
} finally {
|
} finally {
|
||||||
ErrorUtils._inGuard--;
|
_inGuard--;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
applyWithGuardIfNeeded: function(fun, context, args) {
|
applyWithGuardIfNeeded: function(fun, context, args) {
|
||||||
|
@ -47,7 +57,7 @@ var ErrorUtils = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
inGuard: function() {
|
inGuard: function() {
|
||||||
return ErrorUtils._inGuard;
|
return _inGuard;
|
||||||
},
|
},
|
||||||
guard: function(fun, name, context) {
|
guard: function(fun, name, context) {
|
||||||
if (typeof fun !== 'function') {
|
if (typeof fun !== 'function') {
|
||||||
|
@ -70,18 +80,5 @@ var ErrorUtils = {
|
||||||
return guarded;
|
return guarded;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
global.ErrorUtils = ErrorUtils;
|
global.ErrorUtils = ErrorUtils;
|
||||||
|
|
||||||
/**
|
|
||||||
* This is the error handler that is called when we encounter an exception
|
|
||||||
* when loading a module. This will report any errors encountered before
|
|
||||||
* ExceptionsManager is configured.
|
|
||||||
*/
|
|
||||||
function setupErrorGuard() {
|
|
||||||
var onError = function(e) {
|
|
||||||
throw e;
|
|
||||||
};
|
|
||||||
global.ErrorUtils.setGlobalHandler(onError);
|
|
||||||
}
|
|
||||||
|
|
||||||
setupErrorGuard();
|
|
||||||
|
|
Loading…
Reference in New Issue