Skip to content

Commit

Permalink
Do not crash if source cannot be resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas committed Apr 29, 2020
1 parent ab7a76f commit 14d954e
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions ios/RCTMGL/RCTMGLCircleLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ @implementation RCTMGLCircleLayer
- (MGLCircleStyleLayer*)makeLayer:(MGLStyle*)style
{
MGLSource *source = [style sourceWithIdentifier:self.sourceID];
if (source == nil) { return nil; }
MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithIdentifier:self.id source:source];
layer.sourceLayerIdentifier = self.sourceLayerID;
return layer;
Expand Down
1 change: 1 addition & 0 deletions ios/RCTMGL/RCTMGLFillExtrusionLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @implementation RCTMGLFillExtrusionLayer
- (MGLFillExtrusionStyleLayer*)makeLayer:(MGLStyle*)style
{
MGLSource *source = [style sourceWithIdentifier:self.sourceID];
if (source == nil) { return nil; }
MGLFillExtrusionStyleLayer *layer = [[MGLFillExtrusionStyleLayer alloc] initWithIdentifier:self.id source:source];
layer.sourceLayerIdentifier = self.sourceLayerID;
return layer;
Expand Down
1 change: 1 addition & 0 deletions ios/RCTMGL/RCTMGLFillLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @implementation RCTMGLFillLayer
- (MGLStyleLayer*)makeLayer:(MGLStyle*)style
{
MGLSource *source = [style sourceWithIdentifier:self.sourceID];
if (source == nil) { return nil; }
MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithIdentifier:self.id source:source];
layer.sourceLayerIdentifier = self.sourceLayerID;
return layer;
Expand Down
1 change: 1 addition & 0 deletions ios/RCTMGL/RCTMGLHeatmapLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ @implementation RCTMGLHeatmapLayer
- (MGLHeatmapStyleLayer*)makeLayer:(MGLStyle*)style
{
MGLSource *source = [style sourceWithIdentifier:self.sourceID];
if (source == nil) { return nil; }
MGLHeatmapStyleLayer *layer = [[MGLHeatmapStyleLayer alloc] initWithIdentifier:self.id source:source];
layer.sourceLayerIdentifier = self.sourceLayerID;
return layer;
Expand Down
2 changes: 1 addition & 1 deletion ios/RCTMGL/RCTMGLImageSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ - (void)setUrl:(NSString *)url
}
}

- (MGLSource *)makeSource
- (nullable MGLSource *)makeSource
{
NSURL *myURL;

Expand Down
6 changes: 5 additions & 1 deletion ios/RCTMGL/RCTMGLLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ - (void)addToMap:(RCTMGLMapView*) map style:(MGLStyle *)style
_styleLayer = existingLayer;
} else {
_styleLayer = [self makeLayer:style];
if (_styleLayer == nil) {
RCTLogError(@"Failed to make layer: %@", _id);
return;
}
[self insertLayer: map];
}

Expand All @@ -122,7 +126,7 @@ - (void)removeFromMap:(MGLStyle *)style
}
}

- (MGLStyleLayer*)makeLayer:(MGLStyle*)style
- (nullable MGLStyleLayer*)makeLayer:(MGLStyle*)style
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
Expand Down
1 change: 1 addition & 0 deletions ios/RCTMGL/RCTMGLLineLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @implementation RCTMGLLineLayer
- (MGLLineStyleLayer*)makeLayer:(MGLStyle*)style
{
MGLSource *source = [style sourceWithIdentifier:self.sourceID];
if (source == nil) { return nil; }
MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithIdentifier:self.id source:source];
layer.sourceLayerIdentifier = self.sourceLayerID;
return layer;
Expand Down
1 change: 1 addition & 0 deletions ios/RCTMGL/RCTMGLRasterLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @implementation RCTMGLRasterLayer
- (MGLStyleLayer*)makeLayer:(MGLStyle*)style
{
MGLSource *source = [style sourceWithIdentifier:self.sourceID];
if (source == nil) { return nil; }
MGLRasterStyleLayer *layer = [[MGLRasterStyleLayer alloc] initWithIdentifier:self.id source:source];
return layer;
}
Expand Down
2 changes: 1 addition & 1 deletion ios/RCTMGL/RCTMGLRasterSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@implementation RCTMGLRasterSource

- (MGLSource*)makeSource
- (nullable MGLSource*)makeSource
{
if (self.url != nil) {
NSURL *url = [NSURL URLWithString:self.url];
Expand Down
9 changes: 6 additions & 3 deletions ios/RCTMGL/RCTMGLShapeSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ - (void)removeFromMap
[super removeFromMap];
}

- (MGLSource*)makeSource
- (nullable MGLSource*)makeSource
{
NSDictionary<MGLShapeSourceOption, id> *options = [self _getOptions];

Expand All @@ -59,8 +59,11 @@ - (MGLSource*)makeSource
return [[MGLShapeSource alloc] initWithIdentifier:self.id shape:shape options:options];
}

NSURL *url = [[NSURL alloc] initWithString:_url];
return [[MGLShapeSource alloc] initWithIdentifier:self.id URL:url options:options];
if (_url != nil) {
NSURL *url = [[NSURL alloc] initWithString:_url];
return [[MGLShapeSource alloc] initWithIdentifier:self.id URL:url options:options];
}
return nil;
}

- (NSDictionary<MGLShapeSourceOption, id>*)_getOptions
Expand Down
2 changes: 1 addition & 1 deletion ios/RCTMGL/RCTMGLSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern NSString * _Nonnull const DEFAULT_SOURCE_ID;

- (void)addToMap;
- (void)removeFromMap;
- (MGLSource*)makeSource;
- (nullable MGLSource*)makeSource;
- (NSArray<NSString *> *)getLayerIDs;

+ (BOOL)isDefaultSource:(NSString*)sourceID;
Expand Down
9 changes: 7 additions & 2 deletions ios/RCTMGL/RCTMGLSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "RCTMGLSource.h"
#import "UIView+React.h"
#import "RCTMGLMapView.h"
#import <React/RCTLog.h>

@implementation RCTMGLSource

Expand Down Expand Up @@ -83,7 +84,11 @@ - (void)addToMap
_source = existingSource;
} else {
_source = [self makeSource];
[_map.style addSource:_source];
if (_source != nil) {
[_map.style addSource:_source];
} else {
RCTLogError(@"Count not find source: %@. Either make sure it's an existing id or provide url/shape or other data required to create that source", _id);
}
}

if (_layers.count > 0) {
Expand Down Expand Up @@ -112,7 +117,7 @@ - (void)removeFromMap
}
}

- (MGLSource*)makeSource
- (nullable MGLSource*)makeSource
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
Expand Down
1 change: 1 addition & 0 deletions ios/RCTMGL/RCTMGLSymbolLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ - (void)addedToMap
- (MGLSymbolStyleLayer*)makeLayer:(MGLStyle*)style
{
MGLSource *source = [style sourceWithIdentifier:self.sourceID];
if (source == nil) { return nil; }
MGLSymbolStyleLayer *layer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:self.id source:source];
layer.sourceLayerIdentifier = self.sourceLayerID;
return layer;
Expand Down

0 comments on commit 14d954e

Please sign in to comment.