From 3fe877a6c23b9e7f0e7ebf769f9aa182ec6ea49f Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Wed, 2 Nov 2022 16:06:43 -0800 Subject: [PATCH 1/6] feat: build errors with exceptions and underling errors --- Sources/Sentry/Public/SentryError.h | 4 ++++ Sources/Sentry/SentryError.m | 28 +++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Sources/Sentry/Public/SentryError.h b/Sources/Sentry/Public/SentryError.h index d1f508f276e..b86007450da 100644 --- a/Sources/Sentry/Public/SentryError.h +++ b/Sources/Sentry/Public/SentryError.h @@ -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; diff --git a/Sources/Sentry/SentryError.m b/Sources/Sentry/SentryError.m index ba1b7b46e97..c1129d3438d 100644 --- a/Sources/Sentry/SentryError.m +++ b/Sources/Sentry/SentryError.m @@ -4,11 +4,33 @@ NSString *const SentryErrorDomain = @"SentryErrorDomain"; +NSError *_Nullable NSErrorFromSentryErrorWithUnderlyingError( + SentryError error, NSString *description, NSError *underlyingError) +{ + return [NSError errorWithDomain:SentryErrorDomain + code:error + userInfo:@ { + NSLocalizedDescriptionKey : description, + NSUnderlyingErrorKey : underlyingError + }]; +} + +NSError *_Nullable NSErrorFromSentryErrorWithException( + SentryError error, NSString *description, NSException *exception) +{ + return [NSError errorWithDomain:SentryErrorDomain + code:error + userInfo:@ { + NSLocalizedDescriptionKey : [NSString + stringWithFormat:@"%@ (%@)", description, exception.reason], + }]; +} + NSError *_Nullable NSErrorFromSentryError(SentryError error, NSString *description) { - NSMutableDictionary *userInfo = [NSMutableDictionary new]; - [userInfo setValue:description forKey:NSLocalizedDescriptionKey]; - return [NSError errorWithDomain:SentryErrorDomain code:error userInfo:userInfo]; + return [NSError errorWithDomain:SentryErrorDomain + code:error + userInfo:@ { NSLocalizedDescriptionKey : description }]; } NS_ASSUME_NONNULL_END From 45cdc1199184317f7344ca3115cca5e19a4d9e6e Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Wed, 2 Nov 2022 16:08:12 -0800 Subject: [PATCH 2/6] ref: json serialization error handling before: - test if input is valid JSON with `-[NSJSONSerialization isValidJSON]` and if not, build our own error. - problem: no hints as to why the JSON is invalid after: - attempt direct serialization with `-[NSJSONSerialization dataWithJSONObject:options:error:]` and use the library error - this can throw an exception in certain failure modes: surround the call with try/catch - use new functions in SentryError to create an NSError from either an NSException or the inout NSError - only compile like this for nonrelease builds so we don't incur the performance hit of throwing an exception (see apple's docs: "For best performance in 64-bit, you should throw exceptions only when absolutely necessary.") --- Sources/Sentry/SentrySerialization.m | 29 +++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/Sources/Sentry/SentrySerialization.m b/Sources/Sentry/SentrySerialization.m index e5216fc068d..c55c1399bd5 100644 --- a/Sources/Sentry/SentrySerialization.m +++ b/Sources/Sentry/SentrySerialization.m @@ -17,16 +17,31 @@ @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 (error) { - *error = NSErrorFromSentryError( - kSentryErrorJsonConversionError, @"Event cannot be converted to JSON"); - } +#if defined(DEBUG) || defined(TEST) || defined(TESTCI) + } @catch (NSException *exception) { + 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; } From b959c355b2b15b79e7aa7e84f84a4e584e249765 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Thu, 3 Nov 2022 11:14:00 -0800 Subject: [PATCH 3/6] refactor to extract common init --- Sources/Sentry/SentryError.m | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Sources/Sentry/SentryError.m b/Sources/Sentry/SentryError.m index c1129d3438d..d00e8a51f5c 100644 --- a/Sources/Sentry/SentryError.m +++ b/Sources/Sentry/SentryError.m @@ -4,33 +4,30 @@ NSString *const SentryErrorDomain = @"SentryErrorDomain"; +NSError *_Nullable _SentryError(SentryError error, NSDictionary *userInfo) +{ + return [NSError errorWithDomain:SentryErrorDomain code:error userInfo:userInfo]; +} + NSError *_Nullable NSErrorFromSentryErrorWithUnderlyingError( SentryError error, NSString *description, NSError *underlyingError) { - return [NSError errorWithDomain:SentryErrorDomain - code:error - userInfo:@ { - NSLocalizedDescriptionKey : description, - NSUnderlyingErrorKey : underlyingError - }]; + return _SentryError(error, + @ { NSLocalizedDescriptionKey : description, NSUnderlyingErrorKey : underlyingError }); } NSError *_Nullable NSErrorFromSentryErrorWithException( SentryError error, NSString *description, NSException *exception) { - return [NSError errorWithDomain:SentryErrorDomain - code:error - userInfo:@ { - NSLocalizedDescriptionKey : [NSString - stringWithFormat:@"%@ (%@)", description, exception.reason], - }]; + return _SentryError(error, @ { + NSLocalizedDescriptionKey : + [NSString stringWithFormat:@"%@ (%@)", description, exception.reason], + }); } NSError *_Nullable NSErrorFromSentryError(SentryError error, NSString *description) { - return [NSError errorWithDomain:SentryErrorDomain - code:error - userInfo:@ { NSLocalizedDescriptionKey : description }]; + return _SentryError(error, @ { NSLocalizedDescriptionKey : description }); } NS_ASSUME_NONNULL_END From dd01a3e6dbbe1847aaf4ec73080e91963b6928e7 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Thu, 3 Nov 2022 14:53:55 -0800 Subject: [PATCH 4/6] check for nil inout error in exception handler --- Sources/Sentry/SentrySerialization.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/Sentry/SentrySerialization.m b/Sources/Sentry/SentrySerialization.m index c55c1399bd5..b20f31431c0 100644 --- a/Sources/Sentry/SentrySerialization.m +++ b/Sources/Sentry/SentrySerialization.m @@ -33,8 +33,10 @@ + (NSData *_Nullable)dataWithJSONObject:(NSDictionary *)dictionary data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:error]; #if defined(DEBUG) || defined(TEST) || defined(TESTCI) } @catch (NSException *exception) { - SENTRY_HANDLE_ERROR(NSErrorFromSentryErrorWithException( - kSentryErrorJsonConversionError, @"Event cannot be converted to JSON", exception)); + if (error) { + SENTRY_HANDLE_ERROR(NSErrorFromSentryErrorWithException( + kSentryErrorJsonConversionError, @"Event cannot be converted to JSON", exception)); + } } #else } else if (error) { From a9e0435e573609b5c9ab794351ef9efa9b2dc099 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Tue, 8 Nov 2022 11:46:14 -0900 Subject: [PATCH 5/6] add tests --- .../Recording/Monitors/SentryCrashMonitor.c | 2 +- .../Monitors/SentryCrashMonitor_AppState.c | 2 +- .../SentryCrashMonitor_CPPException.cpp | 2 +- .../SentryCrashMonitor_MachException.c | 2 +- .../Monitors/SentryCrashMonitor_NSException.m | 2 +- .../Monitors/SentryCrashMonitor_Signal.c | 2 +- .../Monitors/SentryCrashMonitor_System.m | 2 +- .../Monitors/SentryCrashMonitor_User.c | 2 +- Sources/SentryCrash/Recording/SentryCrash.m | 7 ++-- Sources/SentryCrash/Recording/SentryCrashC.c | 2 +- .../Recording/SentryCrashCachedData.c | 2 +- .../SentryCrash/Recording/SentryCrashReport.c | 2 +- .../Recording/Tools/SentryCrashCPU.c | 2 +- .../Recording/Tools/SentryCrashCPU_arm.c | 2 +- .../Recording/Tools/SentryCrashCPU_arm64.c | 2 +- .../Recording/Tools/SentryCrashCPU_x86_32.c | 2 +- .../Recording/Tools/SentryCrashCPU_x86_64.c | 2 +- .../Recording/Tools/SentryCrashDebug.c | 2 +- .../Recording/Tools/SentryCrashFileUtils.c | 2 +- .../Tools/SentryCrashMachineContext.c | 2 +- .../Recording/Tools/SentryCrashMemory.c | 2 +- .../Recording/Tools/SentryCrashObjCApple.h | 4 +-- .../Recording/Tools/SentryCrashStackCursor.c | 2 +- .../Tools/SentryCrashStackCursor_Backtrace.c | 2 +- .../Tools/SentryCrashStackCursor_SelfThread.c | 2 +- .../Recording/Tools/SentryCrashSysCtl.c | 2 +- .../Recording/Tools/SentryCrashThread.c | 2 +- .../Filters/SentryCrashReportFilterBasic.m | 2 +- .../Helper/SentrySerializationTests.swift | 18 +++++++++++ .../Protocol/SentryNSErrorTests.swift | 32 ++++++++++++++++++- 30 files changed, 82 insertions(+), 31 deletions(-) diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor.c b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor.c index f446dd915f4..0981258bc00 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor.c +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor.c @@ -42,7 +42,7 @@ #include -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" // ============================================================================ diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_AppState.c b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_AppState.c index a59dd9c908f..795fd320235 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_AppState.c +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_AppState.c @@ -30,7 +30,7 @@ #include "SentryCrashJSONCodec.h" #include "SentryCrashMonitorContext.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_CPPException.cpp b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_CPPException.cpp index 787909a68f8..6e91d72b4a7 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_CPPException.cpp +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_CPPException.cpp @@ -29,7 +29,7 @@ #include "SentryCrashStackCursor_SelfThread.h" #include "SentryCrashThread.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_MachException.c b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_MachException.c index a18a4b81b0f..7e73a4e8302 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_MachException.c +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_MachException.c @@ -32,7 +32,7 @@ #include "SentryCrashSystemCapabilities.h" #include "SentryCrashThread.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #if SentryCrashCRASH_HAS_MACH diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_NSException.m b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_NSException.m index a6f6168d734..9fb2f68cf76 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_NSException.m +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_NSException.m @@ -31,7 +31,7 @@ #import "SentryCrashStackCursor_Backtrace.h" #include "SentryCrashThread.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #import "SentryCrashLogger.h" // ============================================================================ diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c index adb56a472e8..389aa1a7d8d 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c @@ -32,7 +32,7 @@ #include "SentryCrashStackCursor_MachineContext.h" #include "SentryCrashSystemCapabilities.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #if SentryCrashCRASH_HAS_SIGNAL diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.m b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.m index cf987b09857..10ee347e819 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.m +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_System.m @@ -33,7 +33,7 @@ #import "SentryCrashSysCtl.h" #import "SentryCrashSystemCapabilities.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #import "SentryCrashLogger.h" #import diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_User.c b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_User.c index 76e6b7443f9..f4bcebf7b77 100644 --- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_User.c +++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_User.c @@ -28,7 +28,7 @@ #include "SentryCrashStackCursor_SelfThread.h" #include "SentryCrashThread.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Recording/SentryCrash.m b/Sources/SentryCrash/Recording/SentryCrash.m index 36b51320c8b..7fb39b38a80 100644 --- a/Sources/SentryCrash/Recording/SentryCrash.m +++ b/Sources/SentryCrash/Recording/SentryCrash.m @@ -38,7 +38,7 @@ #import "SentryCrashSystemCapabilities.h" #import -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #import "SentryCrashLogger.h" #include @@ -355,7 +355,10 @@ - (void)deleteReportWithID:(NSNumber *)reportID // ============================================================================ #define SYNTHESIZE_CRASH_STATE_PROPERTY(TYPE, NAME) \ - -(TYPE)NAME { return sentrycrashstate_currentState()->NAME; } + -(TYPE)NAME \ + { \ + return sentrycrashstate_currentState()->NAME; \ + } SYNTHESIZE_CRASH_STATE_PROPERTY(NSTimeInterval, activeDurationSinceLastCrash) SYNTHESIZE_CRASH_STATE_PROPERTY(NSTimeInterval, backgroundDurationSinceLastCrash) diff --git a/Sources/SentryCrash/Recording/SentryCrashC.c b/Sources/SentryCrash/Recording/SentryCrashC.c index ff2066f5bd2..a2a0adc7bed 100644 --- a/Sources/SentryCrash/Recording/SentryCrashC.c +++ b/Sources/SentryCrash/Recording/SentryCrashC.c @@ -40,7 +40,7 @@ #include "SentryCrashString.h" #include "SentryCrashSystemCapabilities.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Recording/SentryCrashCachedData.c b/Sources/SentryCrash/Recording/SentryCrashCachedData.c index fef8d6f3d6e..7e1362f3a33 100644 --- a/Sources/SentryCrash/Recording/SentryCrashCachedData.c +++ b/Sources/SentryCrash/Recording/SentryCrashCachedData.c @@ -24,7 +24,7 @@ #include "SentryCrashCachedData.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Recording/SentryCrashReport.c b/Sources/SentryCrash/Recording/SentryCrashReport.c index 79693f07837..9c705d3ae65 100644 --- a/Sources/SentryCrash/Recording/SentryCrashReport.c +++ b/Sources/SentryCrash/Recording/SentryCrashReport.c @@ -47,7 +47,7 @@ #include "SentryCrashUUIDConversion.h" #include "SentryScopeSyncC.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU.c b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU.c index 106b624a7e6..cd4d147ffc5 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU.c @@ -31,7 +31,7 @@ #include #include -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" const char * diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm.c b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm.c index 0159c08dea1..90bb24551b7 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm.c @@ -32,7 +32,7 @@ # include "SentryCrashMachineContext_Apple.h" # include -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE # include "SentryCrashLogger.h" static const char *g_registerNames[] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm64.c b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm64.c index 877b474d2aa..4cd9939b248 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm64.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm64.c @@ -32,7 +32,7 @@ # include "SentryCrashMachineContext_Apple.h" # include -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE # include "SentryCrashLogger.h" # define KSPACStrippingMask_ARM64e 0x0000000fffffffff diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_x86_32.c b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_x86_32.c index 3addbb4148f..36acd112d89 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_x86_32.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_x86_32.c @@ -32,7 +32,7 @@ # include "SentryCrashMachineContext_Apple.h" # include -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE # include "SentryCrashLogger.h" static const char *g_registerNames[] = { diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_x86_64.c b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_x86_64.c index d4bc29b58d5..27f1e62cba5 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_x86_64.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashCPU_x86_64.c @@ -33,7 +33,7 @@ # include -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE # include "SentryCrashLogger.h" static const char *g_registerNames[] = { "rax", "rbx", "rcx", "rdx", "rdi", "rsi", "rbp", "rsp", diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashDebug.c b/Sources/SentryCrash/Recording/Tools/SentryCrashDebug.c index 928126c3dcc..ef956a4a4c6 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashDebug.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashDebug.c @@ -26,7 +26,7 @@ #include "SentryCrashDebug.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c b/Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c index 79cfc1e4d63..500b7300d29 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c @@ -26,7 +26,7 @@ #include "SentryCrashFileUtils.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashMachineContext.c b/Sources/SentryCrash/Recording/Tools/SentryCrashMachineContext.c index 0020ea20162..7461add0290 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashMachineContext.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashMachineContext.c @@ -33,7 +33,7 @@ #include -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #ifdef __arm64__ diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashMemory.c b/Sources/SentryCrash/Recording/Tools/SentryCrashMemory.c index 3e728572643..5a64d1ce3c2 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashMemory.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashMemory.c @@ -26,7 +26,7 @@ #include "SentryCrashMemory.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include <_types/_uint8_t.h> diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashObjCApple.h b/Sources/SentryCrash/Recording/Tools/SentryCrashObjCApple.h index ca5e331b2ad..441c108dd1f 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashObjCApple.h +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashObjCApple.h @@ -86,7 +86,7 @@ extern "C" { // SentryCrash: The original values wouldn't have worked. The slot shift and // mask were incorrect. # define TAG_COUNT 8 -//#define TAG_SLOT_MASK 0xf +// #define TAG_SLOT_MASK 0xf # define TAG_SLOT_MASK 0x07 # if SUPPORT_MSB_TAGGED_POINTERS @@ -96,7 +96,7 @@ extern "C" { # define TAG_PAYLOAD_RSHIFT 4 # else # define TAG_MASK 1 -//# define TAG_SLOT_SHIFT 0 +// # define TAG_SLOT_SHIFT 0 # define TAG_SLOT_SHIFT 1 # define TAG_PAYLOAD_LSHIFT 0 # define TAG_PAYLOAD_RSHIFT 4 diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor.c b/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor.c index 686bc8f4f74..409ab9ed575 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor.c @@ -27,7 +27,7 @@ #include "SentryCrashSymbolicator.h" #include -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" static bool diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor_Backtrace.c b/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor_Backtrace.c index 2048f8ca9d1..b8b349ccacd 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor_Backtrace.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor_Backtrace.c @@ -25,7 +25,7 @@ #include "SentryCrashStackCursor_Backtrace.h" #include "SentryCrashCPU.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" static bool diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor_SelfThread.c b/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor_SelfThread.c index 38af9638144..3afd3fd7e93 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor_SelfThread.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashStackCursor_SelfThread.c @@ -26,7 +26,7 @@ #include "SentryCrashStackCursor_Backtrace.h" #include -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #define MAX_BACKTRACE_LENGTH \ diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashSysCtl.c b/Sources/SentryCrash/Recording/Tools/SentryCrashSysCtl.c index 9a370916735..c3acc0a05dc 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashSysCtl.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashSysCtl.c @@ -26,7 +26,7 @@ #include "SentryCrashSysCtl.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashThread.c b/Sources/SentryCrash/Recording/Tools/SentryCrashThread.c index 79a3ba4fb73..dffdb23034f 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashThread.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashThread.c @@ -29,7 +29,7 @@ #include "SentryCrashMemory.h" #include "SentryCrashSystemCapabilities.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #include "SentryCrashLogger.h" #include diff --git a/Sources/SentryCrash/Reporting/Filters/SentryCrashReportFilterBasic.m b/Sources/SentryCrash/Reporting/Filters/SentryCrashReportFilterBasic.m index b2ed90c7a97..17c7c66a625 100644 --- a/Sources/SentryCrash/Reporting/Filters/SentryCrashReportFilterBasic.m +++ b/Sources/SentryCrash/Reporting/Filters/SentryCrashReportFilterBasic.m @@ -29,7 +29,7 @@ #import "NSError+SentrySimpleConstructor.h" #import "SentryCrashVarArgs.h" -//#define SentryCrashLogger_LocalLevel TRACE +// #define SentryCrashLogger_LocalLevel TRACE #import "SentryCrashLogger.h" @implementation SentryCrashReportFilterPassthrough diff --git a/Tests/SentryTests/Helper/SentrySerializationTests.swift b/Tests/SentryTests/Helper/SentrySerializationTests.swift index 6dbdea90f07..2e86290e25d 100644 --- a/Tests/SentryTests/Helper/SentrySerializationTests.swift +++ b/Tests/SentryTests/Helper/SentrySerializationTests.swift @@ -7,6 +7,24 @@ class SentrySerializationTests: XCTestCase { static var traceContext = SentryTraceContext(trace: SentryId(), publicKey: "PUBLIC_KEY", releaseName: "RELEASE_NAME", environment: "TEST", transaction: "transaction", userSegment: "some segment", sampleRate: "0.25") } + func testSerializationFailsWithInvalidJSONObject() { + let json: [String: Any] = [ + "valid object": "hi, i'm a valid object", + "invalid object": NSDate() + ] + + var data: Data? + let exp = expectation(description: "Should throw error") + do { + data = try SentrySerialization.data(withJSONObject: json) + } catch { + exp.fulfill() + XCTAssertEqual(error.localizedDescription, "Event cannot be converted to JSON (Invalid type in JSON write (__NSTaggedDate))") + } + waitForExpectations(timeout: 1) + XCTAssertNil(data) + } + func testSentryEnvelopeSerializer_WithSingleEvent() { // Arrange let event = Event() diff --git a/Tests/SentryTests/Protocol/SentryNSErrorTests.swift b/Tests/SentryTests/Protocol/SentryNSErrorTests.swift index c323f821033..a35e8eee30f 100644 --- a/Tests/SentryTests/Protocol/SentryNSErrorTests.swift +++ b/Tests/SentryTests/Protocol/SentryNSErrorTests.swift @@ -4,11 +4,41 @@ class SentryNSErrorTests: XCTestCase { func testSerialize() { let error = SentryNSError(domain: "domain", code: 10) - + let actual = error.serialize() XCTAssertEqual(error.domain, actual["domain"] as? String) XCTAssertEqual(error.code, actual["code"] as? Int) } + func testSerializeWithUnderlyingNSError() { + let inputUnderlyingErrorCode = 5_123 + let inputUnderlyingError = NSError(domain: "test-error-domain", code: inputUnderlyingErrorCode) + let inputDescription = "some test error" + let actualError = NSErrorFromSentryErrorWithUnderlyingError(SentryError.unknownError, inputDescription, inputUnderlyingError) + + XCTAssertEqual(actualError?.localizedDescription, inputDescription) + guard let actualUnderlyingError = (actualError as? NSError)?.userInfo[NSUnderlyingErrorKey] as? NSError else { + XCTFail("Expected an underlying error in the returned error's info dict") + return + } + XCTAssertEqual(actualUnderlyingError.code, inputUnderlyingErrorCode) + XCTAssertEqual(actualUnderlyingError.domain, inputUnderlyingError.domain) + } + + func testSerializeWithUnderlyingNSException() { + let inputExceptionName = NSExceptionName.decimalNumberDivideByZeroException + let inputExceptionReason = "test exception reason" + let inputUnderlyingException = NSException(name: inputExceptionName, reason: inputExceptionReason, userInfo: ["some userinfo key": "some userinfo value"]) + let inputDescription = "some test exception" + + let actualError = NSErrorFromSentryErrorWithException(SentryError.unknownError, inputDescription, inputUnderlyingException) + + guard let actualDescription = actualError?.localizedDescription else { + XCTFail("Expected a localizedDescription in the resulting error") + return + } + XCTAssertEqual(actualDescription, "\(inputDescription) (\(inputExceptionReason))") + } + } From d9bcb318fd265738102b3f8389421348a3b2606d Mon Sep 17 00:00:00 2001 From: Dhiogo Ramos Brustolin Date: Mon, 14 Nov 2022 09:39:34 +0100 Subject: [PATCH 6/6] Update SentryNSErrorTests.swift --- Tests/SentryTests/Protocol/SentryNSErrorTests.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/SentryTests/Protocol/SentryNSErrorTests.swift b/Tests/SentryTests/Protocol/SentryNSErrorTests.swift index a35e8eee30f..2e5605f84c1 100644 --- a/Tests/SentryTests/Protocol/SentryNSErrorTests.swift +++ b/Tests/SentryTests/Protocol/SentryNSErrorTests.swift @@ -18,7 +18,8 @@ class SentryNSErrorTests: XCTestCase { let actualError = NSErrorFromSentryErrorWithUnderlyingError(SentryError.unknownError, inputDescription, inputUnderlyingError) XCTAssertEqual(actualError?.localizedDescription, inputDescription) - guard let actualUnderlyingError = (actualError as? NSError)?.userInfo[NSUnderlyingErrorKey] as? NSError else { + + guard let error = actualError, let actualUnderlyingError = (error as NSError).userInfo[NSUnderlyingErrorKey] as? NSError else { XCTFail("Expected an underlying error in the returned error's info dict") return }