Skip to content

Commit

Permalink
More verbose error messages for iOS
Browse files Browse the repository at this point in the history
Summary:
Output the reason for the error when failing to load source code. This was a big help when trying to diagnose #13299.

~~Unfortunately there still seems to be no way to get the offending line number (because `loadError.userInfo[RCTJSStackTraceKey]` is empty), but this is good enough.~~

Before:

```
[warn][tid:com.facebook.react.JavaScript][RCTBatchedBridge.m:510] Failed to execute source code. Unhandled JS Exception: SyntaxError
```

After:

```
[warn][tid:com.facebook.react.JavaScript][RCTJSCErrorHandling.mm:30] Couldn't get stack trace for http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:81886
[warn][tid:com.facebook.react.JavaScript][RCTBatchedBridge.m:510] Failed to execute source code. Unhandled JS Exception: SyntaxError Unexpected keyword 'var'
```
Closes #13561

Differential Revision: D4908501

Pulled By: javache

fbshipit-source-id: a316dc70739b917b3cc690309d0ff37a8bb5d412
  • Loading branch information
cooperka authored and facebook-github-bot committed Apr 18, 2017
1 parent cc4648b commit e443b73
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions React/Base/RCTBatchedBridge.m
Expand Up @@ -210,7 +210,7 @@ - (void)loadSource:(RCTSourceLoadBlock)_onSourceLoad onProgress:(RCTSourceLoadPr
if (error && [self.delegate respondsToSelector:@selector(fallbackSourceURLForBridge:)]) { if (error && [self.delegate respondsToSelector:@selector(fallbackSourceURLForBridge:)]) {
NSURL *fallbackURL = [self.delegate fallbackSourceURLForBridge:self->_parentBridge]; NSURL *fallbackURL = [self.delegate fallbackSourceURLForBridge:self->_parentBridge];
if (fallbackURL && ![fallbackURL isEqual:self.bundleURL]) { if (fallbackURL && ![fallbackURL isEqual:self.bundleURL]) {
RCTLogError(@"Failed to load bundle(%@) with error:(%@)", self.bundleURL, error.localizedDescription); RCTLogError(@"Failed to load bundle(%@) with error:(%@ %@)", self.bundleURL, error.localizedDescription, error.localizedFailureReason);
self.bundleURL = fallbackURL; self.bundleURL = fallbackURL;
[RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onProgress:onProgress onComplete:onSourceLoad]; [RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onProgress:onProgress onComplete:onSourceLoad];
return; return;
Expand Down Expand Up @@ -513,7 +513,7 @@ - (void)executeSourceCode:(NSData *)sourceCode
} }


if (loadError) { if (loadError) {
RCTLogWarn(@"Failed to execute source code: %@", [loadError localizedDescription]); RCTLogWarn(@"Failed to execute source code: %@ %@", [loadError localizedDescription], [loadError localizedFailureReason]);
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[self stopLoadingWithError:loadError]; [self stopLoadingWithError:loadError];
}); });
Expand Down
5 changes: 4 additions & 1 deletion React/Base/RCTJSCErrorHandling.mm
Expand Up @@ -13,6 +13,7 @@


#import "RCTAssert.h" #import "RCTAssert.h"
#import "RCTJSStackFrame.h" #import "RCTJSStackFrame.h"
#import "RCTLog.h"


NSString *const RCTJSExceptionUnsymbolicatedStackTraceKey = @"RCTJSExceptionUnsymbolicatedStackTraceKey"; NSString *const RCTJSExceptionUnsymbolicatedStackTraceKey = @"RCTJSExceptionUnsymbolicatedStackTraceKey";


Expand All @@ -25,7 +26,9 @@
userInfo[NSLocalizedFailureReasonErrorKey] = exceptionMessage; userInfo[NSLocalizedFailureReasonErrorKey] = exceptionMessage;
} }
NSString *const stack = [exception[@"stack"] toString]; NSString *const stack = [exception[@"stack"] toString];
if ([stack length]) { if ([@"undefined" isEqualToString:stack]) {
RCTLogWarn(@"Couldn't get stack trace for %@:%@", exception[@"sourceURL"], exception[@"line"]);
} else if ([stack length]) {
NSArray<RCTJSStackFrame *> *const unsymbolicatedFrames = [RCTJSStackFrame stackFramesWithLines:stack]; NSArray<RCTJSStackFrame *> *const unsymbolicatedFrames = [RCTJSStackFrame stackFramesWithLines:stack];
userInfo[RCTJSStackTraceKey] = unsymbolicatedFrames; userInfo[RCTJSStackTraceKey] = unsymbolicatedFrames;
} }
Expand Down
2 changes: 1 addition & 1 deletion React/CxxBridge/RCTCxxBridge.mm
Expand Up @@ -356,7 +356,7 @@ - (void)loadSource:(RCTSourceLoadBlock)_onSourceLoad onProgress:(RCTSourceLoadPr
if (error && [self.delegate respondsToSelector:@selector(fallbackSourceURLForBridge:)]) { if (error && [self.delegate respondsToSelector:@selector(fallbackSourceURLForBridge:)]) {
NSURL *fallbackURL = [self.delegate fallbackSourceURLForBridge:self->_parentBridge]; NSURL *fallbackURL = [self.delegate fallbackSourceURLForBridge:self->_parentBridge];
if (fallbackURL && ![fallbackURL isEqual:self.bundleURL]) { if (fallbackURL && ![fallbackURL isEqual:self.bundleURL]) {
RCTLogError(@"Failed to load bundle(%@) with error:(%@)", self.bundleURL, error.localizedDescription); RCTLogError(@"Failed to load bundle(%@) with error:(%@ %@)", self.bundleURL, error.localizedDescription, error.localizedFailureReason);
self.bundleURL = fallbackURL; self.bundleURL = fallbackURL;
[RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onProgress:onProgress onComplete:onSourceLoad]; [RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onProgress:onProgress onComplete:onSourceLoad];
return; return;
Expand Down

0 comments on commit e443b73

Please sign in to comment.