amical/packages/native-helpers/swift-helper/Sources/ObjCExceptionCatcher/ObjCExceptionCatcher.m
2026-01-08 15:44:34 +05:30

34 lines
1 KiB
Objective-C

#import "ObjCExceptionCatcher.h"
@interface ObjCExceptionInfo ()
@property (nonatomic, readwrite) NSString *name;
@property (nonatomic, readwrite) NSString *reason;
@property (nonatomic, readwrite, nullable) NSArray<NSString *> *callStackSymbols;
@end
@implementation ObjCExceptionInfo
@end
@implementation ObjCExceptionCatcher
+ (nullable id)catchException:(id _Nullable (^)(void))block
exceptionInfo:(ObjCExceptionInfo * _Nullable * _Nullable)exceptionInfo {
@try {
return block();
}
@catch (NSException *exception) {
NSLog(@"[ObjCExceptionCatcher] Caught NSException: %@ - %@",
exception.name, exception.reason);
if (exceptionInfo) {
ObjCExceptionInfo *info = [[ObjCExceptionInfo alloc] init];
info.name = exception.name ?: @"Unknown";
info.reason = exception.reason ?: @"Unknown reason";
info.callStackSymbols = exception.callStackSymbols;
*exceptionInfo = info;
}
return nil;
}
}
@end