Skip to content

Commit

Permalink
ref(ios): Move breadcrumbs conversion to pure obj-c to ensure compati…
Browse files Browse the repository at this point in the history
…bility with the swift implementation (#3854)
  • Loading branch information
krystofwoldrich authored Jun 5, 2024
1 parent 758e3b7 commit 0019f64
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
330F308C2C0F3840002A0D4E /* RNSentryBreadcrumbTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 330F308B2C0F3840002A0D4E /* RNSentryBreadcrumbTests.m */; };
33958C692BFCF12600AD1FB6 /* RNSentryOnDrawReporterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 33958C682BFCF12600AD1FB6 /* RNSentryOnDrawReporterTests.m */; };
33AFDFED2B8D14B300AAB120 /* RNSentryFramesTrackerListenerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 33AFDFEC2B8D14B300AAB120 /* RNSentryFramesTrackerListenerTests.m */; };
33AFDFF12B8D15E500AAB120 /* RNSentryDependencyContainerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 33AFDFF02B8D15E500AAB120 /* RNSentryDependencyContainerTests.m */; };
Expand All @@ -16,6 +17,8 @@

/* Begin PBXFileReference section */
1482D5685A340AB93348A43D /* Pods-RNSentryCocoaTesterTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNSentryCocoaTesterTests.release.xcconfig"; path = "Target Support Files/Pods-RNSentryCocoaTesterTests/Pods-RNSentryCocoaTesterTests.release.xcconfig"; sourceTree = "<group>"; };
330F308B2C0F3840002A0D4E /* RNSentryBreadcrumbTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNSentryBreadcrumbTests.m; sourceTree = "<group>"; };
330F308D2C0F385A002A0D4E /* RNSentryBreadcrumb.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RNSentryBreadcrumb.h; path = ../ios/RNSentryBreadcrumb.h; sourceTree = "<group>"; };
3360898D29524164007C7730 /* RNSentryCocoaTesterTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RNSentryCocoaTesterTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
338739072A7D7D2800950DDD /* RNSentryTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNSentryTests.h; sourceTree = "<group>"; };
33958C672BFCEF5A00AD1FB6 /* RNSentryOnDrawReporter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RNSentryOnDrawReporter.h; path = ../ios/RNSentryOnDrawReporter.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -80,13 +83,15 @@
33AFDFF02B8D15E500AAB120 /* RNSentryDependencyContainerTests.m */,
33AFDFF22B8D15F600AAB120 /* RNSentryDependencyContainerTests.h */,
33958C682BFCF12600AD1FB6 /* RNSentryOnDrawReporterTests.m */,
330F308B2C0F3840002A0D4E /* RNSentryBreadcrumbTests.m */,
);
path = RNSentryCocoaTesterTests;
sourceTree = "<group>";
};
33AFE0122B8F319000AAB120 /* RNSentry */ = {
isa = PBXGroup;
children = (
330F308D2C0F385A002A0D4E /* RNSentryBreadcrumb.h */,
33958C672BFCEF5A00AD1FB6 /* RNSentryOnDrawReporter.h */,
33AFE0132B8F31AF00AAB120 /* RNSentryDependencyContainer.h */,
);
Expand Down Expand Up @@ -204,6 +209,7 @@
33AFDFF12B8D15E500AAB120 /* RNSentryDependencyContainerTests.m in Sources */,
33F58AD02977037D008F60EA /* RNSentryTests.mm in Sources */,
33958C692BFCF12600AD1FB6 /* RNSentryOnDrawReporterTests.m in Sources */,
330F308C2C0F3840002A0D4E /* RNSentryBreadcrumbTests.m in Sources */,
33AFDFED2B8D14B300AAB120 /* RNSentryFramesTrackerListenerTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#import <XCTest/XCTest.h>
#import "RNSentryBreadcrumb.h"
@import Sentry;

@interface RNSentryBreadcrumbTests : XCTestCase

@end

@implementation RNSentryBreadcrumbTests

- (void)testGeneratesSentryBreadcrumbFromNSDictionary
{
SentryBreadcrumb* actualCrumb = [RNSentryBreadcrumb from:@{
@"level": @"error",
@"category": @"testCategory",
@"type": @"testType",
@"message": @"testMessage",
@"data": @{
@"test": @"data"
}
}];

XCTAssertEqual(actualCrumb.level, kSentryLevelError);
XCTAssertEqual(actualCrumb.category, @"testCategory");
XCTAssertEqual(actualCrumb.type, @"testType");
XCTAssertEqual(actualCrumb.message, @"testMessage");
XCTAssertTrue([actualCrumb.data isKindOfClass:[NSDictionary class]]);
XCTAssertEqual(actualCrumb.data[@"test"], @"data");
}

- (void)testUsesInfoAsDefaultSentryLevel
{
SentryBreadcrumb* actualCrumb = [RNSentryBreadcrumb from:@{
@"message": @"testMessage",
}];

XCTAssertEqual(actualCrumb.level, kSentryLevelInfo);
}

@end
28 changes: 2 additions & 26 deletions ios/RNSentry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import <Sentry/SentryFormatter.h>
#import <Sentry/SentryAppStartMeasurement.h>
#import "RNSentryId.h"
#import "RNSentryBreadcrumb.h"

// This guard prevents importing Hermes in JSC apps
#if SENTRY_PROFILING_ENABLED
Expand Down Expand Up @@ -557,32 +558,7 @@ - (NSDictionary*) fetchNativeStackFramesBy: (NSArray<NSNumber*>*)instructionsAdd
RCT_EXPORT_METHOD(addBreadcrumb:(NSDictionary *)breadcrumb)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
SentryBreadcrumb* breadcrumbInstance = [[SentryBreadcrumb alloc] init];

NSString * levelString = breadcrumb[@"level"];
SentryLevel sentryLevel;
if ([levelString isEqualToString:@"fatal"]) {
sentryLevel = kSentryLevelFatal;
} else if ([levelString isEqualToString:@"warning"]) {
sentryLevel = kSentryLevelWarning;
} else if ([levelString isEqualToString:@"error"]) {
sentryLevel = kSentryLevelError;
} else if ([levelString isEqualToString:@"debug"]) {
sentryLevel = kSentryLevelDebug;
} else {
sentryLevel = kSentryLevelInfo;
}
[breadcrumbInstance setLevel:sentryLevel];

[breadcrumbInstance setCategory:breadcrumb[@"category"]];

[breadcrumbInstance setType:breadcrumb[@"type"]];

[breadcrumbInstance setMessage:breadcrumb[@"message"]];

[breadcrumbInstance setData:breadcrumb[@"data"]];

[scope addBreadcrumb:breadcrumbInstance];
[scope addBreadcrumb:[RNSentryBreadcrumb from:breadcrumb]];
}];
}

Expand Down
9 changes: 9 additions & 0 deletions ios/RNSentryBreadcrumb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <Foundation/Foundation.h>

@class SentryBreadcrumb;

@interface RNSentryBreadcrumb : NSObject

+ (SentryBreadcrumb *)from: (NSDictionary *) dict;

@end
33 changes: 33 additions & 0 deletions ios/RNSentryBreadcrumb.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#import "RNSentryBreadcrumb.h"
@import Sentry;

@implementation RNSentryBreadcrumb

+(SentryBreadcrumb*) from: (NSDictionary *) dict
{
SentryBreadcrumb* crumb = [[SentryBreadcrumb alloc] init];

NSString * levelString = dict[@"level"];
SentryLevel sentryLevel;
if ([levelString isEqualToString:@"fatal"]) {
sentryLevel = kSentryLevelFatal;
} else if ([levelString isEqualToString:@"warning"]) {
sentryLevel = kSentryLevelWarning;
} else if ([levelString isEqualToString:@"error"]) {
sentryLevel = kSentryLevelError;
} else if ([levelString isEqualToString:@"debug"]) {
sentryLevel = kSentryLevelDebug;
} else {
sentryLevel = kSentryLevelInfo;
}

[crumb setLevel:sentryLevel];
[crumb setCategory:dict[@"category"]];
[crumb setType:dict[@"type"]];
[crumb setMessage:dict[@"message"]];
[crumb setData:dict[@"data"]];

return crumb;
}

@end

0 comments on commit 0019f64

Please sign in to comment.