Skip to content

Commit e160f54

Browse files
Luke De Feofacebook-github-bot
Luke De Feo
authored andcommitted
Client error message
Summary: See previous diff in stack for context, serialization can fail and when it does we just send an empty string payload to flipper which is v confusing. Instead we now do proper error handling A note about the use of try catch around nsjson serialization i know this is not typical but the previous approach was to use the validator function to see if data could be serialized. this was returning false before but the problem is we have idea what the error is. Unfortunatley despite taking an error parm the NSJSONSerialization dataWithJSONObject function throws an exception so im force to catch it to get a useful error message Differential Revision: D65605276 fbshipit-source-id: 506ecc48bd0692a622679a8f621fede7d626c122
1 parent a4690bd commit e160f54

File tree

3 files changed

+91
-27
lines changed

3 files changed

+91
-27
lines changed

iOS/Plugins/FlipperKitUIDebuggerPlugin/FlipperKitUIDebuggerPlugin/Observer/UIDTreeObserverManager.m

+50-4
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,37 @@ - (void)sendInit {
207207
init.frameworkEventMetadata = [_context.frameworkEventManager eventsMetadata];
208208
init.currentTraversalMode = _traversalMode;
209209

210-
[_context.connection send:[UIDInitEvent name] withRawParams:UID_toJSON(init)];
210+
NSError* error = nil;
211+
if (error) {
212+
[self sendClientErrorMessage:error step:@"init"];
213+
return;
214+
}
215+
[_context.connection send:[UIDInitEvent name]
216+
withRawParams:UID_toJSON(init, &error)];
217+
}
218+
219+
- (void)sendClientErrorMessage:(NSError*)error step:(NSString*)step {
220+
#pragma clang diagnostic push
221+
#pragma clang diagnostic ignored "-Wnullable-to-nonnull-conversion"
222+
223+
NSDictionary* clientError = @{
224+
@"errorMessage" : error.localizedDescription,
225+
@"errorType" : error.localizedFailureReason,
226+
@"step" : step,
227+
};
228+
229+
NSError* errorMessageSerializeError = nil;
230+
NSString* clientErrorEvent =
231+
UID_FoundationtoJSON(clientError, &errorMessageSerializeError);
232+
233+
if (errorMessageSerializeError) {
234+
// uh oh...
235+
return;
236+
}
237+
238+
[_context.connection send:@"traversalError" withRawParams:clientErrorEvent];
239+
240+
#pragma clang diagnostic pop
211241
}
212242

213243
- (void)sendMetadataUpdate {
@@ -220,7 +250,12 @@ - (void)sendMetadataUpdate {
220250
UIDMetadataUpdateEvent* metadataUpdateEvent = [UIDMetadataUpdateEvent new];
221251
metadataUpdateEvent.attributeMetadata = pendingMetadata;
222252

223-
id JSON = UID_toJSON(metadataUpdateEvent);
253+
NSError* error = nil;
254+
id JSON = UID_toJSON(metadataUpdateEvent, &error);
255+
if (error) {
256+
[self sendClientErrorMessage:error step:@"metadataUpdate"];
257+
return;
258+
}
224259

225260
[_context.connection send:[UIDMetadataUpdateEvent name] withRawParams:JSON];
226261
}
@@ -252,7 +287,14 @@ - (void)digest:(nonnull id<UIDUpdate>)update {
252287

253288
uint64_t t3 = UIDPerformanceNow();
254289

255-
NSString* JSON = UID_FoundationtoJSON(intermediate);
290+
NSError* error = nil;
291+
292+
NSString* JSON = UID_FoundationtoJSON(intermediate, &error);
293+
if (error) {
294+
[self sendClientErrorMessage:error step:@"serialization"];
295+
return;
296+
}
297+
256298
uint64_t t4 = UIDPerformanceNow();
257299
NSUInteger payloadSize = JSON.length;
258300

@@ -280,8 +322,12 @@ - (void)digest:(nonnull id<UIDUpdate>)update {
280322
perfStats.payloadSize = payloadSize;
281323
perfStats.dynamicMeasures = UIDPerformanceGet();
282324

325+
NSString* perfStatsMessage = UID_toJSON(perfStats, &error);
326+
if (error) {
327+
[self sendClientErrorMessage:error step:@"perfStats"];
328+
}
283329
[self->_context.connection send:[UIDPerfStatsEvent name]
284-
withRawParams:UID_toJSON(perfStats)];
330+
withRawParams:perfStatsMessage];
285331
});
286332
}
287333

iOS/Plugins/FlipperKitUIDebuggerPlugin/FlipperKitUIDebuggerPlugin/Serializers/UIDJSONSerializer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#ifdef __cplusplus
1818
extern "C" {
1919
#endif
20-
extern NSString* UID_toJSON(id obj);
21-
extern NSString* UID_FoundationtoJSON(id obj);
20+
extern NSString* UID_toJSON(id obj, NSError** error);
21+
extern NSString* UID_FoundationtoJSON(id obj, NSError** error);
2222
extern id UID_toFoundation(id<UIDFoundation> obj);
2323
#ifdef __cplusplus
2424
}

iOS/Plugins/FlipperKitUIDebuggerPlugin/FlipperKitUIDebuggerPlugin/Serializers/UIDJSONSerializer.mm

+39-21
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,55 @@
3030
id UID_toFoundation(id<UIDFoundation> object) {
3131
return [object toFoundation];
3232
}
33+
NSString* UID_FoundationtoJSON(id object, NSError** error) {
34+
uint64_t t0 = UIDPerformanceNow();
3335

34-
NSString* UID_FoundationtoJSON(id object) {
35-
NSError* error = NULL;
36-
if (![NSJSONSerialization isValidJSONObject:object]) {
37-
return @"";
38-
}
36+
// NSJSONSerialization dataWithJSONObject throws and exception if data not
37+
// serializable despite taking an error param hence the try catch
38+
@try {
39+
uint64_t t1 = UIDPerformanceNow();
3940

40-
uint64_t t0 = UIDPerformanceNow();
41+
NSData* data = [NSJSONSerialization dataWithJSONObject:object
42+
options:0
43+
error:error];
44+
45+
if (*error) {
46+
return nil;
47+
}
4148

42-
NSData* data = [NSJSONSerialization dataWithJSONObject:object
43-
options:0
44-
error:&error];
45-
uint64_t t1 = UIDPerformanceNow();
49+
UIDPerformanceAggregate(
50+
@"serialisationBinaryMS",
51+
UIDMonotonicTimeConvertMachUnitsToMS(t1 - t0));
4652

47-
UIDPerformanceAggregate(
48-
@"serialisationBinaryMS", UIDMonotonicTimeConvertMachUnitsToMS(t1 - t0));
53+
NSString* JSON = [[NSString alloc] initWithData:data
54+
encoding:NSUTF8StringEncoding];
4955

50-
NSString* JSON = [[NSString alloc] initWithData:data
51-
encoding:NSUTF8StringEncoding];
56+
uint64_t t2 = UIDPerformanceNow();
5257

53-
uint64_t t2 = UIDPerformanceNow();
58+
UIDPerformanceAggregate(
59+
@"serialisationEncodingMS",
60+
UIDMonotonicTimeConvertMachUnitsToMS(t2 - t1));
5461

55-
UIDPerformanceAggregate(
56-
@"serialisationEncodingMS",
57-
UIDMonotonicTimeConvertMachUnitsToMS(t2 - t1));
62+
return JSON;
5863

59-
return JSON;
64+
} @catch (NSException* exception) {
65+
#pragma clang diagnostic push
66+
#pragma clang diagnostic ignored "-Wnullable-to-nonnull-conversion"
67+
*error = [NSError errorWithDomain:@"com.facebook.flipper.uidebugger"
68+
code:100
69+
userInfo:@{
70+
NSLocalizedDescriptionKey : exception.reason,
71+
NSLocalizedFailureReasonErrorKey : exception.name
72+
}];
73+
74+
return nil;
75+
76+
#pragma clang diagnostic pop
77+
}
6078
}
6179

62-
NSString* UID_toJSON(id object) {
63-
return UID_FoundationtoJSON(UID_toFoundation(object));
80+
NSString* UID_toJSON(id object, NSError** error) {
81+
return UID_FoundationtoJSON(UID_toFoundation(object), error);
6482
}
6583
#ifdef __cplusplus
6684
}

0 commit comments

Comments
 (0)