From e443b7379e7f6076b8e02c9e180f3ac737bdfec0 Mon Sep 17 00:00:00 2001 From: Kevin Cooper Date: Tue, 18 Apr 2017 14:06:58 -0700 Subject: [PATCH] More verbose error messages for iOS Summary: Output the reason for the error when failing to load source code. This was a big help when trying to diagnose https://github.com/facebook/react-native/issues/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 https://github.com/facebook/react-native/pull/13561 Differential Revision: D4908501 Pulled By: javache fbshipit-source-id: a316dc70739b917b3cc690309d0ff37a8bb5d412 --- React/Base/RCTBatchedBridge.m | 4 ++-- React/Base/RCTJSCErrorHandling.mm | 5 ++++- React/CxxBridge/RCTCxxBridge.mm | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 2721f1537f065c..cc62cb56643f1b 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -210,7 +210,7 @@ - (void)loadSource:(RCTSourceLoadBlock)_onSourceLoad onProgress:(RCTSourceLoadPr if (error && [self.delegate respondsToSelector:@selector(fallbackSourceURLForBridge:)]) { NSURL *fallbackURL = [self.delegate fallbackSourceURLForBridge:self->_parentBridge]; 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; [RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onProgress:onProgress onComplete:onSourceLoad]; return; @@ -513,7 +513,7 @@ - (void)executeSourceCode:(NSData *)sourceCode } 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(), ^{ [self stopLoadingWithError:loadError]; }); diff --git a/React/Base/RCTJSCErrorHandling.mm b/React/Base/RCTJSCErrorHandling.mm index 21dd74b85f77a0..3030c19b613e2d 100644 --- a/React/Base/RCTJSCErrorHandling.mm +++ b/React/Base/RCTJSCErrorHandling.mm @@ -13,6 +13,7 @@ #import "RCTAssert.h" #import "RCTJSStackFrame.h" +#import "RCTLog.h" NSString *const RCTJSExceptionUnsymbolicatedStackTraceKey = @"RCTJSExceptionUnsymbolicatedStackTraceKey"; @@ -25,7 +26,9 @@ userInfo[NSLocalizedFailureReasonErrorKey] = exceptionMessage; } 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 *const unsymbolicatedFrames = [RCTJSStackFrame stackFramesWithLines:stack]; userInfo[RCTJSStackTraceKey] = unsymbolicatedFrames; } diff --git a/React/CxxBridge/RCTCxxBridge.mm b/React/CxxBridge/RCTCxxBridge.mm index 3605b1ca667f09..3760fe4bae7f63 100644 --- a/React/CxxBridge/RCTCxxBridge.mm +++ b/React/CxxBridge/RCTCxxBridge.mm @@ -356,7 +356,7 @@ - (void)loadSource:(RCTSourceLoadBlock)_onSourceLoad onProgress:(RCTSourceLoadPr if (error && [self.delegate respondsToSelector:@selector(fallbackSourceURLForBridge:)]) { NSURL *fallbackURL = [self.delegate fallbackSourceURLForBridge:self->_parentBridge]; 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; [RCTJavaScriptLoader loadBundleAtURL:self.bundleURL onProgress:onProgress onComplete:onSourceLoad]; return;