Skip to content

Commit 030e50d

Browse files
committed
Improved error handling
1 parent 033a95d commit 030e50d

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

PACore/Source/PAInvocation.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88

99
import Foundation
1010

11+
public enum PAInvocationError: Error {
12+
case invalidData
13+
14+
public var localizedDescription: String {
15+
switch self {
16+
case .invalidData:
17+
return NSLocalizedString("Unable to process selection data", comment: "Unable to process selection data (error message)")
18+
}
19+
}
20+
}
21+
1122
public final class PAInvocation {
1223

1324
public enum Platform: String {
@@ -46,7 +57,7 @@ public final class PAInvocation {
4657
}
4758
}
4859

49-
public class func invoke(with buffer: String, platform: Platform = .iOS) {
60+
public class func invoke(with buffer: String, platform: Platform = .iOS) throws {
5061
let info = [
5162
Keys.buffer: buffer,
5263
Keys.platform: platform.rawValue.lowercased()
@@ -58,7 +69,7 @@ public final class PAInvocation {
5869
DistributedNotificationCenter.default().post(name: Notifications.CreatePlayground, object: json, userInfo: nil)
5970
}
6071
} catch {
61-
72+
throw PAInvocationError.invalidData
6273
}
6374
}
6475

PlayAwaysExtension/Commands.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ import Foundation
1010
import XcodeKit
1111
import PACore
1212

13+
final class Commander {
14+
15+
class func perform(with invocation: XCSourceEditorCommandInvocation, for platform: PAInvocation.Platform) {
16+
do {
17+
try PAInvocation.invoke(with: invocation.buffer.completeBuffer, platform: platform)
18+
} catch {
19+
NSAlert(error: error).runModal()
20+
}
21+
}
22+
23+
}
24+
1325
class EmbeddedCommand: NSObject, XCSourceEditorCommand {
1426

1527
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
16-
PAInvocation.invoke(with: invocation.buffer.completeBuffer, platform: .iOS)
28+
Commander.perform(with: invocation, for: .iOS)
1729

1830
completionHandler(nil)
1931
}
@@ -23,7 +35,7 @@ class EmbeddedCommand: NSObject, XCSourceEditorCommand {
2335
class MacCommand: NSObject, XCSourceEditorCommand {
2436

2537
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
26-
PAInvocation.invoke(with: invocation.buffer.completeBuffer, platform: .macOS)
38+
Commander.perform(with: invocation, for: .macOS)
2739

2840
completionHandler(nil)
2941
}
@@ -33,7 +45,7 @@ class MacCommand: NSObject, XCSourceEditorCommand {
3345
class TVCommand: NSObject, XCSourceEditorCommand {
3446

3547
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
36-
PAInvocation.invoke(with: invocation.buffer.completeBuffer, platform: .tvOS)
48+
Commander.perform(with: invocation, for: .tvOS)
3749

3850
completionHandler(nil)
3951
}

0 commit comments

Comments
 (0)