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 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#include <memory.h>

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

// ============================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "SentryCrashJSONCodec.h"
#include "SentryCrashMonitorContext.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <errno.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "SentryCrashStackCursor_SelfThread.h"
#include "SentryCrashThread.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <cxxabi.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#import "SentryCrashStackCursor_Backtrace.h"
#include "SentryCrashThread.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#import "SentryCrashLogger.h"

// ============================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#import "SentryCrashSysCtl.h"
#import "SentryCrashSystemCapabilities.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#import "SentryCrashLogger.h"

#import <CommonCrypto/CommonDigest.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "SentryCrashStackCursor_SelfThread.h"
#include "SentryCrashThread.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <memory.h>
Expand Down
7 changes: 5 additions & 2 deletions Sources/SentryCrash/Recording/SentryCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#import "SentryCrashSystemCapabilities.h"
#import <NSData+Sentry.h>

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#import "SentryCrashLogger.h"

#include <inttypes.h>
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/SentryCrashC.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "SentryCrashString.h"
#include "SentryCrashSystemCapabilities.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <inttypes.h>
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/SentryCrashCachedData.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "SentryCrashCachedData.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <errno.h>
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/SentryCrashReport.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include "SentryCrashUUIDConversion.h"
#include "SentryScopeSyncC.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <errno.h>
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/Tools/SentryCrashCPU.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <mach-o/arch.h>
#include <mach/mach.h>

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

const char *
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# include "SentryCrashMachineContext_Apple.h"
# include <stdlib.h>

//#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",
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/Tools/SentryCrashCPU_arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# include "SentryCrashMachineContext_Apple.h"
# include <stdlib.h>

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
# include "SentryCrashLogger.h"

# define KSPACStrippingMask_ARM64e 0x0000000fffffffff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# include "SentryCrashMachineContext_Apple.h"
# include <stdlib.h>

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
# include "SentryCrashLogger.h"

static const char *g_registerNames[] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

# include <stdlib.h>

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
# include "SentryCrashLogger.h"

static const char *g_registerNames[] = { "rax", "rbx", "rcx", "rdx", "rdi", "rsi", "rbp", "rsp",
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/Tools/SentryCrashDebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "SentryCrashDebug.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <errno.h>
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "SentryCrashFileUtils.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <dirent.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#include <mach/mach.h>

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#ifdef __arm64__
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/Tools/SentryCrashMemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "SentryCrashMemory.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <_types/_uint8_t.h>
Expand Down
4 changes: 2 additions & 2 deletions Sources/SentryCrash/Recording/Tools/SentryCrashObjCApple.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "SentryCrashSymbolicator.h"
#include <stdlib.h>

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

static bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "SentryCrashStackCursor_Backtrace.h"
#include "SentryCrashCPU.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

static bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "SentryCrashStackCursor_Backtrace.h"
#include <execinfo.h>

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#define MAX_BACKTRACE_LENGTH \
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/Tools/SentryCrashSysCtl.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "SentryCrashSysCtl.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <errno.h>
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/Tools/SentryCrashThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "SentryCrashMemory.h"
#include "SentryCrashSystemCapabilities.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"

#include <dispatch/dispatch.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#import "NSError+SentrySimpleConstructor.h"
#import "SentryCrashVarArgs.h"

//#define SentryCrashLogger_LocalLevel TRACE
// #define SentryCrashLogger_LocalLevel TRACE
#import "SentryCrashLogger.h"

@implementation SentryCrashReportFilterPassthrough
Expand Down
18 changes: 18 additions & 0 deletions Tests/SentryTests/Helper/SentrySerializationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading