OSDN Git Service

JSONCommand#execute()に例外の発生可能性を示した
authormasakih <masakih@users.sourceforge.jp>
Mon, 6 Mar 2017 13:06:33 +0000 (22:06 +0900)
committermasakih <masakih@users.sourceforge.jp>
Mon, 6 Mar 2017 13:06:33 +0000 (22:06 +0900)
KCD/CommandRegister.swift
KCD/JSONCommand.swift
KCD/JSONViewCommand.swift

index 237abe1..7fb47c6 100644 (file)
@@ -56,35 +56,27 @@ class CommandRegister {
         registerClass(AirCorpsChangeNameCommand.self)
     }
     
-    class func command(for response: APIResponse) throws -> JSONCommand {
-        var command: JSONCommand? = nil
-        if !response.success {
-            command = FailedCommand(apiResponse: response)
-        }
-        for c in registeredClasses {
-            if c.canExecuteAPI(response.api) {
-                command = c.init(apiResponse: response)
-                break
+    class func command(for response: APIResponse) -> JSONCommand {
+        func concreteCommand(for response: APIResponse) -> JSONCommand {
+            if !response.success {
+                return FailedCommand(apiResponse: response)
+            }
+            for c in registeredClasses {
+                if c.canExecuteAPI(response.api) {
+                    return c.init(apiResponse: response)
+                }
             }
-        }
-        
-        if command == nil {
             if IgnoreCommand.canExecuteAPI(response.api) {
-                command = IgnoreCommand(apiResponse: response)
+                return IgnoreCommand(apiResponse: response)
             }
+            return UnknownComand(apiResponse: response)
         }
-        if command == nil {
-            command = UnknownComand(apiResponse: response)
-        }
-        
-        guard var validCommand = command
-            else { throw JSONCommandError.CanNotFindCommand }
         
         #if ENABLE_JSON_LOG
-            validCommand = JSONViewCommand(apiResponse: response, command: validCommand)
+            return JSONViewCommand(apiResponse: response, command: concreteCommand(for: response))
+        #else
+            return concreteCommand(for: response)
         #endif
-        
-        return validCommand
     }
     class func registerClass(_ commandClass: JSONCommand.Type) {
         if registeredClasses.contains(where: { $0 == commandClass }) { return }
index c476a3b..cf5238b 100644 (file)
@@ -23,5 +23,5 @@ class JSONCommand {
     
     var dataKey: String { return "api_data" }
 
-    func execute() {}
+    func execute() throws {}
 }
index 61f1fe5..81c8b5e 100644 (file)
@@ -31,7 +31,12 @@ class JSONViewCommand: JSONCommand {
     }
     
     override func execute() {
-        command.execute()
+        do {
+            try command.execute()
+        }
+        catch {
+            print("JSONTracker Cought Exception -> \(error)")
+        }
         
         guard let _ = jsonTree else { return print("jsonTree is nil.") }
         guard let _ = recieveDate else { return print("recieveDate is nil.") }