Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: json serialization error reporting #2355

Merged
merged 6 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/Sentry/Public/SentryError.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ typedef NS_ENUM(NSInteger, SentryError) {
};

SENTRY_EXTERN NSError *_Nullable NSErrorFromSentryError(SentryError error, NSString *description);
SENTRY_EXTERN NSError *_Nullable NSErrorFromSentryErrorWithUnderlyingError(
SentryError error, NSString *description, NSError *underlyingError);
SENTRY_EXTERN NSError *_Nullable NSErrorFromSentryErrorWithException(
SentryError error, NSString *description, NSException *exception);

SENTRY_EXTERN NSString *const SentryErrorDomain;

Expand Down
25 changes: 22 additions & 3 deletions Sources/Sentry/SentryError.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,30 @@

NSString *const SentryErrorDomain = @"SentryErrorDomain";

NSError *_Nullable NSErrorFromSentryError(SentryError error, NSString *description)
NSError *_Nullable _SentryError(SentryError error, NSDictionary *userInfo)
{
NSMutableDictionary *userInfo = [NSMutableDictionary new];
[userInfo setValue:description forKey:NSLocalizedDescriptionKey];
return [NSError errorWithDomain:SentryErrorDomain code:error userInfo:userInfo];
}

NSError *_Nullable NSErrorFromSentryErrorWithUnderlyingError(
SentryError error, NSString *description, NSError *underlyingError)
{
return _SentryError(error,
@ { NSLocalizedDescriptionKey : description, NSUnderlyingErrorKey : underlyingError });
}

NSError *_Nullable NSErrorFromSentryErrorWithException(
SentryError error, NSString *description, NSException *exception)
{
return _SentryError(error, @ {
NSLocalizedDescriptionKey :
[NSString stringWithFormat:@"%@ (%@)", description, exception.reason],
});
}

NSError *_Nullable NSErrorFromSentryError(SentryError error, NSString *description)
{
return _SentryError(error, @ { NSLocalizedDescriptionKey : description });
}

NS_ASSUME_NONNULL_END
27 changes: 22 additions & 5 deletions Sources/Sentry/SentrySerialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,33 @@ @implementation SentrySerialization
+ (NSData *_Nullable)dataWithJSONObject:(NSDictionary *)dictionary
error:(NSError *_Nullable *_Nullable)error
{
// We'll do this whether we're handling an exception or library error
#define SENTRY_HANDLE_ERROR(__sentry_error) \
SENTRY_LOG_ERROR(@"Invalid JSON: %@", __sentry_error); \
*error = __sentry_error; \
return nil;

NSData *data = nil;
if ([NSJSONSerialization isValidJSONObject:dictionary] != NO) {

#if defined(DEBUG) || defined(TEST) || defined(TESTCI)
@try {
#else
if ([NSJSONSerialization isValidJSONObject:dictionary]) {
#endif
data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:error];
} else {
SENTRY_LOG_ERROR(@"Invalid JSON.");
#if defined(DEBUG) || defined(TEST) || defined(TESTCI)
} @catch (NSException *exception) {
if (error) {
*error = NSErrorFromSentryError(
kSentryErrorJsonConversionError, @"Event cannot be converted to JSON");
SENTRY_HANDLE_ERROR(NSErrorFromSentryErrorWithException(
kSentryErrorJsonConversionError, @"Event cannot be converted to JSON", exception));
}
}
#else
} else if (error) {
SENTRY_HANDLE_ERROR(NSErrorFromSentryErrorWithUnderlyingError(
kSentryErrorJsonConversionError, @"Event cannot be converted to JSON", *error));
}
#endif

return data;
}
Expand Down