Skip to content

Commit b212850

Browse files
feat: Add synchronous storeCrashReport method to RaygunClient
- Expose a public storeCrashReport: method that writes a RaygunMessage to disk synchronously - Applies groupingKeyProvider and beforeSendMessage before storing - Stored reports are sent on next launch via sendAllStoredCrashReports - Intended for use in unhandled exception hooks where async sends would not complete before process termination Amp-Thread-ID: https://ampcode.com/threads/T-019c9e51-098e-704c-b1bb-7cd5b9576955 Co-authored-by: Amp <amp@ampcode.com>
1 parent 03806a2 commit b212850

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Sources/Raygun/RaygunClient.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,42 @@ - (void)sendMessage:(RaygunMessage *)message {
353353
}
354354
}
355355

356+
- (BOOL)storeCrashReport:(RaygunMessage *)message {
357+
if (message == nil) {
358+
[RaygunLogger logWarning:@"Failed to store crash report - Message object cannot be nil"];
359+
return NO;
360+
}
361+
362+
// Call groupingKeyProvider if it's set
363+
if (self.groupingKeyProvider != nil && message.details != nil) {
364+
NSString *groupingKey = self.groupingKeyProvider(message.details);
365+
366+
if (groupingKey != nil && ![groupingKey isEqualToString:@""]) {
367+
message.details.groupingKey = groupingKey;
368+
[RaygunLogger logDebug:@"Applied custom grouping key from provider: %@", groupingKey];
369+
}
370+
}
371+
372+
BOOL send = YES;
373+
if (_beforeSendMessage != nil) {
374+
send = _beforeSendMessage(message);
375+
}
376+
377+
if (!send) {
378+
[RaygunLogger logDebug:@"Crash report was discarded by beforeSendMessage"];
379+
return NO;
380+
}
381+
382+
NSString *path = [_fileManager storeCrashReport:message withMaxReportsStored:self.maxReportsStoredOnDevice];
383+
if (path) {
384+
[RaygunLogger logDebug:@"Stored crash report to %@", path];
385+
return YES;
386+
}
387+
388+
[RaygunLogger logError:@"Failed to store crash report to disk"];
389+
return NO;
390+
}
391+
356392
- (NSError *)getInnerError:(NSError *)error {
357393
NSError *innerErrror = error.userInfo[NSUnderlyingErrorKey];
358394
if (innerErrror) {

Sources/public/RaygunClient.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ NS_SWIFT_NAME(send(error:tags:customData:));
181181
- (void)sendMessage:(RaygunMessage *)message
182182
NS_SWIFT_NAME(send(message:));
183183

184+
/*
185+
* Synchronously store a crash report to disk.
186+
* The report will be sent to Raygun on the next app launch when enableCrashReporting is called.
187+
* This is intended for use in contexts where the process is about to terminate (e.g. unhandled exception hooks)
188+
* and asynchronous network requests would not complete.
189+
*
190+
* @param message The crash report to be stored
191+
*
192+
* @return YES if the report was successfully stored, NO otherwise.
193+
*/
194+
- (BOOL)storeCrashReport:(RaygunMessage *)message
195+
NS_SWIFT_NAME(store(crashReport:));
196+
184197
/*
185198
* Manually record a breadcrumb that will be included in the next crash report.
186199
*

0 commit comments

Comments
 (0)