[ReactNative] console.error shows RedBox with pretty stack trace

This commit is contained in:
Spencer Ahrens
2015-04-22 15:43:11 -07:00
parent c5ea25f7fb
commit e63bfae8f6
5 changed files with 39 additions and 29 deletions

View File

@@ -25,17 +25,11 @@ type Exception = {
message: string;
}
function handleException(e: Exception) {
var stack = parseErrorStack(e);
console.error(
'Err0r: ' +
'\n stack: \n' + stackToString(stack) +
'\n URL: ' + e.sourceURL +
'\n line: ' + e.line +
'\n message: ' + e.message
);
function reportException(e: Exception, stack?: any) {
if (RCTExceptionsManager) {
if (!stack) {
stack = parseErrorStack(e);
}
RCTExceptionsManager.reportUnhandledException(e.message, stack);
if (__DEV__) {
(sourceMapPromise = sourceMapPromise || loadSourceMap())
@@ -50,6 +44,18 @@ function handleException(e: Exception) {
}
}
function handleException(e: Exception) {
var stack = parseErrorStack(e);
console.log(
'Error: ' +
'\n stack: \n' + stackToString(stack) +
'\n URL: ' + e.sourceURL +
'\n line: ' + e.line +
'\n message: ' + e.message
);
reportException(e, stack);
}
function stackToString(stack) {
var maxLength = Math.max.apply(null, stack.map(frame => frame.methodName.length));
return stack.map(frame => stackFrameToString(frame, maxLength)).join('\n');
@@ -71,4 +77,4 @@ function fillSpaces(n) {
return new Array(n + 1).join(' ');
}
module.exports = { handleException };
module.exports = { handleException, reportException };

View File

@@ -77,6 +77,7 @@ function handleErrorWithRedBox(e) {
function setupRedBoxErrorHandler() {
var ErrorUtils = require('ErrorUtils');
ErrorUtils.setGlobalHandler(handleErrorWithRedBox);
GLOBAL.reportException = require('ExceptionsManager').reportException;
}
/**
@@ -134,8 +135,8 @@ function setupGeolocation() {
}
setupDocumentShim();
setupRedBoxErrorHandler();
setupTimers();
setupRedBoxErrorHandler(); // needs to happen after setupTimers
setupAlert();
setupPromise();
setupXHR();