Skip to content

Commit

Permalink
[ReactNative] differentiate fatal and soft exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sahrens authored and vjeux committed May 13, 2015
1 parent 484f63b commit 81ad810
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
21 changes: 16 additions & 5 deletions Libraries/JavaScriptAppEngine/Initialization/ExceptionsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ type Exception = {
message: string;
}

function reportException(e: Exception, stack?: any) {
function reportException(e: Exception, isFatal: bool, stack?: any) {
if (RCTExceptionsManager) {
if (!stack) {
stack = parseErrorStack(e);
}
RCTExceptionsManager.reportUnhandledException(e.message, stack);
if (!RCTExceptionsManager.reportFatalException ||
!RCTExceptionsManager.reportSoftException) {
// Backwards compatibility - no differentiation
// TODO(#7049989): deprecate reportUnhandledException on Android
RCTExceptionsManager.reportUnhandledException(e.message, stack);
} else {
if (isFatal) {
RCTExceptionsManager.reportFatalException(e.message, stack);
} else {
RCTExceptionsManager.reportSoftException(e.message, stack);
}
}
if (__DEV__) {
(sourceMapPromise = sourceMapPromise || loadSourceMap())
.then(map => {
Expand All @@ -44,7 +55,7 @@ function reportException(e: Exception, stack?: any) {
}
}

function handleException(e: Exception) {
function handleException(e: Exception, isFatal: boolean) {
var stack = parseErrorStack(e);
var msg =
'Error: ' + e.message +
Expand All @@ -57,7 +68,7 @@ function handleException(e: Exception) {
} else {
console.error(msg);
}
reportException(e, stack);
reportException(e, isFatal, stack);
}

/**
Expand All @@ -78,7 +89,7 @@ function installConsoleErrorReporter() {
var str = Array.prototype.map.call(arguments, stringifySafe).join(', ');
var error: any = new Error('console.error: ' + str);
error.framesToPop = 1;
reportException(error);
reportException(error, /* isFatal */ false);
};
if (console.reportErrorsAsExceptions === undefined) {
console.reportErrorsAsExceptions = true; // Individual apps can disable this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ function setupDocumentShim() {
GLOBAL.MutationObserver = undefined;
}

function handleErrorWithRedBox(e) {
function handleErrorWithRedBox(e, isFatal) {
try {
require('ExceptionsManager').handleException(e);
require('ExceptionsManager').handleException(e, isFatal);
} catch(ee) {
console.log('Failed to print error: ', ee.message);
}
Expand Down
12 changes: 10 additions & 2 deletions Libraries/Utilities/MessageQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ var REQUEST_PARAMSS = 2;
var RESPONSE_CBIDS = 3;
var RESPONSE_RETURN_VALUES = 4;

var applyWithErrorReporter = function(fun: Function, context: ?any, args: ?any) {
try {
return fun.apply(context, args);
} catch (e) {
ErrorUtils.reportFatalError(e);
}
};

/**
* Utility to catch errors and prevent having to bind, or execute a bound
* function, while catching errors in a process and returning a resulting
Expand All @@ -97,10 +105,10 @@ var RESPONSE_RETURN_VALUES = 4;
*/
var guardReturn = function(operation, operationArguments, getReturnValue, context) {
if (operation) {
ErrorUtils.applyWithGuard(operation, context, operationArguments);
applyWithErrorReporter(operation, context, operationArguments);
}
if (getReturnValue) {
return ErrorUtils.applyWithGuard(getReturnValue, context, null);
return applyWithErrorReporter(getReturnValue, context, null);
}
return null;
};
Expand Down
4 changes: 3 additions & 1 deletion React/Modules/RCTExceptionsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

@protocol RCTExceptionsManagerDelegate <NSObject>

- (void)unhandledJSExceptionWithMessage:(NSString *)message stack:(NSArray *)stack;
- (void)handleSoftJSExceptionWithMessage:(NSString *)message stack:(NSArray *)stack;
- (void)handleFatalJSExceptionWithMessage:(NSString *)message stack:(NSArray *)stack;
- (void)updateJSExceptionWithMessage:(NSString *)message stack:(NSArray *)stack;

@end

Expand Down
24 changes: 21 additions & 3 deletions React/Modules/RCTExceptionsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,23 @@ - (instancetype)init
return [self initWithDelegate:nil];
}

RCT_EXPORT_METHOD(reportUnhandledException:(NSString *)message
RCT_EXPORT_METHOD(reportSoftException:(NSString *)message
stack:(NSArray *)stack)
{
// TODO(#7070533): report a soft error to the server
if (_delegate) {
[_delegate handleSoftJSExceptionWithMessage:message stack:stack];
return;
}

[[RCTRedBox sharedInstance] showErrorMessage:message withStack:stack];
}

RCT_EXPORT_METHOD(reportFatalException:(NSString *)message
stack:(NSArray *)stack)
{
if (_delegate) {
[_delegate unhandledJSExceptionWithMessage:message stack:stack];
[_delegate handleFatalJSExceptionWithMessage:message stack:stack];
return;
}

Expand Down Expand Up @@ -78,11 +90,17 @@ - (instancetype)init
stack:(NSArray *)stack)
{
if (_delegate) {
[_delegate unhandledJSExceptionWithMessage:message stack:stack];
[_delegate updateJSExceptionWithMessage:message stack:stack];
return;
}

[[RCTRedBox sharedInstance] updateErrorMessage:message withStack:stack];
}

// Deprecated. Use reportFatalException directly instead.
RCT_EXPORT_METHOD(reportUnhandledException:(NSString *)message
stack:(NSArray *)stack)
{
[self reportFatalException:message stack:stack];
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
ErrorUtils._globalHandler = fun;
},
reportError: function(error) {
Error._globalHandler && ErrorUtils._globalHandler(error);
ErrorUtils._globalHandler && ErrorUtils._globalHandler(error);
},
reportFatalError: function(error) {
ErrorUtils._globalHandler && ErrorUtils._globalHandler(error, true);
},
applyWithGuard: function(fun, context, args) {
try {
ErrorUtils._inGuard++;
return fun.apply(context, args);
} catch (e) {
ErrorUtils._globalHandler && ErrorUtils._globalHandler(e);
ErrorUtils.reportError(e);
} finally {
ErrorUtils._inGuard--;
}
Expand Down

0 comments on commit 81ad810

Please sign in to comment.