mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-02 09:21:44 +08:00
Cleanup console and ErrorUtils
Reviewed By: davidaurelio Differential Revision: D3981005 fbshipit-source-id: 3c95e62177f78785c7f971dd632126dbfb83746b
This commit is contained in:
committed by
Facebook Github Bot
parent
08e715bc9c
commit
3137ba993d
@@ -11,41 +11,43 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
let exceptionID = 0;
|
||||
|
||||
/**
|
||||
* Handles the developer-visible aspect of errors and exceptions
|
||||
*/
|
||||
let exceptionID = 0;
|
||||
function reportException(e: Error, isFatal: bool) {
|
||||
const parseErrorStack = require('parseErrorStack');
|
||||
const symbolicateStackTrace = require('symbolicateStackTrace');
|
||||
const RCTExceptionsManager = require('NativeModules').ExceptionsManager;
|
||||
|
||||
const currentExceptionID = ++exceptionID;
|
||||
if (RCTExceptionsManager) {
|
||||
const {ExceptionsManager} = require('NativeModules');
|
||||
if (ExceptionsManager) {
|
||||
const parseErrorStack = require('parseErrorStack');
|
||||
const stack = parseErrorStack(e);
|
||||
const currentExceptionID = ++exceptionID;
|
||||
if (isFatal) {
|
||||
RCTExceptionsManager.reportFatalException(e.message, stack, currentExceptionID);
|
||||
ExceptionsManager.reportFatalException(e.message, stack, currentExceptionID);
|
||||
} else {
|
||||
RCTExceptionsManager.reportSoftException(e.message, stack, currentExceptionID);
|
||||
ExceptionsManager.reportSoftException(e.message, stack, currentExceptionID);
|
||||
}
|
||||
if (__DEV__) {
|
||||
const symbolicateStackTrace = require('symbolicateStackTrace');
|
||||
symbolicateStackTrace(stack).then(
|
||||
(prettyStack) => {
|
||||
if (prettyStack) {
|
||||
RCTExceptionsManager.updateExceptionMessage(e.message, prettyStack, currentExceptionID);
|
||||
ExceptionsManager.updateExceptionMessage(e.message, prettyStack, currentExceptionID);
|
||||
} else {
|
||||
throw new Error('The stack is null');
|
||||
}
|
||||
}
|
||||
).catch(
|
||||
(error) =>
|
||||
console.warn('Unable to symbolicate stack trace: ' + error.message)
|
||||
(error) => console.warn('Unable to symbolicate stack trace: ' + error.message)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
declare var console: typeof console & {
|
||||
_errorOriginal: Function,
|
||||
reportErrorsAsExceptions: boolean,
|
||||
};
|
||||
|
||||
/**
|
||||
* Logs exceptions to the (native) console and displays them
|
||||
*/
|
||||
@@ -57,8 +59,7 @@ function handleException(e: Error, isFatal: boolean) {
|
||||
if (!e.message) {
|
||||
e = new Error(e);
|
||||
}
|
||||
|
||||
if (typeof console._errorOriginal === 'function') {
|
||||
if (console._errorOriginal) {
|
||||
console._errorOriginal(e.message);
|
||||
} else {
|
||||
console.error(e.message);
|
||||
@@ -66,6 +67,29 @@ function handleException(e: Error, isFatal: boolean) {
|
||||
reportException(e, isFatal);
|
||||
}
|
||||
|
||||
function reactConsoleErrorHandler() {
|
||||
console._errorOriginal.apply(console, arguments);
|
||||
if (!console.reportErrorsAsExceptions) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (arguments[0] && arguments[0].stack) {
|
||||
reportException(arguments[0], /* isFatal */ false);
|
||||
} else {
|
||||
const stringifySafe = require('stringifySafe');
|
||||
const str = Array.prototype.map.call(arguments, stringifySafe).join(', ');
|
||||
if (str.slice(0, 10) === '"Warning: ') {
|
||||
// React warnings use console.error so that a stack trace is shown, but
|
||||
// we don't (currently) want these to show a redbox
|
||||
// (Note: Logic duplicated in polyfills/console.js.)
|
||||
return;
|
||||
}
|
||||
const error : any = new Error('console.error: ' + str);
|
||||
error.framesToPop = 1;
|
||||
reportException(error, /* isFatal */ false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a redbox with stacktrace for all console.error messages. Disable by
|
||||
* setting `console.reportErrorsAsExceptions = false;` in your app.
|
||||
@@ -76,33 +100,12 @@ function installConsoleErrorReporter() {
|
||||
return; // already installed
|
||||
}
|
||||
// Flow doesn't like it when you set arbitrary values on a global object
|
||||
(console: any)._errorOriginal = console.error.bind(console);
|
||||
console.error = function reactConsoleError() {
|
||||
// Flow doesn't like it when you set arbitrary values on a global object
|
||||
(console: any)._errorOriginal.apply(null, arguments);
|
||||
if (!console.reportErrorsAsExceptions) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (arguments[0] && arguments[0].stack) {
|
||||
reportException(arguments[0], /* isFatal */ false);
|
||||
} else {
|
||||
const stringifySafe = require('stringifySafe');
|
||||
const str = Array.prototype.map.call(arguments, stringifySafe).join(', ');
|
||||
if (str.slice(0, 10) === '"Warning: ') {
|
||||
// React warnings use console.error so that a stack trace is shown, but
|
||||
// we don't (currently) want these to show a redbox
|
||||
// (Note: Logic duplicated in polyfills/console.js.)
|
||||
return;
|
||||
}
|
||||
const error : any = new Error('console.error: ' + str);
|
||||
error.framesToPop = 1;
|
||||
reportException(error, /* isFatal */ false);
|
||||
}
|
||||
};
|
||||
console._errorOriginal = console.error.bind(console);
|
||||
console.error = reactConsoleErrorHandler;
|
||||
if (console.reportErrorsAsExceptions === undefined) {
|
||||
// Individual apps can disable this
|
||||
// Flow doesn't like it when you set arbitrary values on a global object
|
||||
(console: any).reportErrorsAsExceptions = true; // Individual apps can disable this
|
||||
console.reportErrorsAsExceptions = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user