Skip to content

Commit

Permalink
feat: Add storeEnvelope on SentryClient
Browse files Browse the repository at this point in the history
Hybrid SDKs as react-native need a way to synchronously store an envelope to disk. Previously, this
was possible by exposing SentryFileManager on the SentryClient, but this breaks users of
react-native that use use_frameworks!,
see getsentry/sentry-react-native#1171. A way to fix this would be to expose
SentryFileManager and SentryCurrentDateProvider as proposed in
#835. This has the downside of making classes public,
that shouldn't be public. A better workaround is to expose storeEnvelope on the SentryClient.
  • Loading branch information
philipphofmann committed Nov 9, 2020
1 parent d84d6ca commit ea5db28
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Sources/Sentry/Public/SentryClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ SENTRY_NO_INIT

- (void)captureEnvelope:(SentryEnvelope *)envelope NS_SWIFT_NAME(capture(envelope:));

- (SentryFileManager *)fileManager;
/**
* Needed by hybrid SDKs as react-native to synchronously store an envelope to disk.
*/
- (void)storeEnvelope:(SentryEnvelope *)envelope;

@end

Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ - (void)captureUserFeedback:(SentryUserFeedback *)userFeedback
[self.transport sendUserFeedback:userFeedback];
}

- (void)storeEnvelope:(SentryEnvelope *)envelope
{
[self.fileManager storeEnvelope:envelope];
}

/**
* returns BOOL chance of YES is defined by sampleRate.
* if sample rate isn't within 0.0 - 1.0 it returns YES (like if sampleRate
Expand Down
1 change: 1 addition & 0 deletions Sources/Sentry/SentrySessionCrashedHandler.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "SentrySessionCrashedHandler.h"
#import "SentryClient+Private.h"
#import "SentryClient.h"
#import "SentryCrashAdapter.h"
#import "SentryCurrentDate.h"
Expand Down
1 change: 1 addition & 0 deletions Sources/Sentry/SentrySessionTracker.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "SentrySessionTracker.h"
#import "SentryClient+Private.h"
#import "SentryClient.h"
#import "SentryFileManager.h"
#import "SentryHub.h"
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/include/SentryClient+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface SentryClient (Private)

- (SentryFileManager *)fileManager;

- (SentryId *)captureError:(NSError *)error
withSession:(SentrySession *)session
withScope:(SentryScope *)scope;
Expand Down
11 changes: 10 additions & 1 deletion Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SentryClientTest: XCTestCase {
let message: SentryMessage

let user: User
let fileManager: SentryFileManager

init() {
session = SentrySession(releaseName: "release")
Expand All @@ -35,6 +36,8 @@ class SentryClientTest: XCTestCase {

user = User()
user.email = "[email protected]"

fileManager = try! SentryFileManager(dsn: TestConstants.dsn, andCurrentDateProvider: TestCurrentDateProvider())
}

func getSut(configureOptions: (Options) -> Void = { _ in }) -> Client {
Expand All @@ -45,7 +48,7 @@ class SentryClientTest: XCTestCase {
])
configureOptions(options)

client = Client(options: options, andTransport: transport, andFileManager: try SentryFileManager(dsn: TestConstants.dsn, andCurrentDateProvider: TestCurrentDateProvider()))
client = Client(options: options, andTransport: transport, andFileManager: fileManager)
} catch {
XCTFail("Options could not be created")
}
Expand Down Expand Up @@ -84,6 +87,7 @@ class SentryClientTest: XCTestCase {
override func setUp() {
super.setUp()
fixture = Fixture()
fixture.fileManager.deleteAllEnvelopes()
}

func testCaptureMessage() {
Expand Down Expand Up @@ -606,6 +610,11 @@ class SentryClientTest: XCTestCase {
}
}

func testStoreEnvelope_StoresEnvelopeToDisk() {
fixture.getSut().store(SentryEnvelope(event: Event()))
XCTAssertEqual(1, fixture.fileManager.getAllEnvelopes().count)
}

private func givenEventWithDebugMeta() -> Event {
let event = Event(level: SentryLevel.fatal)
let debugMeta = DebugMeta()
Expand Down

0 comments on commit ea5db28

Please sign in to comment.