Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[ios] Standardize exception definitions #12583

Merged
merged 5 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions platform/darwin/src/MGLComputedShapeSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ FOUNDATION_EXTERN MGL_EXPORT const MGLShapeSourceOption MGLShapeSourceOptionWrap
*/
FOUNDATION_EXTERN MGL_EXPORT const MGLShapeSourceOption MGLShapeSourceOptionClipsCoordinates;

FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLInvalidDatasourceException;

/**
Data source for `MGLComputedShapeSource`. This protocol defines two optional methods for fetching
data, one based on tile coordinates, and one based on a bounding box. Classes that implement this
Expand Down
12 changes: 8 additions & 4 deletions platform/darwin/src/MGLComputedShapeSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <mbgl/tile/tile_id.hpp>
#include <mbgl/util/geojson.hpp>

const MGLExceptionName MGLInvalidDatasourceException = @"MGLInvalidDatasourceException";

const MGLShapeSourceOption MGLShapeSourceOptionWrapsCoordinates = @"MGLShapeSourceOptionWrapsCoordinates";
const MGLShapeSourceOption MGLShapeSourceOptionClipsCoordinates = @"MGLShapeSourceOptionClipsCoordinates";

Expand Down Expand Up @@ -205,10 +207,12 @@ - (void)setDataSource:(id<MGLComputedShapeSourceDataSource>)dataSource {
self.dataSourceImplementsFeaturesForTile = [dataSource respondsToSelector:@selector(featuresInTileAtX:y:zoomLevel:)];
self.dataSourceImplementsFeaturesForBounds = [dataSource respondsToSelector:@selector(featuresInCoordinateBounds:zoomLevel:)];

if(!self.dataSourceImplementsFeaturesForBounds && !self.dataSourceImplementsFeaturesForTile) {
[NSException raise:@"Invalid Datasource" format:@"Datasource does not implement any MGLComputedShapeSourceDataSource methods"];
} else if(self.dataSourceImplementsFeaturesForBounds && self.dataSourceImplementsFeaturesForTile) {
[NSException raise:@"Invalid Datasource" format:@"Datasource implements multiple MGLComputedShapeSourceDataSource methods"];
if (!self.dataSourceImplementsFeaturesForBounds && !self.dataSourceImplementsFeaturesForTile) {
[NSException raise:MGLInvalidDatasourceException
format:@"Datasource does not implement any MGLComputedShapeSourceDataSource methods"];
} else if (self.dataSourceImplementsFeaturesForBounds && self.dataSourceImplementsFeaturesForTile) {
[NSException raise:MGLInvalidDatasourceException
format:@"Datasource implements multiple MGLComputedShapeSourceDataSource methods"];
}

_dataSource = dataSource;
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLForegroundStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@implementation MGLForegroundStyleLayer

- (NSString *)sourceIdentifier {
[NSException raise:@"MGLAbstractClassException"
[NSException raise:MGLAbstractClassException
format:@"MGLForegroundStyleLayer is an abstract class"];
return nil;
}
Expand Down
3 changes: 3 additions & 0 deletions platform/darwin/src/MGLOfflinePack.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#import <Foundation/Foundation.h>

#import "MGLFoundation.h"
#import "MGLTypes.h"
#import "MGLOfflineRegion.h"

NS_ASSUME_NONNULL_BEGIN

FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLInvalidOfflinePackException;

/**
The state an offline pack is currently in.
*/
Expand Down
4 changes: 3 additions & 1 deletion platform/darwin/src/MGLOfflinePack.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <mbgl/storage/default_file_source.hpp>

const MGLExceptionName MGLInvalidOfflinePackException = @"MGLInvalidOfflinePackException";

/**
Assert that the current offline pack is valid.

Expand All @@ -17,7 +19,7 @@
#define MGLAssertOfflinePackIsValid() \
do { \
if (_state == MGLOfflinePackStateInvalid) { \
[NSException raise:@"Invalid offline pack" \
[NSException raise:MGLInvalidOfflinePackException \
format: \
@"-[MGLOfflineStorage removePack:withCompletionHandler:] has been called " \
@"on this instance of MGLOfflinePack, rendering it invalid. It is an " \
Expand Down
2 changes: 2 additions & 0 deletions platform/darwin/src/MGLOfflineStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ FOUNDATION_EXTERN MGL_EXPORT const MGLOfflinePackUserInfoKey MGLOfflinePackUserI

FOUNDATION_EXTERN MGL_EXPORT NSString * const MGLOfflinePackMaximumCountUserInfoKey __attribute__((unavailable("Use MGLOfflinePackUserInfoKeyMaximumCount")));

FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLUnsupportedRegionTypeException;

/**
A block to be called once an offline pack has been completely created and
added.
Expand Down
6 changes: 4 additions & 2 deletions platform/darwin/src/MGLOfflineStorage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
const MGLOfflinePackUserInfoKey MGLOfflinePackUserInfoKeyError = @"Error";
const MGLOfflinePackUserInfoKey MGLOfflinePackUserInfoKeyMaximumCount = @"MaximumCount";

const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegionTypeException";

@interface MGLOfflineStorage ()

@property (nonatomic, strong, readwrite) NSMutableArray<MGLOfflinePack *> *packs;
Expand Down Expand Up @@ -278,8 +280,8 @@ - (void)addPackForRegion:(id <MGLOfflineRegion>)region withContext:(NSData *)con

- (void)_addPackForRegion:(id <MGLOfflineRegion>)region withContext:(NSData *)context completionHandler:(MGLOfflinePackAdditionCompletionHandler)completion {
if (![region conformsToProtocol:@protocol(MGLOfflineRegion_Private)]) {
[NSException raise:@"Unsupported region type" format:
@"Regions of type %@ are unsupported.", NSStringFromClass([region class])];
[NSException raise:MGLUnsupportedRegionTypeException
format:@"Regions of type %@ are unsupported.", NSStringFromClass([region class])];
return;
}

Expand Down
7 changes: 3 additions & 4 deletions platform/darwin/src/MGLShape.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ + (nullable MGLShape *)shapeWithData:(NSData *)data encoding:(NSStringEncoding)e
}

- (mbgl::Geometry<double>)geometryObject {
[NSException raise:@"MGLAbstractClassException"
[NSException raise:MGLAbstractClassException
format:@"MGLShape is an abstract class"];
return mbgl::Point<double>();
}
Expand Down Expand Up @@ -103,9 +103,8 @@ - (NSUInteger)hash

- (CLLocationCoordinate2D)coordinate
{
[[NSException exceptionWithName:@"MGLAbstractClassException"
reason:@"MGLShape is an abstract class"
userInfo:nil] raise];
[NSException raise:MGLAbstractClassException
format:@"MGLShape is an abstract class"];
return kCLLocationCoordinate2DInvalid;
}

Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ - (instancetype)initWithPendingSource:(std::unique_ptr<mbgl::style::Source>)pend

- (void)addToMapView:(MGLMapView *)mapView {
if (_pendingSource == nullptr) {
[NSException raise:@"MGLRedundantSourceException"
[NSException raise:MGLRedundantSourceException
format:@"This instance %@ was already added to %@. Adding the same source instance " \
"to the style more than once is invalid.", self, mapView.style];
}
Expand Down
6 changes: 6 additions & 0 deletions platform/darwin/src/MGLStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ NS_ASSUME_NONNULL_BEGIN
*/
static MGL_EXPORT const NSInteger MGLStyleDefaultVersion = 10;

FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLInvalidStyleURLException;
FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLRedundantLayerException;
FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLRedundantLayerIdentifierException;
FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLRedundantSourceException;
FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLRedundantSourceIdentifierException;

/**
The proxy object for the current map style.

Expand Down
20 changes: 13 additions & 7 deletions platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
#import "NSImage+MGLAdditions.h"
#endif

const MGLExceptionName MGLInvalidStyleURLException = @"MGLInvalidStyleURLException";
const MGLExceptionName MGLRedundantLayerException = @"MGLRedundantLayerException";
const MGLExceptionName MGLRedundantLayerIdentifierException = @"MGLRedundantLayerIdentifierException";
const MGLExceptionName MGLRedundantSourceException = @"MGLRedundantSourceException";
const MGLExceptionName MGLRedundantSourceIdentifierException = @"MGLRedundantSourceIdentifierException";

/**
Model class for localization changes.
*/
Expand Down Expand Up @@ -210,7 +216,7 @@ - (void)addSource:(MGLSource *)source
try {
[source addToMapView:self.mapView];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantSourceIdentifierException" format:@"%s", err.what()];
[NSException raise:MGLRedundantSourceIdentifierException format:@"%s", err.what()];
}
}

Expand Down Expand Up @@ -313,14 +319,14 @@ - (void)insertObject:(MGLStyleLayer *)styleLayer inLayersAtIndex:(NSUInteger)ind
MGLStyleLayer *sibling = layers.size() ? [self layerFromMBGLLayer:layers.at(0)] : nil;
[styleLayer addToStyle:self belowLayer:sibling];
} catch (const std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
[NSException raise:MGLRedundantLayerIdentifierException format:@"%s", err.what()];
}
} else {
try {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index)];
[styleLayer addToStyle:self belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
[NSException raise:MGLRedundantLayerIdentifierException format:@"%s", err.what()];
}
}
}
Expand Down Expand Up @@ -402,7 +408,7 @@ - (void)addLayer:(MGLStyleLayer *)layer
try {
[layer addToStyle:self belowLayer:nil];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
[NSException raise:MGLRedundantLayerIdentifierException format:@"%s", err.what()];
}
[self didChangeValueForKey:@"layers"];
}
Expand Down Expand Up @@ -431,7 +437,7 @@ - (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)sibling
try {
[layer addToStyle:self belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
[NSException raise:MGLRedundantLayerIdentifierException format:@"%s", err.what()];
}
[self didChangeValueForKey:@"layers"];
}
Expand Down Expand Up @@ -473,14 +479,14 @@ - (void)insertLayer:(MGLStyleLayer *)layer aboveLayer:(MGLStyleLayer *)sibling {
try {
[layer addToStyle:self belowLayer:nil];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
[NSException raise:MGLRedundantLayerIdentifierException format:@"%s", err.what()];
}
} else {
MGLStyleLayer *sibling = [self layerFromMBGLLayer:layers.at(index + 1)];
try {
[layer addToStyle:self belowLayer:sibling];
} catch (std::runtime_error & err) {
[NSException raise:@"MGLRedundantLayerIdentifierException" format:@"%s", err.what()];
[NSException raise:MGLRedundantLayerIdentifierException format:@"%s", err.what()];
}
}
[self didChangeValueForKey:@"layers"];
Expand Down
2 changes: 2 additions & 0 deletions platform/darwin/src/MGLStyleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

NS_ASSUME_NONNULL_BEGIN

FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLInvalidStyleLayerException;

/**
`MGLStyleLayer` is an abstract base class for style layers. A style layer
manages the layout and appearance of content at a specific z-index in a style.
Expand Down
4 changes: 3 additions & 1 deletion platform/darwin/src/MGLStyleLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <mbgl/style/style.hpp>
#include <mbgl/style/layer.hpp>

const MGLExceptionName MGLInvalidStyleLayerException = @"MGLInvalidStyleLayerException";

@interface MGLStyleLayer ()

@property (nonatomic, readonly) mbgl::style::Layer *rawLayer;
Expand Down Expand Up @@ -33,7 +35,7 @@ - (instancetype)initWithPendingLayer:(std::unique_ptr<mbgl::style::Layer>)pendin
- (void)addToStyle:(MGLStyle *)style belowLayer:(MGLStyleLayer *)otherLayer
{
if (_pendingLayer == nullptr) {
[NSException raise:@"MGLRedundantLayerException"
[NSException raise:MGLRedundantLayerException
format:@"This instance %@ was already added to %@. Adding the same layer instance " \
"to the style more than once is invalid.", self, style];
}
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLStyleLayer_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct LayerWrapper {
#define MGLAssertStyleLayerIsValid() \
do { \
if (!self.rawLayer) { \
[NSException raise:@"Invalid style layer" \
[NSException raise:MGLInvalidStyleLayerException \
format: \
@"-[MGLStyle removeLayer:] has been called " \
@"with this instance but another style layer instance was added with the same identifer. It is an " \
Expand Down
5 changes: 2 additions & 3 deletions platform/darwin/src/MGLTilePyramidOfflineRegion.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ + (BOOL)supportsSecureCoding {
}

- (instancetype)init {
[NSException raise:@"Method unavailable"
format:
[NSException raise:NSGenericException format:
@"-[MGLTilePyramidOfflineRegion init] is unavailable. "
@"Use -initWithStyleURL:bounds:fromZoomLevel:toZoomLevel: instead."];
return nil;
Expand All @@ -37,7 +36,7 @@ - (instancetype)initWithStyleURL:(NSURL *)styleURL bounds:(MGLCoordinateBounds)b
}

if (!styleURL.scheme) {
[NSException raise:@"Invalid style URL" format:
[NSException raise:MGLInvalidStyleURLException format:
@"%@ does not support setting a relative file URL as the style URL. "
@"To download the online resources required by this style, "
@"specify a URL to an online copy of this style. "
Expand Down
4 changes: 2 additions & 2 deletions platform/darwin/src/MGLTileSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@implementation MGLTileSource

- (NSURL *)configurationURL {
[NSException raise:@"MGLAbstractClassException"
[NSException raise:MGLAbstractClassException
format:@"MGLTileSource is an abstract class"];
return nil;
}
Expand All @@ -41,7 +41,7 @@ - (NSURL *)configurationURL {
}

- (NSString *)attributionHTMLString {
[NSException raise:@"MGLAbstractClassException"
[NSException raise:MGLAbstractClassException
format:@"MGLTileSource is an abstract class"];
return nil;
}
Expand Down
8 changes: 8 additions & 0 deletions platform/darwin/src/MGLTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ NS_ASSUME_NONNULL_BEGIN
typedef NSString *NSNotificationName;
#endif

typedef NSString *MGLExceptionName NS_TYPED_EXTENSIBLE_ENUM;

/**
:nodoc: Generic exceptions used across multiple disparate classes. Exceptions
that are unique to a class or class-cluster should be defined in those headers.
*/
FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLAbstractClassException;

/** Indicates an error occurred in the Mapbox SDK. */
FOUNDATION_EXTERN MGL_EXPORT NSErrorDomain const MGLErrorDomain;

Expand Down
2 changes: 2 additions & 0 deletions platform/darwin/src/MGLTypes.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#import "MGLTypes.h"

const MGLExceptionName MGLAbstractClassException = @"MGLAbstractClassException";

NSString * const MGLErrorDomain = @"MGLErrorDomain";
4 changes: 2 additions & 2 deletions platform/darwin/src/MGLVectorStyleLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
@implementation MGLVectorStyleLayer

- (void)setPredicate:(NSPredicate *)predicate {
[NSException raise:@"MGLAbstractClassException"
[NSException raise:MGLAbstractClassException
format:@"MGLVectorStyleLayer is an abstract class"];
}

- (NSPredicate *)predicate {
[NSException raise:@"MGLAbstractClassException"
[NSException raise:MGLAbstractClassException
format:@"MGLVectorStyleLayer is an abstract class"];
return nil;
}
Expand Down
2 changes: 2 additions & 0 deletions platform/darwin/src/NSBundle+MGLAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN
#define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment) \
[[NSBundle mgl_frameworkBundle] localizedStringForKey:(key) value:(val) table:(tbl)]

FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLBundleNotFoundException;

@interface NSBundle (MGLAdditions)

/// Returns the bundle containing the SDK’s classes and Info.plist file.
Expand Down
6 changes: 4 additions & 2 deletions platform/darwin/src/NSBundle+MGLAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#import "MGLAccountManager.h"

const MGLExceptionName MGLBundleNotFoundException = @"MGLBundleNotFoundException";

@implementation NSBundle (MGLAdditions)

+ (instancetype)mgl_frameworkBundle {
Expand All @@ -14,8 +16,8 @@ + (instancetype)mgl_frameworkBundle {
if (bundlePath) {
bundle = [self bundleWithPath:bundlePath];
} else {
[NSException raise:@"MGLBundleNotFoundException" format:
@"The Mapbox framework bundle could not be found. If using the Mapbox Maps SDK for iOS as a static framework, make sure that Mapbox.bundle is copied into the root of the app bundle."];
[NSException raise:MGLBundleNotFoundException
format:@"The Mapbox framework bundle could not be found. If using the Mapbox Maps SDK for iOS as a static framework, make sure that Mapbox.bundle is copied into the root of the app bundle."];
}
}

Expand Down
8 changes: 4 additions & 4 deletions platform/darwin/test/MGLOfflinePackTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ - (void)testInvalidation {

XCTAssertEqual(invalidPack.state, MGLOfflinePackStateInvalid, @"Offline pack should be invalid when initialized independently of MGLOfflineStorage.");

XCTAssertThrowsSpecificNamed(invalidPack.region, NSException, @"Invalid offline pack", @"Invalid offline pack should raise an exception when accessing its region.");
XCTAssertThrowsSpecificNamed(invalidPack.context, NSException, @"Invalid offline pack", @"Invalid offline pack should raise an exception when accessing its context.");
XCTAssertThrowsSpecificNamed([invalidPack resume], NSException, @"Invalid offline pack", @"Invalid offline pack should raise an exception when being resumed.");
XCTAssertThrowsSpecificNamed([invalidPack suspend], NSException, @"Invalid offline pack", @"Invalid offline pack should raise an exception when being suspended.");
XCTAssertThrowsSpecificNamed(invalidPack.region, NSException, MGLInvalidOfflinePackException, @"Invalid offline pack should raise an exception when accessing its region.");
XCTAssertThrowsSpecificNamed(invalidPack.context, NSException, MGLInvalidOfflinePackException, @"Invalid offline pack should raise an exception when accessing its context.");
XCTAssertThrowsSpecificNamed([invalidPack resume], NSException, MGLInvalidOfflinePackException, @"Invalid offline pack should raise an exception when being resumed.");
XCTAssertThrowsSpecificNamed([invalidPack suspend], NSException, MGLInvalidOfflinePackException, @"Invalid offline pack should raise an exception when being suspended.");
}

- (void)testProgressBoxing {
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/test/MGLOfflineRegionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ - (void)testStyleURLs {
XCTAssertEqualObjects(region.styleURL, [MGLStyle streetsStyleURLWithVersion:MGLStyleDefaultVersion], @"Streets isn’t the default style.");

NSURL *localURL = [NSURL URLWithString:@"beautiful.style"];
XCTAssertThrowsSpecificNamed([[MGLTilePyramidOfflineRegion alloc] initWithStyleURL:localURL bounds:bounds fromZoomLevel:0 toZoomLevel:DBL_MAX], NSException, @"Invalid style URL", @"No exception raised when initializing region with a local file URL as the style URL.");
XCTAssertThrowsSpecificNamed([[MGLTilePyramidOfflineRegion alloc] initWithStyleURL:localURL bounds:bounds fromZoomLevel:0 toZoomLevel:DBL_MAX], NSException, MGLInvalidStyleURLException, @"No exception raised when initializing region with a local file URL as the style URL.");
}

- (void)testEquality {
Expand Down
Loading