Skip to content

Commit

Permalink
chore(8_1_X): update aps-analytics (#11179)
Browse files Browse the repository at this point in the history
* chore: update aps-analytics

* fix(ios): improve uncaught exception stack traces

* build(lint): fix formatting
  • Loading branch information
garymathews authored and lokeshchdhry committed Aug 29, 2019
1 parent 3ec42b5 commit 0148ee6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
Binary file modified android/titanium/lib/aps-analytics.jar
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
*/
@property (nonatomic, readonly) NSString *backtrace;

/**
* Returns the native stack as a static string.
*/
@property (nonatomic, readonly) NSArray<NSString *> *nativeStack;

- (id)initWithMessage:(NSString *)message sourceURL:(NSString *)sourceURL lineNo:(NSInteger)lineNo;
- (id)initWithDictionary:(NSDictionary *)dictionary;

Expand Down
52 changes: 41 additions & 11 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiExceptionHandler.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2015 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2019 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand Down Expand Up @@ -43,16 +43,30 @@ + (TiExceptionHandler *)defaultExceptionHandler

- (void)reportException:(NSException *)exception
{
NSArray *stackTrace = [exception callStackSymbols];
NSString *message = [NSString stringWithFormat:
@"[ERROR] The application has crashed with an uncaught exception '%@'.\nReason:\n%@\nStack trace:\n\n%@\n",
exception.name, exception.reason, [stackTrace componentsJoinedByString:@"\n"]];
NSLog(@"%@", message);
id<TiExceptionHandlerDelegate> currentDelegate = _delegate;
if (currentDelegate == nil) {
currentDelegate = self;
// attempt to generate a script error, which includes JS stack information
JSContext *context = [JSContext currentContext];
JSValue *jsError = [JSValue valueWithNewErrorFromMessage:[exception reason] inContext:context];
@try {
TiScriptError *error = [TiUtils scriptErrorValue:@{
@"message" : [exception reason],
@"sourceURL" : [[jsError valueForProperty:@"sourceURL"] toString],
@"line" : [[jsError valueForProperty:@"line"] toNumber],
@"column" : [[jsError valueForProperty:@"column"] toNumber],
@"stack" : [[jsError valueForProperty:@"stack"] toString],
@"nativeStack" : [exception callStackSymbols]
}];
[self reportScriptError:error];

// cant generate script error, fallback to default behaviour
} @catch (NSException *e) {
id<TiExceptionHandlerDelegate> currentDelegate = _delegate;
if (currentDelegate == nil) {
currentDelegate = self;
}
[currentDelegate handleUncaughtException:exception];
return;
}
[currentDelegate handleUncaughtException:exception];
[prevUncaughtExceptionHandler handleUncaughtException:exception];
}

- (void)reportScriptError:(TiScriptError *)scriptError
Expand All @@ -68,7 +82,10 @@ - (void)reportScriptError:(TiScriptError *)scriptError

- (void)showScriptError:(TiScriptError *)error
{
NSArray<NSString *> *exceptionStackTrace = [NSThread callStackSymbols];
NSArray<NSString *> *exceptionStackTrace = [error valueForKey:@"nativeStack"];
if (exceptionStackTrace == nil) {
exceptionStackTrace = [NSThread callStackSymbols];
}

if (exceptionStackTrace == nil) {
[[TiApp app] showModalError:[error description]];
Expand Down Expand Up @@ -127,6 +144,7 @@ @implementation TiScriptError
@synthesize column = _column;
@synthesize dictionaryValue = _dictionaryValue;
@synthesize backtrace = _backtrace;
@synthesize nativeStack = _nativeStack;

- (id)initWithMessage:(NSString *)message sourceURL:(NSString *)sourceURL lineNo:(NSInteger)lineNo
{
Expand Down Expand Up @@ -155,6 +173,7 @@ - (id)initWithDictionary:(NSDictionary *)dictionary
if (_backtrace == nil) {
_backtrace = [[[dictionary objectForKey:@"stack"] description] copy];
}
_nativeStack = [[dictionary objectForKey:@"nativeStack"] copy];
_dictionaryValue = [dictionary copy];
}
return self;
Expand All @@ -166,6 +185,7 @@ - (void)dealloc
RELEASE_TO_NIL(_sourceURL);
RELEASE_TO_NIL(_backtrace);
RELEASE_TO_NIL(_dictionaryValue);
RELEASE_TO_NIL(_nativeStack);
[super dealloc];
}

Expand Down Expand Up @@ -205,10 +225,14 @@ - (NSString *)detailedDescription
//
// thanks to: http://www.restoroot.com/Blog/2008/10/18/crash-reporter-for-iphone-applications/
//
static BOOL uncaughtException = NO;
static void TiUncaughtExceptionHandler(NSException *exception)
{
static BOOL insideException = NO;

// prevent signal handler repeating exception
uncaughtException = YES;

// prevent recursive exceptions
if (insideException) {
exit(1);
Expand All @@ -217,6 +241,7 @@ static void TiUncaughtExceptionHandler(NSException *exception)
insideException = YES;

[[TiExceptionHandler defaultExceptionHandler] reportException:exception];
[[APSAnalytics sharedInstance] flush];

insideException = NO;
if (prevUncaughtExceptionHandler != NULL) {
Expand All @@ -232,6 +257,11 @@ static void TiUncaughtExceptionHandler(NSException *exception)

static void TiSignalHandler(int code)
{
// already caught exception, no need for signal exception
if (uncaughtException) {
signal(code, SIG_DFL);
return;
}
NSException *exception = [NSException exceptionWithName:@"SIGNAL_ERROR" reason:[NSString stringWithFormat:@"signal error code: %d", code] userInfo:nil];
[[TiExceptionHandler defaultExceptionHandler] reportException:exception];
[[APSAnalytics sharedInstance] flush];
Expand Down
12 changes: 12 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,12 @@ + (UIImage *)imageWithColor:(UIColor *)color

+ (id)stripInvalidJSONPayload:(id)jsonPayload
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
// ISO08601 formatting, same as Javascript stringified new Date()
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

if ([jsonPayload isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *result = [NSMutableDictionary new];
for (NSString *key in [jsonPayload allKeys]) {
Expand All @@ -2125,6 +2131,9 @@ + (id)stripInvalidJSONPayload:(id)jsonPayload
value = [TiUtils stripInvalidJSONPayload:value];
}
if ([self isSupportedFragment:value]) {
if ([value isKindOfClass:[NSDate class]]) {
value = [dateFormatter stringFromDate:value];
}
[result setObject:value forKey:key];
} else {
DebugLog(@"[WARN] Found invalid attribute \"%@\" that cannot be serialized, skipping it ...", key)
Expand All @@ -2138,6 +2147,9 @@ + (id)stripInvalidJSONPayload:(id)jsonPayload
value = [TiUtils stripInvalidJSONPayload:value];
}
if ([self isSupportedFragment:value]) {
if ([value isKindOfClass:[NSDate class]]) {
value = [dateFormatter stringFromDate:value];
}
[result addObject:value];
} else {
DebugLog(@"[WARN] Found invalid value \"%@\" that cannot be serialized, skipping it ...", value);
Expand Down

0 comments on commit 0148ee6

Please sign in to comment.