csantanapr commented on a change in pull request #23: Add Codable Support for 
Swift 4.x
URL: 
https://github.com/apache/incubator-openwhisk-runtime-swift/pull/23#discussion_r172730781
 
 

 ##########
 File path: core/swift40Action/epilogue.swift
 ##########
 @@ -21,24 +21,100 @@ import Foundation
 let inputStr: String = readLine() ?? "{}"
 let json = inputStr.data(using: .utf8, allowLossyConversion: true)!
 
+func _whisk_print_error(message: String, error: Error?){
+    if let error = error {
+        print("{\"error\":\"\(message) \(error.localizedDescription)\"}")
+    } else {
+       print("{\"error\":\"\(message)\"}")
+    }
+}
 
 // snippet of code "injected" (wrapper code for invoking traditional main)
 func _run_main(mainFunction: ([String: Any]) -> [String: Any]) -> Void {
-    let parsed = try! JSONSerialization.jsonObject(with: json, options: []) 
as! [String: Any]
-    let result = mainFunction(parsed)
-    if JSONSerialization.isValidJSONObject(result) {
-        do {
-            let jsonData = try JSONSerialization.data(withJSONObject: result, 
options: [])
-            if let jsonStr = String(data: jsonData, encoding: 
String.Encoding.utf8) {
-                print("\(jsonStr)")
-            } else {
-                print("Error serializing data to JSON, data conversion returns 
nil string")
+    do {
+        let parsed = try JSONSerialization.jsonObject(with: json, options: []) 
as! [String: Any]
+        let result = mainFunction(parsed)
+        if JSONSerialization.isValidJSONObject(result) {
+            do {
+                let jsonData = try JSONSerialization.data(withJSONObject: 
result, options: [])
+                if let jsonStr = String(data: jsonData, encoding: 
String.Encoding.utf8) {
+                    print("\(jsonStr)")
+                } else {
+                    _whisk_print_error(message: "Error serializing data to 
JSON, data conversion returns nil string", error: nil)
+                }
+            } catch {
+                _whisk_print_error(message: "JFailed to encode Dictionary type 
to JSON string:", error: error)
 
 Review comment:
   yeah Typo

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to