Skip to content

Commit 1e576bb

Browse files
authored
Add exception monitoring handler (#161)
1 parent 95ce3c2 commit 1e576bb

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

Leanplum-SDK/Classes/Constants.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#import "Constants.h"
2626
#import "LeanplumRequest.h"
27+
#import "Utils.h"
2728

2829
@implementation LPConstantsState
2930

@@ -292,6 +293,7 @@ void leanplumIncrementUserCodeBlock(int delta)
292293

293294
void leanplumInternalError(NSException *e)
294295
{
296+
[Utils handleException:e];
295297
if ([e.name isEqualToString:@"Leanplum Error"]) {
296298
@throw e;
297299
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// LPCrashHandler.h
3+
// Leanplum iOS SDK Version 2.0.6
4+
//
5+
// Copyright (c) 2018 Leanplum, Inc. All rights reserved.
6+
//
7+
// Licensed to the Apache Software Foundation (ASF) under one
8+
// or more contributor license agreements. See the NOTICE file
9+
// distributed with this work for additional information
10+
// regarding copyright ownership. The ASF licenses this file
11+
// to you under the Apache License, Version 2.0 (the "License");
12+
// you may not use this file except in compliance with the License.
13+
// You may obtain a copy of the License at
14+
//
15+
// http://www.apache.org/licenses/LICENSE-2.0
16+
//
17+
// Unless required by applicable law or agreed to in writing,
18+
// software distributed under the License is distributed on an
19+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
// KIND, either express or implied. See the License for the
21+
// specific language governing permissions and limitations
22+
// under the License.
23+
24+
#import <Foundation/Foundation.h>
25+
26+
@protocol LPCrashReporting
27+
-(void)reportException:(NSException *)exception;
28+
@end
29+
30+
@interface LPCrashHandler : NSObject
31+
32+
+(instancetype)sharedCrashHandler;
33+
-(void)reportException:(NSException *)exception;
34+
35+
@end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// LPCrashHandler.h
3+
// Leanplum iOS SDK Version 2.0.6
4+
//
5+
// Copyright (c) 2018 Leanplum, Inc. All rights reserved.
6+
//
7+
// Licensed to the Apache Software Foundation (ASF) under one
8+
// or more contributor license agreements. See the NOTICE file
9+
// distributed with this work for additional information
10+
// regarding copyright ownership. The ASF licenses this file
11+
// to you under the Apache License, Version 2.0 (the "License");
12+
// you may not use this file except in compliance with the License.
13+
// You may obtain a copy of the License at
14+
//
15+
// http://www.apache.org/licenses/LICENSE-2.0
16+
//
17+
// Unless required by applicable law or agreed to in writing,
18+
// software distributed under the License is distributed on an
19+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
// KIND, either express or implied. See the License for the
21+
// specific language governing permissions and limitations
22+
// under the License.
23+
24+
#import "LPCrashHandler.h"
25+
26+
@interface LPCrashHandler()
27+
28+
@property (nonatomic, strong) id<LPCrashReporting> crashReporter;
29+
30+
@end
31+
32+
@implementation LPCrashHandler
33+
34+
+(instancetype)sharedCrashHandler
35+
{
36+
static LPCrashHandler *sharedCrashHandler = nil;
37+
@synchronized(self) {
38+
if (!sharedCrashHandler) {
39+
sharedCrashHandler = [[self alloc] init];
40+
}
41+
}
42+
return sharedCrashHandler;
43+
}
44+
45+
-(instancetype)init
46+
{
47+
self = [super init];
48+
if (self) {
49+
[self initializeLeanplumReporter];
50+
}
51+
return self;
52+
}
53+
54+
-(void)initializeLeanplumReporter
55+
{
56+
Class LPCrashReporterClass = NSClassFromString(@"LPCrashReporter");
57+
if (LPCrashReporterClass) {
58+
_crashReporter = [[LPCrashReporterClass alloc] init];
59+
}
60+
}
61+
62+
-(void)reportException:(NSException *)exception
63+
{
64+
if (self.crashReporter) {
65+
[self.crashReporter reportException:exception];
66+
}
67+
}
68+
69+
@end

Leanplum-SDK/Classes/Leanplum.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,10 @@ + (void)startWithUserId:(NSString *)userId
10631063
#endif
10641064
[self maybeRegisterForNotifications];
10651065
LP_END_TRY
1066+
1067+
LP_TRY
1068+
[Utils initExceptionHandling];
1069+
LP_END_TRY
10661070
}
10671071

10681072
// On first run with Leanplum, determine if this app was previously installed without Leanplum.

Leanplum-SDK/Classes/Utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@
5353
*/
5454
+ (NSString *)urlEncodedStringFromString:(NSString *)urlString;
5555

56+
/**
57+
* Initialize exception handling
58+
*/
59+
+ (void)initExceptionHandling;
60+
61+
/**
62+
* Report an exception
63+
*/
64+
+ (void)handleException:(NSException *)exception;
65+
5666
@end

Leanplum-SDK/Classes/Utils.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#import "Utils.h"
2626
#import <CommonCrypto/CommonDigest.h>
27+
#import "LPCrashHandler.h"
2728

2829
@implementation Utils
2930

@@ -81,4 +82,14 @@ +(NSString *)urlEncodedStringFromString:(NSString *)urlString {
8182
allowed];
8283
}
8384

85+
+ (void)initExceptionHandling
86+
{
87+
[LPCrashHandler sharedCrashHandler];
88+
}
89+
90+
+ (void)handleException:(NSException *)exception
91+
{
92+
[[LPCrashHandler sharedCrashHandler] reportException:exception];
93+
}
94+
8495
@end

0 commit comments

Comments
 (0)