Skip to content

Commit

Permalink
ref: Serialization use logging macros (#4345)
Browse files Browse the repository at this point in the history
Use the logging macros everywhere for consistency.
  • Loading branch information
philipphofmann authored Sep 18, 2024
1 parent 29567a9 commit 5269d20
Showing 1 changed file with 37 additions and 64 deletions.
101 changes: 37 additions & 64 deletions Sources/Sentry/SentrySerialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,33 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
error:&error];
if (nil != error) {
SENTRY_LOG_ERROR(@"Failed to parse envelope header %@", error);
} else {
SentryId *eventId = nil;
NSString *eventIdAsString = headerDictionary[@"event_id"];
if (nil != eventIdAsString) {
eventId = [[SentryId alloc] initWithUUIDString:eventIdAsString];
}

SentrySdkInfo *sdkInfo = nil;
if (nil != headerDictionary[@"sdk"]) {
sdkInfo = [[SentrySdkInfo alloc] initWithDict:headerDictionary];
}

SentryTraceContext *traceContext = nil;
if (nil != headerDictionary[@"trace"]) {
traceContext =
[[SentryTraceContext alloc] initWithDict:headerDictionary[@"trace"]];
}

envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:eventId
sdkInfo:sdkInfo
traceContext:traceContext];

if (headerDictionary[@"sent_at"] != nil) {
envelopeHeader.sentAt = sentry_fromIso8601String(headerDictionary[@"sent_at"]);
}
break;
}

SentryId *eventId = nil;
NSString *eventIdAsString = headerDictionary[@"event_id"];
if (nil != eventIdAsString) {
eventId = [[SentryId alloc] initWithUUIDString:eventIdAsString];
}

SentrySdkInfo *sdkInfo = nil;
if (nil != headerDictionary[@"sdk"]) {
sdkInfo = [[SentrySdkInfo alloc] initWithDict:headerDictionary];
}

SentryTraceContext *traceContext = nil;
if (nil != headerDictionary[@"trace"]) {
traceContext = [[SentryTraceContext alloc] initWithDict:headerDictionary[@"trace"]];
}

envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:eventId
sdkInfo:sdkInfo
traceContext:traceContext];

if (headerDictionary[@"sent_at"] != nil) {
envelopeHeader.sentAt = sentry_fromIso8601String(headerDictionary[@"sent_at"]);
}

break;
}
}
Expand All @@ -135,7 +136,6 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
return nil;
}

NSAssert(envelopeHeaderIndex > 0, @"EnvelopeHeader was parsed, its index is expected.");
if (envelopeHeaderIndex == 0) {
SENTRY_LOG_ERROR(@"EnvelopeHeader was parsed, its index is expected.");
return nil;
Expand All @@ -155,38 +155,27 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
#ifdef DEBUG
NSString *itemHeaderString = [[NSString alloc] initWithData:itemHeaderData
encoding:NSUTF8StringEncoding];
[SentryLog
logWithMessage:[NSString stringWithFormat:@"Item Header %@", itemHeaderString]
andLevel:kSentryLevelDebug];
SENTRY_LOG_DEBUG(@"Item Header %@", itemHeaderString);
#endif
NSError *error = nil;
NSDictionary *headerDictionary = [NSJSONSerialization JSONObjectWithData:itemHeaderData
options:0
error:&error];
if (nil != error) {
[SentryLog
logWithMessage:[NSString
stringWithFormat:@"Failed to parse envelope item header %@",
error]
andLevel:kSentryLevelError];
SENTRY_LOG_ERROR(@"Failed to parse envelope item header %@", error);
return nil;
}
NSString *_Nullable type = [headerDictionary valueForKey:@"type"];
if (nil == type) {
[SentryLog
logWithMessage:[NSString stringWithFormat:@"Envelope item type is required."]
andLevel:kSentryLevelError];
SENTRY_LOG_ERROR(@"Envelope item type is required.");
break;
}
NSNumber *bodyLengthNumber = [headerDictionary valueForKey:@"length"];
NSUInteger bodyLength = [bodyLengthNumber unsignedIntegerValue];
if (endOfEnvelope == i && bodyLength != 0) {
[SentryLog
logWithMessage:[NSString
stringWithFormat:@"Envelope item has no data but header "
@"indicates it's length is %d.",
(int)bodyLength]
andLevel:kSentryLevelError];
SENTRY_LOG_ERROR(
@"Envelope item has no data but header indicates it's length is %d.",
(int)bodyLength);
break;
}

Expand Down Expand Up @@ -246,10 +235,7 @@ + (SentrySession *_Nullable)sessionWithData:(NSData *)sessionData
options:0
error:&error];
if (nil != error) {
[SentryLog
logWithMessage:[NSString
stringWithFormat:@"Failed to deserialize session data %@", error]
andLevel:kSentryLevelError];
SENTRY_LOG_ERROR(@"Failed to deserialize session data %@", error);
return nil;
}
SentrySession *session = [[SentrySession alloc] initWithJSONObject:sessionDictionary];
Expand All @@ -260,9 +246,7 @@ + (SentrySession *_Nullable)sessionWithData:(NSData *)sessionData
}

if (nil == session.releaseName || [session.releaseName isEqualToString:@""]) {
[SentryLog
logWithMessage:@"Deserialized session doesn't contain a release name. Dropping it."
andLevel:kSentryLevelError];
SENTRY_LOG_ERROR(@"Deserialized session doesn't contain a release name. Dropping it.");
return nil;
}

Expand All @@ -286,10 +270,7 @@ + (SentryAppState *_Nullable)appStateWithData:(NSData *)data
options:0
error:&error];
if (nil != error) {
[SentryLog
logWithMessage:[NSString
stringWithFormat:@"Failed to deserialize app state data %@", error]
andLevel:kSentryLevelError];
SENTRY_LOG_ERROR(@"Failed to deserialize app state data %@", error);
return nil;
}

Expand All @@ -303,11 +284,7 @@ + (NSDictionary *)deserializeDictionaryFromJsonData:(NSData *)data
options:0
error:&error];
if (nil != error) {
[SentryLog
logWithMessage:[NSString
stringWithFormat:@"Failed to deserialize json item dictionary: %@",
error]
andLevel:kSentryLevelError];
SENTRY_LOG_ERROR(@"Failed to deserialize json item dictionary: %@", error);
}

return eventDictionary;
Expand All @@ -320,11 +297,7 @@ + (SentryLevel)levelFromData:(NSData *)eventEnvelopeItemData
options:0
error:&error];
if (nil != error) {
[SentryLog
logWithMessage:[NSString stringWithFormat:
@"Failed to retrieve event level from envelope item data: %@",
error]
andLevel:kSentryLevelError];
SENTRY_LOG_ERROR(@"Failed to retrieve event level from envelope item data: %@", error);
return kSentryLevelError;
}

Expand Down

0 comments on commit 5269d20

Please sign in to comment.