Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-26007] iOS: Refactor crash dialog #10022

Merged
merged 17 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions iphone/Classes/GeolocationModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ - (NSNumber *)accuracy
- (void)setAccuracy:(NSNumber *)value
{
ENSURE_UI_THREAD(setAccuracy, value);
ENSURE_TYPE(value, NSNumber);

accuracy = [TiUtils doubleValue:value];
// don't prematurely start it
if (locationManager != nil) {
Expand Down
11 changes: 6 additions & 5 deletions iphone/Classes/TiApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,6 @@ - (void)watchKitExtensionRequestHandler:(id)key withUserInfo:(NSDictionary *)use

- (void)invokeSelector:(SEL)selector withArguments:(NSOrderedSet<id> *)arguments onDelegate:(id)delegate
{
NSArray *keys = [NSStringFromSelector(selector) componentsSeparatedByString:@":"];
NSMutableDictionary *output = [NSMutableDictionary dictionaryWithCapacity:[arguments count]];

NSInteger index = 2; // Index 0 and 1 are reserved for the invocation internals ("self" and "_cmd")
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[delegate methodSignatureForSelector:selector]];
[inv setSelector:selector];
Expand Down Expand Up @@ -1244,8 +1241,12 @@ - (void)showModalError:(NSString *)message
return;
}
ENSURE_UI_THREAD(showModalError, message);
TiErrorController *error = [[[TiErrorController alloc] initWithError:message] autorelease];
[self showModalController:error animated:YES];

TiErrorController *error = [[TiErrorController alloc] initWithError:message];
TiErrorNavigationController *nav = [[[TiErrorNavigationController alloc] initWithRootViewController:error] autorelease];
RELEASE_TO_NIL(error);

[[[self controller] topPresentedController] presentViewController:nav animated:YES completion:nil];
}

- (void)showModalController:(UIViewController *)modalController animated:(BOOL)animated
Expand Down
54 changes: 48 additions & 6 deletions iphone/Classes/TiBindingTiValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,30 @@

// if this looks like a JS Error object, get the message
if ([dict objectForKey:@"line"] != nil && [dict objectForKey:@"column"] != nil) {
TiStringRef prop = TiStringCreateWithUTF8CString("message");
TiValueRef val = TiObjectGetProperty(jsContext, obj, prop, NULL);
id value = TiBindingTiValueToNSObject(jsContext, val);
if (value && ![value isEqual:[NSNull null]]) {
[dict setObject:value forKey:@"message"];
TiStringRef messageKeyRef = TiStringCreateWithUTF8CString("message");
TiStringRef stackKeyRef = TiStringCreateWithUTF8CString("stack");
TiValueRef messageRef = TiObjectGetProperty(jsContext, obj, messageKeyRef, NULL);
TiValueRef stackRef = TiObjectGetProperty(jsContext, obj, stackKeyRef, NULL);

id message = TiBindingTiValueToNSObject(jsContext, messageRef);
if (message && ![message isEqual:[NSNull null]]) {
[dict setObject:message forKey:@"message"];
}
TiStringRelease(messageKeyRef);

id stack = TiBindingTiValueToNSObject(jsContext, stackRef);
if (stack && ![stack isEqual:[NSNull null]]) {

// lets re-format the stack similar to node.js
stack = [stack stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"file://%@", [[NSBundle mainBundle] bundlePath]] withString:@"("];
stack = [stack stringByReplacingOccurrencesOfString:@"\n" withString:@")\n at "];
stack = [stack stringByReplacingOccurrencesOfString:@"])" withString:@"]"];
stack = [stack stringByReplacingOccurrencesOfString:@"@(" withString:@"("];
stack = [NSString stringWithFormat:@" at %@)", stack];

[dict setObject:stack forKey:@"stack"];
}
TiStringRelease(prop);
TiStringRelease(stackKeyRef);
}

TiPropertyNameArrayRelease(props);
Expand Down Expand Up @@ -260,12 +277,16 @@ TiValueRef TiBindingTiValueFromNSObject(TiContextRef jsContext, NSObject *obj)
if ([obj isKindOfClass:[NSDictionary class]]) {
return TiBindingTiValueFromNSDictionary(jsContext, (NSDictionary *)obj);
}

// Handle native errors
if ([obj isKindOfClass:[NSException class]]) {
TiStringRef jsString = TiStringCreateWithCFString((CFStringRef)[(NSException *)obj reason]);
TiValueRef result = TiValueMakeString(jsContext, jsString);
TiStringRelease(jsString);
TiObjectRef excObject = TiObjectMakeError(jsContext, 1, &result, NULL);
NSDictionary *details = [(NSException *)obj userInfo];

// Add "nativeReason" key
NSString *subreason = [details objectForKey:kTiExceptionSubreason];
if (subreason != nil) {
TiStringRef propertyName = TiStringCreateWithUTF8CString("nativeReason");
Expand All @@ -274,6 +295,27 @@ TiValueRef TiBindingTiValueFromNSObject(TiContextRef jsContext, NSObject *obj)
TiStringRelease(propertyName);
TiStringRelease(valueString);
}

// Add "nativeStack" key
NSArray<NSString *> *nativeStack = [(NSException *)obj callStackSymbols];
if (nativeStack != nil) {
NSMutableArray<NSString *> *formattedStackTrace = [[[NSMutableArray alloc] init] autorelease];
NSUInteger exceptionStackTraceLength = [nativeStack count];

// re-size stack trace and format results. Starting at index = 4 to not include the script-error API's
for (NSInteger i = 3; i < (exceptionStackTraceLength >= 20 ? 20 : exceptionStackTraceLength); i++) {
NSString *line = [[nativeStack objectAtIndex:i] stringByReplacingOccurrencesOfString:@" " withString:@""];
[formattedStackTrace addObject:line];
}

TiStringRef propertyName = TiStringCreateWithUTF8CString("nativeStack");
TiStringRef valueString = TiStringCreateWithCFString((CFStringRef)[formattedStackTrace componentsJoinedByString:@"\n"]);
TiObjectSetProperty(jsContext, excObject, propertyName, TiValueMakeString(jsContext, valueString), kTiPropertyAttributeReadOnly, NULL);
TiStringRelease(propertyName);
TiStringRelease(valueString);
}

// Add "nativeLocation" key
NSString *location = [details objectForKey:kTiExceptionLocation];
if (location != nil) {
TiStringRef propertyName = TiStringCreateWithUTF8CString("nativeLocation");
Expand Down
13 changes: 8 additions & 5 deletions iphone/Classes/TiErrorController.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
* Please see the LICENSE included with this distribution for details.
*/

@interface TiErrorNavigationController : UINavigationController

@end

@interface TiErrorController : UIViewController {

NSString *error;
UILabel *disclosureLabel;
UILabel *messageLabel;
UIButton *dismissButton;
UIView *centerView;
UILabel *titleLabel;

UIScrollView *scrollView;
UITextView *messageView;
UIButton *continueButton;
}

- (id)initWithError:(NSString *)error_;
Expand Down