forked from mxcl/PromiseKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.swift
More file actions
87 lines (73 loc) · 2.45 KB
/
Error.swift
File metadata and controls
87 lines (73 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import Foundation
public enum PMKError: Error {
/**
The completionHandler with form (T?, ErrorType?) was called with (nil, nil)
This is invalid as per Cocoa/Apple calling conventions.
*/
case invalidCallingConvention
/**
A handler returned its own promise. 99% of the time, this is likely a
programming error. It is also invalid per Promises/A+.
*/
case returnedSelf
/** `when()`, `race()` etc. were called with invalid parameters, eg. an empty array. */
case badInput
/// The operation was cancelled
case cancelled
/// `nil` was returned from `flatMap`
@available(*, deprecated, message: "See: `compactMap`")
case flatMap(Any, Any.Type)
case compactMap(Any, Any.Type)
/// the lastValue or firstValue of a sequence was requested but the sequence was empty
case emptySequence
}
extension PMKError: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case .flatMap(let obj, let type):
return "Could not `flatMap<\(type)>`: \(obj)"
case .compactMap(let obj, let type):
return "Could not `compactMap<\(type)>`: \(obj)"
case .invalidCallingConvention:
return "A closure was called with an invalid calling convention, probably (nil, nil)"
case .returnedSelf:
return "A promise handler returned itself"
case .badInput:
return "Bad input was provided to a PromiseKit function"
case .cancelled:
return "The asynchronous sequence was cancelled"
case .emptySequence:
return "The first or last element was requested for an empty sequence"
}
}
}
extension PMKError: LocalizedError {
public var errorDescription: String? {
return debugDescription
}
}
//////////////////////////////////////////////////////////// Cancellation
public protocol CancellableError: Error {
var isCancelled: Bool { get }
}
extension Error {
public var isCancelled: Bool {
do {
throw self
} catch PMKError.cancelled {
return true
} catch let error as CancellableError {
return error.isCancelled
} catch URLError.cancelled {
return true
} catch CocoaError.userCancelled {
return true
} catch {
return false
}
}
}
public enum CatchPolicy {
case allErrors
case allErrorsExceptCancellation
}