[React Native] Actually bring back console.error redboxes with disable flag

This commit is contained in:
Spencer Ahrens 2015-04-28 15:41:33 -07:00
parent 9209c444c7
commit ecf7baebea
2 changed files with 49 additions and 11 deletions

View File

@ -16,6 +16,7 @@ var RCTExceptionsManager = require('NativeModules').ExceptionsManager;
var loadSourceMap = require('loadSourceMap'); var loadSourceMap = require('loadSourceMap');
var parseErrorStack = require('parseErrorStack'); var parseErrorStack = require('parseErrorStack');
var stringifySafe = require('stringifySafe');
var sourceMapPromise; var sourceMapPromise;
@ -25,17 +26,11 @@ type Exception = {
message: string; message: string;
} }
function handleException(e: Exception) { function reportException(e: Exception, stack?: any) {
var stack = parseErrorStack(e);
console.error(
'Err0r: ' +
'\n stack: \n' + stackToString(stack) +
'\n URL: ' + e.sourceURL +
'\n line: ' + e.line +
'\n message: ' + e.message
);
if (RCTExceptionsManager) { if (RCTExceptionsManager) {
if (!stack) {
stack = parseErrorStack(e);
}
RCTExceptionsManager.reportUnhandledException(e.message, stack); RCTExceptionsManager.reportUnhandledException(e.message, stack);
if (__DEV__) { if (__DEV__) {
(sourceMapPromise = sourceMapPromise || loadSourceMap()) (sourceMapPromise = sourceMapPromise || loadSourceMap())
@ -50,6 +45,47 @@ function handleException(e: Exception) {
} }
} }
function handleException(e: Exception) {
var stack = parseErrorStack(e);
var msg =
'Error: ' + e.message +
'\n stack: \n' + stackToString(stack) +
'\n URL: ' + e.sourceURL +
'\n line: ' + e.line +
'\n message: ' + e.message;
if (console.errorOriginal) {
console.errorOriginal(msg);
} else {
console.error(msg);
}
reportException(e, stack);
}
/**
* Shows a redbox with stacktrace for all console.error messages. Disable by
* setting `console.reportErrorsAsExceptions = false;` in your app.
*/
function installConsoleErrorReporter() {
if (console.reportException) {
return; // already installed
}
console.reportException = reportException;
console.errorOriginal = console.error.bind(console);
console.error = function reactConsoleError() {
console.errorOriginal.apply(null, arguments);
if (!console.reportErrorsAsExceptions) {
return;
}
var str = Array.prototype.map.call(arguments, stringifySafe).join(', ');
var error: any = new Error('console.error: ' + str);
error.framesToPop = 1;
reportException(error);
};
if (console.reportErrorsAsExceptions === undefined) {
console.reportErrorsAsExceptions = true; // Individual apps can disable this
}
}
function stackToString(stack) { function stackToString(stack) {
var maxLength = Math.max.apply(null, stack.map(frame => frame.methodName.length)); var maxLength = Math.max.apply(null, stack.map(frame => frame.methodName.length));
return stack.map(frame => stackFrameToString(frame, maxLength)).join('\n'); return stack.map(frame => stackFrameToString(frame, maxLength)).join('\n');
@ -71,4 +107,4 @@ function fillSpaces(n) {
return new Array(n + 1).join(' '); return new Array(n + 1).join(' ');
} }
module.exports = { handleException }; module.exports = { handleException, installConsoleErrorReporter };

View File

@ -11,6 +11,8 @@
*/ */
'use strict'; 'use strict';
require('ExceptionsManager').installConsoleErrorReporter();
var React = require('React'); var React = require('React');
var StyleSheet = require('StyleSheet'); var StyleSheet = require('StyleSheet');
var View = require('View'); var View = require('View');