Skip to content

Commit

Permalink
Add convenience methods to check for predefined errors
Browse files Browse the repository at this point in the history
  • Loading branch information
shoumikhin committed May 18, 2018
1 parent 6e2bff4 commit 2780035
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion PromisesObjC.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PromisesObjC'
s.version = '1.2'
s.version = '1.2.1'
s.authors = 'Google Inc.'
s.license = { :type => 'Apache', :file => 'LICENSE' }
s.homepage = 'https://github.com/google/promises'
Expand Down
2 changes: 1 addition & 1 deletion PromisesSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PromisesSwift'
s.version = '1.2'
s.version = '1.2.1'
s.authors = 'Google Inc.'
s.license = { :type => 'Apache', :file => 'LICENSE' }
s.homepage = 'https://github.com/google/promises'
Expand Down
12 changes: 6 additions & 6 deletions Sources/FBLPromises/FBLPromise+Catch.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

@implementation FBLPromise (CatchAdditions)

- (FBLPromise *)catch:(FBLPromiseCatchBlock)reject {
- (FBLPromise *)catch:(FBLPromiseCatchWorkBlock)reject {
return [self onQueue:FBLPromise.defaultDispatchQueue catch:reject];
}

- (FBLPromise *)onQueue:(dispatch_queue_t)queue catch:(FBLPromiseCatchBlock)reject {
- (FBLPromise *)onQueue:(dispatch_queue_t)queue catch:(FBLPromiseCatchWorkBlock)reject {
NSParameterAssert(queue);
NSParameterAssert(reject);

Expand All @@ -40,14 +40,14 @@ - (FBLPromise *)onQueue:(dispatch_queue_t)queue catch:(FBLPromiseCatchBlock)reje

@implementation FBLPromise (DotSyntax_CatchAdditions)

- (FBLPromise* (^)(FBLPromiseCatchBlock))catch {
return ^(FBLPromiseCatchBlock catch) {
- (FBLPromise* (^)(FBLPromiseCatchWorkBlock))catch {
return ^(FBLPromiseCatchWorkBlock catch) {
return [self catch:catch];
};
}

- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseCatchBlock))catchOn {
return ^(dispatch_queue_t queue, FBLPromiseCatchBlock catch) {
- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseCatchWorkBlock))catchOn {
return ^(dispatch_queue_t queue, FBLPromiseCatchWorkBlock catch) {
return [self onQueue:queue catch:catch];
};
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/FBLPromises/FBLPromiseError.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

#import "FBLPromiseError.h"

NSString *const FBLPromiseErrorDomain = @"com.google.FBLPromises.Error";
NSErrorDomain const FBLPromiseErrorDomain = @"com.google.FBLPromises.Error";
10 changes: 5 additions & 5 deletions Sources/FBLPromises/include/FBLPromise+Catch.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface FBLPromise<Value>(CatchAdditions)

typedef void (^FBLPromiseCatchBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseCatchWorkBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");

/**
Creates a pending promise which eventually gets resolved with same resolution as the receiver.
Expand All @@ -29,7 +29,7 @@ typedef void (^FBLPromiseCatchBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
@param reject A block to handle the error that receiver was rejected with.
@return A new pending promise.
*/
- (FBLPromise *)catch:(FBLPromiseCatchBlock)reject NS_SWIFT_UNAVAILABLE("");
- (FBLPromise *)catch:(FBLPromiseCatchWorkBlock)reject NS_SWIFT_UNAVAILABLE("");

/**
Creates a pending promise which eventually gets resolved with same resolution as the receiver.
Expand All @@ -40,7 +40,7 @@ typedef void (^FBLPromiseCatchBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
@return A new pending promise.
*/
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
catch:(FBLPromiseCatchBlock)reject NS_REFINED_FOR_SWIFT;
catch:(FBLPromiseCatchWorkBlock)reject NS_REFINED_FOR_SWIFT;

@end

Expand All @@ -50,8 +50,8 @@ typedef void (^FBLPromiseCatchBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
*/
@interface FBLPromise<Value>(DotSyntax_CatchAdditions)

- (FBLPromise* (^)(FBLPromiseCatchBlock))catch FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseCatchBlock))catchOn FBL_PROMISES_DOT_SYNTAX
- (FBLPromise* (^)(FBLPromiseCatchWorkBlock))catch FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseCatchWorkBlock))catchOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");

@end
Expand Down
14 changes: 12 additions & 2 deletions Sources/FBLPromises/include/FBLPromiseError.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@

NS_ASSUME_NONNULL_BEGIN

extern NSErrorDomain const FBLPromiseErrorDomain NS_REFINED_FOR_SWIFT;

/**
Possible error codes in `FBLPromiseErrorDomain`.
*/
typedef NS_ENUM(NSInteger, FBLPromiseErrorCode) {
typedef NS_ERROR_ENUM(FBLPromiseErrorDomain, FBLPromiseErrorCode) {
/** Promise failed to resolve in time. */
FBLPromiseErrorCodeTimedOut = 1,
/** Validation predicate returned false. */
FBLPromiseErrorCodeValidationFailure = 2,
} NS_REFINED_FOR_SWIFT;

extern NSString* const FBLPromiseErrorDomain NS_REFINED_FOR_SWIFT;
NS_INLINE BOOL FBLPromiseErrorIsTimedOut(NSError *error) NS_SWIFT_UNAVAILABLE("") {
return error.domain == FBLPromiseErrorDomain &&
error.code == FBLPromiseErrorCodeTimedOut;
}

NS_INLINE BOOL FBLPromiseErrorIsValidationFailure(NSError *error) NS_SWIFT_UNAVAILABLE("") {
return error.domain == FBLPromiseErrorDomain &&
error.code == FBLPromiseErrorCodeValidationFailure;
}

NS_ASSUME_NONNULL_END
8 changes: 4 additions & 4 deletions Sources/Promises/PromiseError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ extension PromiseError {
let error = error as NSError
if error.domain != __FBLPromiseErrorDomain { return nil }
switch error.code {
case __FBLPromiseErrorCode.timedOut.rawValue:
case __FBLPromiseError.timedOut.rawValue:
self = .timedOut
case __FBLPromiseErrorCode.validationFailure.rawValue:
case __FBLPromiseError.validationFailure.rawValue:
self = .validationFailure
default:
return nil
Expand All @@ -46,9 +46,9 @@ extension PromiseError: CustomNSError {
public var errorCode: Int {
switch self {
case .timedOut:
return __FBLPromiseErrorCode.timedOut.rawValue
return __FBLPromiseError.timedOut.rawValue
case .validationFailure:
return __FBLPromiseErrorCode.validationFailure.rawValue
return __FBLPromiseError.validationFailure.rawValue
}
}

Expand Down
6 changes: 2 additions & 4 deletions Tests/FBLPromisesTests/FBLPromise+TimeoutTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ - (void)testPromiseTimeoutFail {
XCTFail();
return nil;
}] catch:^(NSError *error) {
XCTAssertEqualObjects(error.domain, FBLPromiseErrorDomain);
XCTAssertEqual(error.code, FBLPromiseErrorCodeTimedOut);
XCTAssertTrue(FBLPromiseErrorIsTimedOut(error));
}];

// Assert.
XCTAssert(FBLWaitForPromisesWithTimeout(10));
XCTAssertEqualObjects(promise.error.domain, FBLPromiseErrorDomain);
XCTAssertEqual(promise.error.code, FBLPromiseErrorCodeTimedOut);
XCTAssertTrue(FBLPromiseErrorIsTimedOut(promise.error));
XCTAssertNil(promise.value);
}

Expand Down
6 changes: 2 additions & 4 deletions Tests/FBLPromisesTests/FBLPromise+ValidateTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ - (void)testPromiseValidateFalse {
XCTFail();
return nil;
}] catch:^(NSError *error) {
XCTAssertEqualObjects(error.domain, FBLPromiseErrorDomain);
XCTAssertEqual(error.code, FBLPromiseErrorCodeValidationFailure);
XCTAssertTrue(FBLPromiseErrorIsValidationFailure(error));
}];

// Assert.
XCTAssert(FBLWaitForPromisesWithTimeout(10));
XCTAssertEqualObjects(promise.error.domain, FBLPromiseErrorDomain);
XCTAssertEqual(promise.error.code, FBLPromiseErrorCodeValidationFailure);
XCTAssertTrue(FBLPromiseErrorIsValidationFailure(promise.error));
XCTAssertNil(promise.value);
}

Expand Down
6 changes: 3 additions & 3 deletions g3doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ In your `Package.swift` file, add `Promises` dependency to corresponding targets
let package = Package(
// ...
dependencies: [
.package(url: "https://github.com/google/promises.git", from: "1.2"),
.package(url: "https://github.com/google/promises.git", from: "1.2.1"),
],
// ...
)
Expand Down Expand Up @@ -443,13 +443,13 @@ Or, the module, if `CLANG_ENABLE_MODULES = YES`:
To use `Promises` for both Swift and Objective-C, add the following to your `Podfile`:

```ruby
pod 'PromisesSwift', '~> 1.2'
pod 'PromisesSwift', '~> 1.2.1'
```

To use `Promises` for Objective-C only, add the following to your `Podfile`:

```ruby
pod 'PromisesObjC', '~> 1.2'
pod 'PromisesObjC', '~> 1.2.1'
```

Also, don't forget to `use_frameworks!` in your target. Then, run `pod install`.
Expand Down

0 comments on commit 2780035

Please sign in to comment.