Authored by 孙凯

add try catch review by hongmo

... ... @@ -27,6 +27,8 @@
#import "YH_EventDataFactory.h"
#import "YH_CrashReporter.h"
#import "NSURLSession+AutoTrack.h"
#import "UIImageView+GestureAutoTrack.h"
#import "UILabel+GestureAutoTrack.h"
#define kYHEventReportIgnoredViewController @"YHEventReportQueue"
... ... @@ -133,6 +135,8 @@ static NSArray *kYHEventReportIgnoredViewControllerArray;
[UIActionSheet startTrack];
[UIAlertView startTrack];
[UITabBar startTrack];
[UIImageView startTrack];
[UILabel startTrack];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
... ...
... ... @@ -131,31 +131,45 @@
- (void)yher_start
{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithRequestUrl:[self.currentRequest.URL absoluteString]];
@try {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithRequestUrl:[self.currentRequest.URL absoluteString]];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
[self yher_start];
}
+ (nullable NSData *)yher_sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse * _Nullable * _Nullable)response error:(NSError **)error
{
NSString *hostInHTTPField = [request.allHTTPHeaderFields objectForKey:@"Host"];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
[[YH_EventCollector sharedInstance] timeEventStartWithRequestUrl:[request.URL absoluteString]];
@try {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
[[YH_EventCollector sharedInstance] timeEventStartWithRequestUrl:[request.URL absoluteString]];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
NSData *data = [self yher_sendSynchronousRequest:request returningResponse:response error:error];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && !error && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSURLResponse *res = *response;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)res;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[request.URL absoluteString] HTTPMethod:request.HTTPMethod statusCode:statusCode contentLength:contentLength];
@try {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && !error && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSURLResponse *res = *response;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)res;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[request.URL absoluteString] HTTPMethod:request.HTTPMethod statusCode:statusCode contentLength:contentLength];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
return data;
}
... ... @@ -174,15 +188,20 @@
if (handler) {
handler(response,data,connectionError);
}
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && !connectionError && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[request.URL absoluteString] HTTPMethod:request.HTTPMethod statusCode:statusCode contentLength:contentLength];
@try {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && !connectionError && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[request.URL absoluteString] HTTPMethod:request.HTTPMethod statusCode:statusCode contentLength:contentLength];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
}];
}
... ... @@ -206,14 +225,19 @@
if (self.my_delegate && [self.my_delegate respondsToSelector:@selector(connectionDidFinishLoading:)]) {
[self.my_delegate connectionDidFinishLoading:connection];
}
NSString *hostInHTTPField = [connection.currentRequest.allHTTPHeaderFields objectForKey:@"Host"];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && ([[connection.currentRequest.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)connection.my_response;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[connection.currentRequest.URL absoluteString] HTTPMethod:connection.currentRequest.HTTPMethod statusCode:statusCode contentLength:contentLength];
@try {
NSString *hostInHTTPField = [connection.currentRequest.allHTTPHeaderFields objectForKey:@"Host"];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && ([[connection.currentRequest.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)connection.my_response;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[connection.currentRequest.URL absoluteString] HTTPMethod:connection.currentRequest.HTTPMethod statusCode:statusCode contentLength:contentLength];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
}
... ...
... ... @@ -63,15 +63,20 @@
if (completionHandler) {
completionHandler(data,response,error);
}
NSString *hostInHTTPField = [request.allHTTPHeaderFields objectForKey:@"Host"];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && !error && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
@try {
NSString *hostInHTTPField = [request.allHTTPHeaderFields objectForKey:@"Host"];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[request.URL absoluteString] HTTPMethod:request.HTTPMethod statusCode:statusCode contentLength:contentLength];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && !error && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[request.URL absoluteString] HTTPMethod:request.HTTPMethod statusCode:statusCode contentLength:contentLength];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
}];
}
... ... @@ -87,14 +92,19 @@
if (completionHandler) {
completionHandler(location,response,error);
}
NSString *hostInHTTPField = [request.allHTTPHeaderFields objectForKey:@"Host"];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && !error && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[request.URL absoluteString] HTTPMethod:request.HTTPMethod statusCode:statusCode contentLength:contentLength];
@try {
NSString *hostInHTTPField = [request.allHTTPHeaderFields objectForKey:@"Host"];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable && !error && ([[request.URL host] isEqualToString:YHEventReportHttpHost] || [hostInHTTPField isEqualToString:YHEventReportHttpHost])) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
NSString *contentLength = [NSString stringWithFormat:@"%lld",[[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue]];
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)httpResponse.statusCode];
[[YH_EventCollector sharedInstance] timeEventEndWithRequestUrl:[request.URL absoluteString] HTTPMethod:request.HTTPMethod statusCode:statusCode contentLength:contentLength];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
}];
}
... ... @@ -149,9 +159,13 @@ static inline BOOL yher_addMethod(Class class, SEL selector, Method method) {
- (void)yher_resume
{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
NSURLSessionTask *task = (NSURLSessionTask*)self;
[[YH_EventCollector sharedInstance] timeEventStartWithRequestUrl:[task.currentRequest.URL absoluteString]];
@try {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
NSURLSessionTask *task = (NSURLSessionTask*)self;
[[YH_EventCollector sharedInstance] timeEventStartWithRequestUrl:[task.currentRequest.URL absoluteString]];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
[self yher_resume];
}
... ...
... ... @@ -42,15 +42,23 @@
void yher_collectionViewDidSelectItemAtIndexPath(id self, SEL _cmd, id collectionView, NSIndexPath* indexPath) {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithView:self collectionView:collectionView indexPath:indexPath];
@try {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithView:self collectionView:collectionView indexPath:indexPath];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
SEL selector = NSSelectorFromString(@"yher_collectionViewDidSelectItemAtIndexPath");
((void(*)(id, SEL, id, id))objc_msgSend)(self, selector, collectionView, indexPath);
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventEndWithView:self collectionView:collectionView indexPath:indexPath];
@try {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventEndWithView:self collectionView:collectionView indexPath:indexPath];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
@try {
... ...
... ... @@ -55,17 +55,25 @@
void yher_tableViewDidSelectRowAtIndexPath(id self, SEL _cmd, id tableView, NSIndexPath* indexPath) {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithUITableView:tableView indexPath:indexPath];
@try {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithUITableView:tableView indexPath:indexPath];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
SEL selector = NSSelectorFromString(@"yher_tableViewDidSelectRowAtIndexPath");
((void(*)(id, SEL, id, id))objc_msgSend)(self, selector, tableView, indexPath);
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventEndWithUITableView:tableView indexPath:indexPath];
@try {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventEndWithUITableView:tableView indexPath:indexPath];
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
@try {
//关闭 AutoTrack
... ...