Skip to content

Commit

Permalink
Reduce use of RCTLogError (redbox) in prop parsing (#36161)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #36161

Changelog:
[iOS][Fixed] - Invalid prop values no longer trigger redbox in the legacy renderer

## Context

We are changing React Native to behave more like a browser in the sense that **bad style values are not runtime errors**. (See e.g. D43159284 (d6e9891), D43184380.) The recommended way for developers to ensure they are passing correct style values is to use a typechecker (TypeScript or Flow) in conjunction with E2E tests and manual spot checks.

## This diff

This change is similar to D43184380, but here we target the legacy renderer on iOS by removing the redboxes from most of `RCTConvert`'s methods.

I'm intentionally going with the simplest possible improvement which is to downgrade the redboxes to `RCTLogInfo` ( = log to stdout but not the JS console). Leaving the call sites in place (as opposed to deleting them) will be helpful if we decide that we want to repurpose these checks for a new, more visible diagnostic (though we would likely only build such a diagnostic in Fabric at this point).

Reviewed By: javache

Differential Revision: D43184379

fbshipit-source-id: 5a3d12f5d884372c7dc8743227d58d403caf24e3
  • Loading branch information
motiz88 authored and facebook-github-bot committed Feb 15, 2023
1 parent 167d52d commit cb28a2c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion React/Base/RCTConvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ RCT_EXTERN NSArray *RCTConvertArrayValue(SEL, id);
* avoid repeating the same boilerplate for every error message.
*/
#define RCTLogConvertError(json, typeName) \
RCTLogError(@"JSON value '%@' of type %@ cannot be converted to %@", json, [json classForCoder], typeName)
RCTLogInfo(@"JSON value '%@' of type %@ cannot be converted to %@", json, [json classForCoder], typeName)

/**
* This macro is used for creating simple converter functions that just call
Expand Down
22 changes: 11 additions & 11 deletions React/Base/RCTConvert.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ + (NSIndexSet *)NSIndexSet:(id)json
for (NSNumber *number in json) {
NSInteger index = number.integerValue;
if (RCT_DEBUG && index < 0) {
RCTLogError(@"Invalid index value %lld. Indices must be positive.", (long long)index);
RCTLogInfo(@"Invalid index value %lld. Indices must be positive.", (long long)index);
}
[indexSet addIndex:index];
}
Expand Down Expand Up @@ -169,7 +169,7 @@ + (NSURLRequest *)NSURLRequest:(id)json
__block BOOL allHeadersAreStrings = YES;
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id header, BOOL *stop) {
if (![header isKindOfClass:[NSString class]]) {
RCTLogError(
RCTLogInfo(
@"Values of HTTP headers passed must be of type string. "
"Value of header '%@' is not a string.",
key);
Expand Down Expand Up @@ -200,11 +200,11 @@ + (RCTFileURL *)RCTFileURL:(id)json
{
NSURL *fileURL = [self NSURL:json];
if (!fileURL.fileURL) {
RCTLogError(@"URI must be a local file, '%@' isn't.", fileURL);
RCTLogInfo(@"URI must be a local file, '%@' isn't.", fileURL);
return nil;
}
if (![[NSFileManager defaultManager] fileExistsAtPath:fileURL.path]) {
RCTLogError(@"File '%@' could not be found.", fileURL);
RCTLogInfo(@"File '%@' could not be found.", fileURL);
return nil;
}
return fileURL;
Expand All @@ -225,7 +225,7 @@ + (NSDate *)NSDate:(id)json
});
NSDate *date = [formatter dateFromString:json];
if (!date) {
RCTLogError(
RCTLogInfo(
@"JSON String '%@' could not be interpreted as a date. "
"Expected format: YYYY-MM-DD'T'HH:mm:ss.sssZ",
json);
Expand All @@ -242,7 +242,7 @@ + (NSLocale *)NSLocale:(id)json
if ([json isKindOfClass:[NSString class]]) {
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:json];
if (!locale) {
RCTLogError(@"JSON String '%@' could not be interpreted as a valid locale. ", json);
RCTLogInfo(@"JSON String '%@' could not be interpreted as a valid locale. ", json);
}
return locale;
} else if (json) {
Expand All @@ -267,15 +267,15 @@ + (NSLocale *)NSLocale:(id)json
if ([allValues containsObject:json] || [json isEqual:defaultValue]) {
return json;
}
RCTLogError(@"Invalid %s '%@'. should be one of: %@", typeName, json, allValues);
RCTLogInfo(@"Invalid %s '%@'. should be one of: %@", typeName, json, allValues);
return defaultValue;
}
if (RCT_DEBUG && ![json isKindOfClass:[NSString class]]) {
RCTLogError(@"Expected NSNumber or NSString for %s, received %@: %@", typeName, [json classForCoder], json);
RCTLogInfo(@"Expected NSNumber or NSString for %s, received %@: %@", typeName, [json classForCoder], json);
}
id value = mapping[json];
if (RCT_DEBUG && !value && [json description].length > 0) {
RCTLogError(
RCTLogInfo(
@"Invalid %s '%@'. should be one of: %@",
typeName,
json,
Expand Down Expand Up @@ -541,7 +541,7 @@ static void convertCGStruct(const char *type, NSArray *fields, CGFloat *result,
NSUInteger count = fields.count;
if ([json isKindOfClass:[NSArray class]]) {
if (RCT_DEBUG && [json count] != count) {
RCTLogError(
RCTLogInfo(
@"Expected array with count %llu, but count is %llu: %@",
(unsigned long long)count,
(unsigned long long)[json count],
Expand Down Expand Up @@ -1295,7 +1295,7 @@ + (UIImage *)UIImage:(id)json
}

if (!CGSizeEqualToSize(imageSource.size, CGSizeZero) && !CGSizeEqualToSize(imageSource.size, image.size)) {
RCTLogError(
RCTLogInfo(
@"Image source %@ size %@ does not match loaded image size %@.",
URL.path.lastPathComponent,
NSStringFromCGSize(imageSource.size),
Expand Down
2 changes: 1 addition & 1 deletion React/Views/RCTConvert+Transform.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ + (CATransform3D)CATransform3D:(id)json
transform = CATransform3DConcat(next, transform);

} else {
RCTLogError(@"Unsupported transform type for a CATransform3D: %@.", property);
RCTLogInfo(@"Unsupported transform type for a CATransform3D: %@.", property);
}
}
return transform;
Expand Down
2 changes: 1 addition & 1 deletion React/Views/RCTFont.mm
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ + (RCTFontVariantDescriptor *)RCTFontVariantDescriptor:(id)json
});
RCTFontVariantDescriptor *value = mapping[json];
if (RCT_DEBUG && !value && [json description].length > 0) {
RCTLogError(
RCTLogInfo(
@"Invalid RCTFontVariantDescriptor '%@'. should be one of: %@",
json,
[[mapping allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]);
Expand Down
2 changes: 1 addition & 1 deletion React/Views/RCTView.m
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
case RCTPointerEventsBoxNone:
return hitSubview;
default:
RCTLogError(@"Invalid pointer-events specified %lld on %@", (long long)_pointerEvents, self);
RCTLogInfo(@"Invalid pointer-events specified %lld on %@", (long long)_pointerEvents, self);
return hitSubview ?: hitView;
}
}
Expand Down
2 changes: 1 addition & 1 deletion React/Views/RCTViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ - (RCTShadowView *)shadowView
view.userInteractionEnabled = NO;
break;
default:
RCTLogError(@"UIView base class does not support pointerEvent value: %@", json);
RCTLogInfo(@"UIView base class does not support pointerEvent value: %@", json);
}
}
RCT_CUSTOM_VIEW_PROPERTY(removeClippedSubviews, BOOL, RCTView)
Expand Down

0 comments on commit cb28a2c

Please sign in to comment.